ListView的多条目

这次说一下listview 的多条目展示

多条目展示   比普通的适配器要多两个方法 :

getItemViewType       设置多条目的类型,加一层判断   

getViewTypeCount    有几种类型,写的要比它大,比如有两种类型,那就可以输入所有>2的

下面就是具体代码

class myAdapter extends BaseAdapter{



        List<DateBean.ItemListBeanX.DataBeanX.ItemListBean> list;

        public myAdapter(List<DateBean.ItemListBeanX.DataBeanX.ItemListBean> list) {
            this.list = list;
        }
        @Override
        public int getItemViewType(int position) {
            if (position%2==0){
                return TYPE_0;
            }else {
                return TYPE_1;
            }
        }

        @Override
        public int getViewTypeCount() {
            return 2;
        }
        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int i) {
            return list.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override

        public View getView(int i, View view, ViewGroup viewGroup) {

            //获取类型

            int type = getItemViewType(i);

            //两种类型,两个viewHolder

            viewHolder0 holder0 = null;
            viewHolder1 holder1 = null;
            //具体实现内容看清楚
            switch (type){
                case 0:
                    if (view==null){
                        view = View.inflate(getActivity(),R.layout.er,null);
                        holder0=new viewHolder0();
                        holder0.text_1 = (TextView) view.findViewById(R.id.text1_f1);
                        holder0.img_1= (ImageView) view.findViewById(R.id.img1_f1);
                        view.setTag(holder0);
                    }else {
                        holder0 = (viewHolder0) view.getTag();
                    }
                    break;
                case 1:
                    if (view==null){
                        view = View.inflate(getActivity(),R.layout.san,null);
                        holder1 = new viewHolder1();
                        holder1.text_2 = (TextView) view.findViewById(R.id.text2_f1);
                        holder1.img_2= (ImageView) view.findViewById(R.id.img2_f1);
                        view.setTag(holder1);
                    }else {
                        holder1 = (viewHolder1) view.getTag();
                    }
                    break;

            }

             //虽然控件是一样多的,但是位置是不一样的,XMl文件就不写了

            switch (type){
                case 0:
                    holder0.text_1.setText(list.get(i).getData().getTitle());
                    loderImage(list.get(i).getData().getImage(),holder0.img_1);
                    break;
                case 1:
                    holder1.text_2.setText(list.get(i).getData().getTitle());
                    loderImage(list.get(i).getData().getImage(),holder1.img_2);
                    break;
            }

            return view;
        }



        class viewHolder0{
            ImageView img_1;
            TextView text_1;
        }
        class viewHolder1{
            ImageView img_2;
            TextView text_2;
        }

    }


以上就是我分享的listview 的多条目展示,还有一些不足和没有说到的,请多多包涵


PS:有不同的方法,这是我掌握的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值