在Android中的ExpandableLists,我只想一次只允许一个父列表展开 [英] ExpandableLists in Android, I want to allow only one parent list to expand at a time

查看:68
本文介绍了在Android中的ExpandableLists,我只想一次只允许一个父列表展开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to make only one item in the parent list expand at a time, my code for the onCreate is currently the following. (It works as I want, But the method to allow only one parent open at a time doesnt work)

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文件中.所以我不知道如何将以上内容应用到我的程序中...

I understand the above code, However, I dont use expandableListViews, Im refferncing my program to my .xml files with the layouts of the expandablelist. And so I dont know how to apply the above to my program...

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>

我不知道如何应用的方法如下:

The method that I dont know how to apply is the following:

                    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活动中?

1) how do I refference the 'list' to my expandablelist activity?

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

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

I do not have any errors right now, But the method does not work.

任何帮助将不胜感激!

推荐答案

通过创建一个名为list的公共explist变量,然后通过以下调用将其引用到适配器视图中,解决了该问题.

The problem was solved by creating a public explist variable called list, and then referencing it to the adapter in view with the following call.

list = getExpandableListView(); 

因为此原因,之前的expand方法使应用程序崩溃.引用list.collapsegroup后,它工作正常.

The expand method before was crashing the app because of this. After refferencing the list.collapsegroup in, it worked fine.

public void onGroupExpand(int groupPosition) { 
                    System.out.println("GROUP LISTENER WORKS?");
                    if(adptr=="expListAdapter"){
                        int len = expListAdapter.getGroupCount();
                        //System.out.println(len);
                        for (int i = 0; i < len; i++) {
                            if (i != groupPosition) {
                            list.collapseGroup(i);
                            }
                        }

                    }
                    else if(adptr=="expListAdapter2"){
                        int len = expListAdapter2.getGroupCount();
                        for (int i = 0; i < len; i++) {
                            if (i != groupPosition) {
                            list.collapseGroup(i);
                            }
                        }
                        //System.out.println(len);
                    }
                    else if(adptr=="expListAdapter3"){
                        int len = expListAdapter3.getGroupCount();
                        for (int i = 0; i < len; i++) {
                            if (i != groupPosition) {
                            list.collapseGroup(i);
                            }
                        }

                    }
            }

这篇关于在Android中的ExpandableLists,我只想一次只允许一个父列表展开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆