前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >一个简单好用的二级选项列表弹出框demo

一个简单好用的二级选项列表弹出框demo

作者头像
项勇
发布2023-03-24 13:23:32
4640
发布2023-03-24 13:23:32
举报
文章被收录于专栏:项勇项勇

年后第一篇笔记,还是听听过年歌吧

效果

用法

1.首先要准备好要填充的一级列表的的数据和对应的二级列表数据

demo一级列表一共是三个选项

所以会有三个子选项的选项集合

代码语言:javascript
复制
ParentList = new ArrayList<>();
for (CharSequence ss : getResources().getTextArray(R.array.parent_item)){
    ParentList.add((String)ss);
}
ArrayList<String> childLists1 = new ArrayList<>();
for (CharSequence ss : getResources().getTextArray(R.array.child_1)){
    childLists1.add((String)ss);
}
ArrayList<String> childLists2 = new ArrayList<>();
for (CharSequence ss : getResources().getTextArray(R.array.child_2)){
    childLists2.add((String)ss);
}
ArrayList<String> childLists3 = new ArrayList<>();
for (CharSequence ss : getResources().getTextArray(R.array.child_3)){
    childLists3.add((String)ss);
}
ChildLists = new ArrayList<>();
ChildLists.add(childLists1);
ChildLists.add(childLists2);
ChildLists.add(childLists3);

2.把准备好的一级二级数据传入adapter

代码语言:javascript
复制
...
mExpandableListView = dialogView.findViewById(R.id.expandablelistview);
final MultistageAdapter moAdapter = new MultistageAdapter(this,ParentList, ChildLists, mParentmValue, mChildValue);
mExpandableListView.setAdapter(moAdapter);
...

3.监听点击事件

代码语言:javascript
复制
//一级选项点击回调
mExpandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
    @Override
    public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
        mParentmValue2 =  groupPosition;
        mChildValue2 = 0;
        moAdapter.notifyDataSetChanged(mParentmValue2,mChildValue2); //更新选中状态
        return false;
    }
});
//二级选项点击回调
mExpandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
    @Override
    public boolean onChildClick(ExpandableListView parent, View v, int reverseTypePosition, int reverseCisPosition, long id) {
        mParentmValue2 =  reverseTypePosition;
        mChildValue2 =  reverseCisPosition;
        moAdapter.notifyDataSetChanged(mParentmValue2,mChildValue2);//更新选中状态
        return false;
    }
});

4.确认框中处理

代码语言:javascript
复制
private void showConfirmDialog(){
    final AlertDialog.Builder normalDialog = new AlertDialog.Builder(this);
    final String value = ParentList.get(mParentmValue2)+"/"+ChildLists.get(mParentmValue2).get(mChildValue);
    normalDialog.setTitle(value);
    normalDialog.setPositiveButton("OK",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    mParentmValue =  mParentmValue2;
                    mChildValue =  mChildValue2;
                    Toast.makeText(getApplicationContext(),value,Toast.LENGTH_SHORT).show();
                }
            });
    ...
    normalDialog.show();
}
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-02-27,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 项勇 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档