ExpandableListView中ListView无法使用Item的监听事件

开发中很常见的一个问题,项目中的listview不仅仅是简单的文字,常常需要自己定义listview,自己的Adapter去继承BaseAdapter,在adapter中按照需求进行编写,问题就出现了,可能会发生点击每一个item的时候没有反应,无法获取的焦点。原因多半是由于在你自己定义的Item中存在诸如ImageButton,Button,CheckBox等子控件(也可以说是Button或者Checkable的子类控件),此时这些子控件会将焦点获取到,所以常常当点击item时变化的是子控件,item本身的点击没有响应。
这时候就可以使用descendantFocusability来解决
该属性是当一个为view获取焦点时,定义viewGroup和其子控件两者之间的关系。

属性的值有三种:

    beforeDescendants:viewgroup会优先其子类控件而获取到焦点

    afterDescendants:viewgroup只有当其子类控件不需要获取焦点时才获取焦点

    blocksDescendants:viewgroup会覆盖子类控件而直接获得焦点

想实现ChildView的点击事件,实现接口onChildClick
第一,在适配器里isChildSelectable 必须返回true.
第二,在ChildView布局child_item_layout.xml最外层的layout设置个属性 android:descendantFocusability="blocksDescendants"

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是关于 Android ExpandableListView使用方法: 1. 在 layout 文件添加 ExpandableListView 组件。例如: ```xml <ExpandableListView android:id="@+id/my_expandable_listview" android:layout_width="match_parent" android:layout_height="match_parent" /> ``` 2. 创建一个 ExpandableListAdapter 类,继承自 BaseExpandableListAdapter,实现其的方法。例如: ```java public class MyExpandableListAdapter extends BaseExpandableListAdapter { private Context mContext; private List<String> mGroupData; private List<List<String>> mChildData; public MyExpandableListAdapter(Context context, List<String> groupData, List<List<String>> childData) { mContext = context; mGroupData = groupData; mChildData = childData; } @Override public int getGroupCount() { return mGroupData.size(); } @Override public int getChildrenCount(int groupPosition) { return mChildData.get(groupPosition).size(); } @Override public Object getGroup(int groupPosition) { return mGroupData.get(groupPosition); } @Override public Object getChild(int groupPosition, int childPosition) { return mChildData.get(groupPosition).get(childPosition); } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public boolean hasStableIds() { return true; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { if (convertView == null) { convertView = LayoutInflater.from(mContext).inflate(R.layout.group_item_layout, parent, false); } TextView groupTextView = convertView.findViewById(R.id.group_textview); groupTextView.setText(mGroupData.get(groupPosition)); return convertView; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { if (convertView == null) { convertView = LayoutInflater.from(mContext).inflate(R.layout.child_item_layout, parent, false); } TextView childTextView = convertView.findViewById(R.id.child_textview); childTextView.setText(mChildData.get(groupPosition).get(childPosition)); return convertView; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } } ``` 3. 在 Activity 或 Fragment ,初始化 ExpandableListView,并设置适配器。例如: ```java ExpandableListView myExpandableListView = findViewById(R.id.my_expandable_listview); MyExpandableListAdapter myExpandableListAdapter = new MyExpandableListAdapter(this, groupData, childData); myExpandableListView.setAdapter(myExpandableListAdapter); ``` 4. (可选)设置 ExpandableListView监听器,例如监听组展开/收起事件: ```java myExpandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() { @Override public void onGroupExpand(int groupPosition) { // 当组展开时的操作 } }); myExpandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() { @Override public void onGroupCollapse(int groupPosition) { // 当组收起时的操作 } }); ``` 这样就完成了 ExpandableListView 的基本使用。需要注意的是,ExpandableListView 的组和子项都需要自己定义布局,例如在上面的适配器,分别使用了 group_item_layout 和 child_item_layout 两个布局。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值