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

前端之家收集整理的这篇文章主要介绍了Android中的ExpandableLists,我想一次只允许一个父列表扩展前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想一次只扩展父列表中的一个项目,我的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,我使用expandablelist的布局将我的程序保存到我的.xml文件中.所以我不知道如何将上述内容应用到我的程序中……

main.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)如何将“列表”引用到我的expandablelist活动?

list在类中定义为私有的ExpandableListView列表;

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

任何帮助是极大的赞赏!

解决方法

// 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;
    }
});
原文链接:https://www.f2er.com/android/317374.html

猜你在找的Android相关文章