使用ExpandableListView实现ListView嵌套ListView

今天写界面发现是个ListView的嵌套


对我这个新手而言,只能发动网友的力量在网上找demo了,奈何没有找到。。。。(可能我搜索关键词不准确尴尬)还浪费了我的积分!后来突然想起之前有用ExpandableListView写过分组列表,让它默认展开并且不能收缩不就实现了我想要的效果了吗?哈哈哈,机智如我:)

关键代码:

 @ViewInject(value = R.id.elv_config)
    ExpandableListView elvConfig;

    List<HashMap<String, String>> gruops = new ArrayList<HashMap<String, String>>();

    List<List<HashMap<String, String>>> childs = new ArrayList<List<HashMap<String, String>>>();


数据我就不贴出来了 吐舌头,用HashMap存储数据,填充的时候与布局id一一对应就行了。
SimpleExpandableListAdapter sela = new SimpleExpandableListAdapter(
                        App.getInstance(), gruops, R.layout.item_car_config, new String[]{"name"},
                        new int[]{R.id.tv_config_parent}, childs, R.layout.item_child_car_config,
                        new String[]{"pzzname","pzznamez"}, new int[]{R.id.tv_config_child,R.id.tv_config_child_content});
                // 加入列表
                elvConfig.setGroupIndicator(null);
                elvConfig.setAdapter(sela);

                // 展开分组
                for (int i = 0; i < gruops.size(); i++) {
                    elvConfig.expandGroup(i);
                }

                // 禁止点击收缩
                elvConfig.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
                    @Override
                    public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                        return true;
                    }
                });


关键的就是设置以下两个事件



因为我的这个列表是在页面里面的fragment中加载出来的,一开始不能适应最外面的ScrollView导致不能填满容器,所以我重写了ExpandableListView

public class CustomeExpandableListView extends ExpandableListView{
    public CustomeExpandableListView(Context context) {
        super(context);
    }

    public CustomeExpandableListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public CustomeExpandableListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    // 重写该方法,使ExpandableListView适应ScrollView
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);

        super.onMeasure(widthMeasureSpec, expandSpec);

    }
}



好了,这算不算是一篇技术博客?用词不当处还望多多包涵~

有好的方法也希望互相学习微笑,一起进步!



今天朋友给我分享了一篇博客,附上地址

ListView中显示不同的视图布局


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值