ExpandableListView控件 使用中问题(不断更新)

1:本意是想在控件初始化数据后,默认选中某一子项,然后定位到该位置,将该子项背景色更改,大概流程为

expandableListView.expandGroup(groupPosition,true);
expandableListView.setSelectedChild(groupPosition, childPosition,true);
expandableListView.smoothScrollToPosition(getPosition(groupPosition, childPosition));

其中getPosition方法为获取子项从第一项开始(包括组)的位置

public int  getPosition(int groupIndex, int childIndex) {
    int position = 0;
    for (int i = 0; i < groupIndex; i++) {
        position++;
        if (expandableListView.isGroupExpanded(i)){
                position = position + expandableListAdapter.getChildrenCount(i);
        }
    }
    position++;
    position = position + childIndex;
    return position;
}

后来发现更改子项背景色时会出现问题,可能跟ExpandableListAdapter的getGroupView和getChildView有关,找了几天没有解决,就暂时搁置了,如有哪位大佬提供思路,再进一步研究.

2:同时只展开一个Group

expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
    @Override
    public void onGroupExpand(int groupPosition) {
        //只展开一个Group
        for (int i = 0; i < expandableListAdapter.getGroupCount(); i++) {
            if (groupPosition != i) {
                expandableListView.collapseGroup(i);
            }
        }
        Log.d(TAG,String.format("onGroupExpand:groupposition %d",groupPosition));
    }
});
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值