gpt4 book ai didi

android - Android 中的 ExpandableLists,我想一次只允许扩展一个父列表

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:07:44 24 4
gpt4 key购买 nike

我只想一次展开父列表中的一项,目前我的 onCreate 代码如下。 (按我的意愿工作,但是一次只允许一个父打开的方法不起作用)

public void onCreate(Bundle savedInstanceState) {
try{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

final SimpleExpandableListAdapter expListAdapter =
new SimpleExpandableListAdapter(
//FOR LEVEL 1 SCREEN
this,
createGroupList(), // METHOD TO CREATE PARENTLIST Creating group List.
R.layout.group_row, // REF TO XML FILENAME.
new String[] { "Group Item" }, // STRING ARRAY HOLDING ONE KEY THAT REF'S ALL PARENT LIST ITEMS.
new int[] { R.id.row_name }, // REF TO I.D. OF PARENT XML. ID of each group item.-Data under the key goes into this TextView.
createChildList(), // METHOD TO CREATE CHILD LST. childData describes second-level entries.
R.layout.child_row, // XMLFILE NAME FOR CHILD LIST.Layout for sub-level entries(second level).
new String[] {"Sub Item"}, // KEY FOR CHILD ITEMS IN HASHMAP. Keys in childData maps to display.
new int[] { R.id.grp_child} // XML ID FOR TEXTVIEW. Data under the keys above go into these TextViews.
);
setListAdapter( expListAdapter ); // setting the adapter in the list.

//**THIS IS WHAT I DONT KNOW HOW TO CODE:**


list = (ExpandableListView) findViewById(R.id.row_name);
list.setAdapter( expListAdapter);
list.setGroupIndicator(null);


list.setOnGroupExpandListener(new OnGroupExpandListener() {

public void onGroupExpand(int groupPosition) {
int len = expListAdapter.getGroupCount();
for (int i = 0; i < len; i++) {
if (i != groupPosition) {
list.collapseGroup(i);
}
}
}
});

}catch(Exception e){

System.out.println("Errrr +++ " + e.getMessage());
}
}

我理解上面的代码,但是,我不使用 expandableListViews,我将我的程序引用到我的 .xml 文件中,其中包含可扩展列表的布局。所以我不知道如何将以上内容应用到我的程序中......

主.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ExpandableListView android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bkg"/>

<TextView android:id="@+id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="No items"/>
</LinearLayout>

不知道怎么申请的方法如下:

                    list = (ExpandableListView) findViewById(R.id.row_name);
list.setAdapter( expListAdapter);
list.setGroupIndicator(null);

list.setOnGroupExpandListener(new OnGroupExpandListener() {
public void onGroupExpand(int groupPosition) {
int len = expListAdapter.getGroupCount();
for (int i = 0; i < len; i++) {
if (i != groupPosition) {
list.collapseGroup(i);
}
}
}
});

1) 如何将“列表”引用到我的可扩展列表 Activity 中?

列表在类中定义为private ExpandableListView list;

我现在没有任何错误,但该方法不起作用。

非常感谢任何帮助!

最佳答案

// Declare variable 
private static int prev = -1;
// and OnGroupExpandListener implement


mExpandableList.setOnGroupExpandListener(new OnGroupExpandListener() {

@Override
public void onGroupExpand(int groupPosition) {

if(prev!=-1)
{

mExpandableList.collapseGroup(prev);

}
prev=groupPosition;
}
});

更新解决方案

mExpandableList.setOnGroupExpandListener(new OnGroupExpandListener() {
// Keep track of previous expanded parent
int previousGroup = -1;

@Override
public void onGroupExpand(int groupPosition) {
// Collapse previous parent if expanded.
if ((previousGroup != -1) && (groupPosition != previousGroup)) {
mExpandableList.collapseGroup(previousGroup);
}
previousGroup = groupPosition;
}
});

关于android - Android 中的 ExpandableLists,我想一次只允许扩展一个父列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9119709/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com