【问题标题】:android expandablelistview does not expand or receive click eventsandroid expandablelistview 不展开也不接收点击事件
【发布时间】:2012-06-16 00:31:06
【问题描述】:

我这辈子都想不通为什么我的 ExpandableListView 没有展开...我几乎在每个可以为 ExpandableListView 找到的点击侦听器中都使用了日志语句,而且看起来它们中的任何一个都没有叫。

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

主要活动:

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;
            }
        });
    }

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

列表视图适配器:

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;
    }


}

组布局:

<?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>

子布局:

<?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>

主页面布局:

<?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>

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

【问题讨论】:

  • 可能是因为你那里没有子数据,你调试了吗
  • 尝试在您的子布局中为 textviews 添加一些默认文本值

标签: android click listener expandablelistview expandablelistadapter


【解决方案1】:

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

让我们看看setOnGroupClickListener中的boolean onGroupClick(...)的正文。你返回了 true 表示"the click was handled"

如果你想扩展,你应该返回 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;
        }
    });

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... */

如果你将onGroupClick 实现为return true,则第 8 行以下的代码将永远不会被执行。 (也就是说,组永远不会被折叠、展开)

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

【讨论】:

  • 您刚刚节省了 3 天的头痛时间。感谢和 +1 的好答案
【解决方案2】:

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

【讨论】:

  • 这对我有帮助。
  • 如果你在 xml 中执行它可能不起作用。添加 button.setFocusable(false);在java中。
【解决方案3】:

您可能需要检查三件事,

  1. 检查您是否有任何可用于孩子的数据,因为如果您没有任何数据,孩子根本不会出现。

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

 if (convertView == null) {
    LayoutInflater inflater = LayoutInflater.from(ctx);
    convertView = inflater.inflate(R.layout.forum_list_child_item_row, null);
    }
  1. 你还需要在这里传递Viewgroup

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

【讨论】:

  • 数据不好是我的问题,再次感谢!!
【解决方案4】:

我知道这已经被回答了,但是尝试设置你要充气的任何东西的基本布局以具有属性:

android:descendantFocusability="blocksDescendants"

【讨论】:

    【解决方案5】:

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

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

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

    【讨论】:

    • 将此代码添加到切换按钮、切换按钮或任何可展开列表视图中。
    【解决方案6】:

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

    【讨论】:

      【解决方案7】:

      我遇到了类似的问题,通过从 xml 上的 ExpandableListView 中删除 android:clickable="true" 属性得到了解决。

      【讨论】:

        【解决方案8】:

        当您使用可扩展列表时,组扩展是其中的默认功能。意味着该组仅在您单击它时才会自行扩展,您不需要覆盖 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;
        }
            }
        
               }
        

        【讨论】:

          【解决方案9】:

          forumListView.collapseGroup(groupPosition);

          应该是

          forumListView.collapseGroup(i);

          【讨论】:

            【解决方案10】:

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

            【讨论】:

              【解决方案11】:

              在我的情况下,我在组和子视图中有按钮,甚至设置 android:focusable="false" android:focusableInTouchMode="false" 两者都不起作用。

              所以我不得不将它们从 ImageButton 更改为 ImageView。点击的监听器是相同的。您可能需要创建自定义背景来为ImageView 提供触摸动画。

              【讨论】:

                猜你喜欢
                • 2013-11-13
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2011-10-29
                相关资源
                最近更新 更多