android expandablelistview 不展开或接收点击事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11008286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:42:32  来源:igfitidea点击:

android expandablelistview does not expand or receive click events

androidclicklistenerexpandablelistviewexpandablelistadapter

提问by Mitch Ware

I can't for the life of me figure out why my ExpandableListView doesn't expand... I have used log statements in just about every click listener I can find for the ExpandableListView and it doesnt look like any of them get called.

我一生都无法弄清楚为什么我的 ExpandableListView 不展开......我几乎在每个可以找到的用于 ExpandableListView 的点击侦听器中都使用了日志语句,并且看起来它们中的任何一个都没有被调用。

I know there are many posts on this topic but I have read through them all and tried many things and am having no luck, hopefully I'm missing some tiny error that will be easy to spot for someone else.

我知道有很多关于这个主题的帖子,但我已经通读了所有帖子并尝试了很多东西,但我没有运气,希望我错过了一些很容易被其他人发现的小错误。

Main Activity:

主要活动:

public class ForumListActivity extends Activity  {

    private static ArrayList<Forum> forumList;
    private static ArrayList<ArrayList<SubForum>> subForumList;
    private ExpandableListView forumListView;
    private ForumListAdapter forumListAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.main_page);
        this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

        forumListView = (ExpandableListView) this.findViewById(R.id.main_page_forum_list);

        forumList = new ArrayList<Forum>();
        subForumList = new ArrayList<ArrayList<SubForum>>();
        setUpForums(this);

        forumListAdapter = new ForumListAdapter(this, forumList, subForumList);
        forumListView.setAdapter(forumListAdapter);

        forumListView.setOnGroupExpandListener(new OnGroupExpandListener(){
            @Override
            public void onGroupExpand(int groupPosition) {
                Log.d("onGroupExpand", "this works?");
                for(int i=0; i<forumListAdapter.getGroupCount(); i++) {
                    if(i != groupPosition) 
                        forumListView.collapseGroup(groupPosition);
                }
            }
        });

        forumListView.setOnGroupClickListener(new OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                Log.d("onGroupClick:", "worked");
                parent.expandGroup(groupPosition);
                return true;
            }
        });
    }

Note: the method setUpForums() just takes system arrays and puts them into forumList and subForumList

注意:setUpForums() 方法只是将系统数组放入forumList 和subForumList

ListViewAdapter:

列表视图适配器:

public class ForumListAdapter extends BaseExpandableListAdapter {

    private ArrayList<Forum> groups;
    private ArrayList<ArrayList<SubForum>> children;
    private Context ctx;

    public ForumListAdapter(Context ctx, ArrayList<Forum> groups, ArrayList<ArrayList<SubForum>> children) {
        this.ctx = ctx;
        this.groups = groups;
        this.children = children;
    }



    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return children.get(groupPosition).get(childPosition);
    }



    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }



    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(ctx);
            convertView = inflater.inflate(R.layout.forum_list_child_item_row, null);
        }

        SubForum currentSubForum = children.get(groupPosition).get(childPosition);
        TextView name = (TextView)convertView.findViewById(R.id.child_row_forum_title);
        TextView desc = (TextView)convertView.findViewById(R.id.child_row_forum_description);

        if (name != null)
            name.setText(currentSubForum.getName());

        if (desc != null)
            desc.setText(currentSubForum.getDescription());

        convertView.setFocusableInTouchMode(true);
        return convertView;
    }



    @Override
    public int getChildrenCount(int groupPosition) {
        return children.get(groupPosition).size();
    }



    @Override
    public Object getGroup(int groupPosition) {
        return groups.get(groupPosition);
    }



    @Override
    public int getGroupCount() {
        return groups.size();
    }



    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }



    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if (convertView == null)
        {
            LayoutInflater inflater = LayoutInflater.from(ctx);
            convertView = inflater.inflate(R.layout.forum_list_group_item_row, null);
        }

        Forum currentForum = (Forum) groups.get(groupPosition);
        TextView name = (TextView) convertView.findViewById(R.id.group_item_forum_title);
        //ImageView image = (ImageView) convertView.findViewById(R.id.group_item_expander_image);

        if(name != null)
            name.setText(currentForum.getName());           

        /*
        if(image != null) {
            int[][] group_state_sets = {{}, {android.R.attr.state_expanded}};
            image.setVisibility(View.VISIBLE);  
            int stateSetIndex = (isExpanded ? 1 : 0) ;  
            Drawable drawable = image.getDrawable();  
            drawable.setState(group_state_sets[stateSetIndex]);  
        }
        */

        return convertView;
    }



    @Override
    public boolean hasStableIds() {
        return true;
    }



    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }


}

Group Layout:

集团布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/turquoise_gradient"
    android:orientation="horizontal"
    android:paddingTop="6dip"
    android:paddingBottom="6dip"
    android:paddingLeft="6dip" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/turquoise_gradient"
        android:orientation="vertical"
        android:padding="2dip" >

        <TextView
            android:id="@+id/group_item_forum_title"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical|left"
            android:gravity="left"
            android:paddingLeft="5dip"
            android:textColor="@color/white"
            android:textSize="16dip" />

    </LinearLayout>

    <!--  
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:gravity="center|right">

        <ImageView
            android:id="@+id/group_item_expander_image"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/collapse_down" />


    </LinearLayout> -->

</LinearLayout>

child layout:

子布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/turquoise_gradient"
    android:orientation="horizontal"
    android:paddingTop="6dip"
    android:paddingBottom="6dip"
    android:paddingLeft="6dip" >


    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="2dip"
        android:background="@drawable/turquoise_gradient" >

        <TextView
            android:id="@+id/child_row_forum_title"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:layout_gravity="center_vertical"
            android:paddingLeft="5dip"
            android:textColor="@color/white"
            android:maxLines="1"
            android:textSize="11dip"  />

         <TextView
            android:id="@+id/child_row_forum_description"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="left"
            android:layout_gravity="center_vertical"
            android:paddingLeft="15dip"
            android:textColor="@color/white"
            android:maxLines="2"
            android:textSize="11dip"  />

    </LinearLayout>

</LinearLayout>

main page layout:

主页面布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/black"
    android:orientation="vertical" >

    <ExpandableListView
        android:id="@+id/main_page_forum_list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@color/black"
        android:divider="@color/black"
        android:dividerHeight="1dip"
        android:clickable="true" />

</LinearLayout>

Any help you can provide is greatly appreciated!

非常感谢您能提供的任何帮助!

回答by Joon Hong

I've also encountered similar problem like you. After a couple of days of investigation, I found that I did something wrong. So I fixed it to work correctly by making small change.

我也遇到过和你一样的问题。经过几天的调查,我发现我做错了什么。所以我通过做一些小的改变来修复它以正常工作。

Let's look at the body of boolean onGroupClick(...)in setOnGroupClickListener. You've returned truethat means "the click was handled"

让我们看看boolean onGroupClick(...)in的主体setOnGroupClickListener。您已返回true表示“点击已处理”

You should return falseif you want to expand. So I suggest you to do like this :

如果你想扩展,你应该返回 false。所以我建议你这样做:

forumListView.setOnGroupClickListener(new OnGroupClickListener() {
    @Override
    public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
            Log.d("onGroupClick:", "worked");
            parent.expandGroup(groupPosition);
            return false;
        }
    });

in android.widget.ExpandableListViewclass, there is a method named boolean handleItemClick(View v, int position, long id)which is responsible for expanding/collapsing groups or passing on the click to the proper child.

android.widget.ExpandableListView课堂上,有一个名为的方法boolean handleItemClick(View v, int position, long id)负责展开/折叠组或将点击传递给适当的孩子。

 /* It's a group click, so pass on event */
         if (mOnGroupClickListener != null) {
             if (mOnGroupClickListener.onGroupClick(this, v,
                     posMetadata.position.groupPos, id)) {
                 posMetadata.recycle();
                 return true;
             }
         }

  /* expanding/collapsing/other tasks... */

if you implement onGroupClickto return true, the the code below 8th line will never be executed. (that means, groups will never be collapsed, expanded)

如果实现onGroupClick,以返回true,低于8号线的代码不会被执行。(这意味着,组永远不会被折叠、展开)

Hope my answer helped you :-) good luck!

希望我的回答对你有帮助:-)祝你好运!

回答by Teo Inke

In case you have a widget on your list item, such as a Button, you may have to add android:focusable="false"to it. The Button was not allowing my list item to be clicked. That was the issue in my case.

如果您的列表项上有一个小部件,例如一个按钮,您可能需要添加android:focusable="false"到它。按钮不允许单击我的列表项。这就是我的问题。

回答by Rakshi

There are probably three things u need to check,

你可能需要检查三件事,

  1. check if u have any data available for the chid, cos if u dont have any data the child will not appear at all.
  1. 检查您是否有任何可用于 chid 的数据,因为如果您没有任何数据,则孩子根本不会出现。

2.try removing if condition check while you using layout inflaters

2. 尝试在使用布局充气器时移除 if 条件检查

 if (convertView == null) {
    LayoutInflater inflater = LayoutInflater.from(ctx);
    convertView = inflater.inflate(R.layout.forum_list_child_item_row, null);
    }
  1. you need to also pass Viewgrouphere

      convertView = inflater.inflate(R.layout.forum_list_child_item_row,parent, false);
    
  1. 你也需要通过Viewgroup这里

      convertView = inflater.inflate(R.layout.forum_list_child_item_row,parent, false);
    

回答by R Brown

I know this was already answered, but try setting the base layout of whatever you're inflating to have the attribute:

我知道这已经得到了回答,但是请尝试设置您要充气的任何内容的基本布局以具有该属性:

android:descendantFocusability="blocksDescendants"

回答by Sumedh Ulhe

If your expandable listview parent has button or switch it doesn't get called i wasted whole day in this. So just use below code

如果您的可扩展列表视图父级有按钮或开关,它不会被调用,我在此浪费了一整天。所以只需使用下面的代码

android:focusable="false"
android:focusableInTouchMode="false"

Add this code inside toggle button,switch button or any which is on expandable listview

将此代码添加到切换按钮、切换按钮或任何可扩展列表视图中

回答by Tobio

Make sure your custom group layout does not have android:textIsSelectable="false"as "true", if the text in textview is set to selectable, the expandable listview would expand in gingerbread but not in jellybean and might not work in ICS too.

确保您的自定义组布局不具有android:textIsSelectable="false"“true”,如果 textview 中的文本设置为可选择,可扩展列表视图将在姜饼中展开但不在 jellybean 中展开,并且可能也无法在 ICS 中工作。

回答by cassioso

I had a similar problem and it was solved by removing the android:clickable="true"property from ExpandableListView on xml.

我有一个类似的问题,它是通过android:clickable="true"从 xml 上的 ExpandableListView 中删除属性来解决的。

回答by Max Volos

forumListView.collapseGroup(groupPosition);

forumListView.collapseGroup( groupPosition);

should be

应该

forumListView.collapseGroup(i);

forumListView.collapseGroup( i);

回答by Anil Jadhav

Add implements OnGroupExpandListenerto your Activity. Then It will works. I am Using same it works fine.

添加implements OnGroupExpandListener到您的活动。然后它会起作用。我正在使用它工作正常。

回答by Anu

When you are working with expandable lists then group expand is default functionality in it . means that group will expand itself only when you click on it you donot need to overrirde onGroupExpand(int groupPosition) or any other method just simply populate your data into your list somthing like this:

当您使用可扩展列表时,组扩展是其中的默认功能。意味着该组仅在您单击它时才会扩展自身,您不需要覆盖 onGroupExpand(int groupPosition) 或任何其他方法,只需将您的数据填充到您的列表中,如下所示:

   public class MyActivity extends Activity { 

  private ExpandableListView forumListView;
  private ForumListAdapter forumListAdapter;
  String[] forumList={"group 1","group 2","group 3"};
 String[][] subForumList={{"group 1 child1","group 1 child1","group 1 child3"},
                     {"group 2 child1","group 2 child2","group 2 child3"},
                     {"group 3 child1","group 3 child2","group 3 child3"},
                     };
     @Override
     public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);


forumListView = (ExpandableListView) this.findViewById(R.id.main_page_forum_list);




forumListAdapter = new ForumListAdapter(this, forumList, subForumList);
forumListView.setAdapter(forumListAdapter);



  /* forumListView.setOnGroupExpandListener(new OnGroupExpandListener(){
    public void onGroupExpand(int groupPosition) {
        Log.d("onGroupExpand", "this shit works?");
        for(int i=0; i<forumListAdapter.getGroupCount(); i++) {
            if(i != groupPosition) 
                forumListView.collapseGroup(groupPosition);
        }
    }
});

forumListView.setOnGroupClickListener(new OnGroupClickListener() {
     public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long  id) {
        Log.d("onGroupClick:", "worked");
        parent.expandGroup(groupPosition);
        return true;
    }
});*/
     }

    public class ForumListAdapter extends BaseExpandableListAdapter {

      String[] groups;
   String[][] children;
     private Context ctx;

   public ForumListAdapter(Context ctx, String[] groups, String[][] children) {
    this.ctx = ctx;
    this.groups = groups;
    this.children = children;
}

public Object getChild(int arg0, int arg1) {
    // TODO Auto-generated method stub
    return children[arg0][arg1];
}

public long getChildId(int arg0, int arg1) {
    // TODO Auto-generated method stub
    return arg1;
}

public View getChildView(int arg0, int arg1, boolean arg2, View arg3,
        ViewGroup arg4) {
    if (arg3 == null) {
        LayoutInflater inflater = LayoutInflater.from(ctx);
        arg3 = inflater.inflate(R.layout.child, null);
    }

    String childData = children[arg0][arg1];
    TextView name = (TextView)arg3.findViewById(R.id.child_row_forum_title);
    TextView desc = (TextView)arg3.findViewById(R.id.child_row_forum_description);

    if (name != null)
        name.setText(childData);

    if (desc != null)
       // desc.setText(currentSubForum.getDescription());

    arg3.setFocusableInTouchMode(true);
    return arg3;}

public int getChildrenCount(int arg0) {
    // TODO Auto-generated method stub
    return children[arg0].length;
}

public Object getGroup(int arg0) {
    // TODO Auto-generated method stub
    return groups[arg0];
}

public int getGroupCount() {
    // TODO Auto-generated method stub
    return groups.length;
}

public long getGroupId(int arg0) {
    // TODO Auto-generated method stub
    return arg0;
}

public View getGroupView(int arg0, boolean arg1, View arg2, ViewGroup arg3) {
    if (arg2 == null)
    {
        LayoutInflater inflater = LayoutInflater.from(ctx);
        arg2 = inflater.inflate(R.layout.group, null);
    }


    TextView name = (TextView) arg2.findViewById(R.id.group_item_forum_title);
    //ImageView image = (ImageView) arg2.findViewById(R.id.group_item_expander_image);

    if(name != null)
        name.setText(groups[arg0]);           

    /*
    if(image != null) {
        int[][] group_state_sets = {{}, {android.R.attr.state_expanded}};
        image.setVisibility(View.VISIBLE);  
        int stateSetIndex = (isExpanded ? 1 : 0) ;  
        Drawable drawable = image.getDrawable();  
        drawable.setState(group_state_sets[stateSetIndex]);  
    }
    */

    return arg2;}

public boolean hasStableIds() {
    // TODO Auto-generated method stub
    return false;
}

public boolean isChildSelectable(int arg0, int arg1) {
    // TODO Auto-generated method stub
    return false;
}
    }

       }