listview和recyclerview多布局

转载 2016年08月31日 14:04:12

1.listview:

class MyAdapter extends BaseAdapter{
    Context mContext;
    LinearLayout linearLayout = null;
    LayoutInflater inflater;
    TextView tex;
    final int VIEW_TYPE = 3;
    final int TYPE_1 = 0;
    final int TYPE_2 = 1;
    final int TYPE_3 = 2;

    public MyAdapter(Context context) {
      // TODO Auto-generated constructor stub
      mContext = context;
      inflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
      // TODO Auto-generated method stub
      return listString.size();
    }

    //每个convert view都会调用此方法,获得当前所需要的view样式
    @Override
    public int getItemViewType(int position) {
      // TODO Auto-generated method stub
      int p = position%6;
      if(p == 0)
        return TYPE_1;
      else if(p < 3)
        return TYPE_2;
      else if(p < 6)
        return TYPE_3;
      else
        return TYPE_1;
    }

    @Override
    public int getViewTypeCount() {
      // TODO Auto-generated method stub
      return 3;
    }

    @Override
    public Object getItem(int arg0) {
      // TODO Auto-generated method stub
      return listString.get(arg0);
    }

    @Override
    public long getItemId(int position) {
      // TODO Auto-generated method stub
      return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
      // TODO Auto-generated method stub
      viewHolder1 holder1 = null;
      viewHolder2 holder2 = null;
      viewHolder3 holder3 = null;
      int type = getItemViewType(position);

      //无convertView,需要new出各个控件
      if(convertView == null)
      { 
        Log.e("convertView = ", " NULL");

        //按当前所需的样式,确定new的布局
        switch(type)
        {
        case TYPE_1:
          convertView = inflater.inflate(R.layout.listitem1, parent, false);
          holder1 = new viewHolder1();
          holder1.textView = (TextView)convertView.findViewById(R.id.textview1);
          holder1.checkBox = (CheckBox)convertView.findViewById(R.id.checkbox);
          Log.e("convertView = ", "NULL TYPE_1");
          convertView.setTag(holder1);
          break;
        case TYPE_2:
          convertView = inflater.inflate(R.layout.listitem2, parent, false);
          holder2 = new viewHolder2();
          holder2.textView = (TextView)convertView.findViewById(R.id.textview2);
          Log.e("convertView = ", "NULL TYPE_2");
          convertView.setTag(holder2);
          break;
        case TYPE_3:
          convertView = inflater.inflate(R.layout.listitem3, parent, false);
          holder3 = new viewHolder3();
          holder3.textView = (TextView)convertView.findViewById(R.id.textview3);
          holder3.imageView = (ImageView)convertView.findViewById(R.id.imageview);
          Log.e("convertView = ", "NULL TYPE_3");
          convertView.setTag(holder3);
          break;
        }
      }else{
        //有convertView,按样式,取得不用的布局
        switch(type)
        {
        case TYPE_1:
          holder1 = (viewHolder1) convertView.getTag();
          Log.e("convertView !!!!!!= ", "NULL TYPE_1");
          break;
        case TYPE_2:
          holder2 = (viewHolder2) convertView.getTag();
          Log.e("convertView !!!!!!= ", "NULL TYPE_2");
          break;
        case TYPE_3:
          holder3 = (viewHolder3) convertView.getTag();
          Log.e("convertView !!!!!!= ", "NULL TYPE_3");
          break;
        }
      }

    //设置资源
      switch(type)
      {
        case TYPE_1:
          holder1.textView.setText(Integer.toString(position));
          holder1.checkBox.setChecked(true);
          break;
        case TYPE_2:
          holder2.textView.setText(Integer.toString(position));
          break;
        case TYPE_3:
          holder3.textView.setText(Integer.toString(position));
          holder3.imageView.setBackgroundResource(R.drawable.icon);
          break;
      }

      return convertView;
    }
  }

  //各个布局的控件资源
  class viewHolder1{
    CheckBox checkBox;
    TextView textView;
  }

  class viewHolder2{
    TextView textView;
  }

  class viewHolder3{
    ImageView imageView;
    TextView textView;
  }
}

2.recyclerview;

public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    Context mContext;


    public MyAdapter(Context context){
        mContext = context;
    }






    class TextViewHolder extends RecyclerView.ViewHolder{


        public TextViewHolder(View itemView) {
            super(itemView);
        }
    }


    class ImageViewHolder extends RecyclerView.ViewHolder{
        public ImageViewHolder(View itemView) {
            super(itemView);
        }
    }







    @Override
    public int getItemViewType(int position) {
        return position%2;
    }



    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 0){
            View view = LayoutInflater.from(mContext).inflate(R.layout.layout_textitem,parent,false);
            return new TextViewHolder(view);
        }else {
            return new ImageViewHolder(LayoutInflater.from(mContext).
                    inflate(R.layout.layout_imageitem,null));
        }
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {


    }


    @Override
    public int getItemCount() {
        return 10;
    }
}







相关文章推荐

RecyclerView使用详解一代替ListView(点击事件,添加头布局,上拉刷新下拉加载)

老规矩,先看效果; 这里我就不再过多的介绍RecyclerView,既然大家在找使用RecyclerView的demo想必大家都了解过了,如果不了解网上随便打开一篇RecyclerView的文章应该都...

说说ListView,Percent布局以及RecyclerView

android常用控件和布局的继承机构 新布局 百分比布局 ListView的使用和优化 RecyclerView的使用...

隐藏Listview和RecyclerView 滑动边界的阴影,去除滚动条加分隔线等等等,让你的布局完美起来~

1、Listview的属性设置,虽然现在用RecyclerView的特多了,很少用Listview了,但是还得说说,假如哪天你看看你曾经的项目,还看到你曾经写的PullToRefreshListVie...
  • kl0715
  • kl0715
  • 2017年02月14日 13:08
  • 2992

ScrollView嵌套RecyclerView、ListView时解决布局问题

scrollview 嵌套recyclerview 时,recyclerview不显示,这就需要我们自己计算recyclerview的高度,比如: ViewGroup.LayoutParams mPa...

RecyclerView多布局使用

  • 2016年12月01日 13:43
  • 27.66MB
  • 下载

RecyclerView多布局

  • 2016年03月01日 13:19
  • 3.08MB
  • 下载

ListView、RecyclerView 两种方式实现聊天界面支持文字、表情、图片和语音信息,支持下拉加载更多

ListView、RecyclerView 两种方式实现聊天界面支持文字、表情、图片和语音信息,支持下拉加载更多...

仿微信聊天列表之RecyclerView多布局

背景最近在做关于聊天的项目,需要用到RecyclerView多布局,然而在网上却没有比较详细的讲解,于是萌生了自己写一篇的念头,可能写的不好,不喜勿喷。转载请标明出处,原创不易效果图不废话直接先上效果...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:listview和recyclerview多布局
举报原因:
原因补充:

(最多只允许输入30个字)