RecyclerView的使用(三)多布局

作者: 丿歌吟有梦 | 来源:发表于2016-10-31 11:28 被阅读11003次

    RecyclerView的使用(三)多布局

    在项目中,我们经常用到多布局的情况,跟ListView一样,我们需要通过ViewType来进行区分。
    界面效果如图:


    这里写图片描述

    (一)单布局的Adapter

    首先我们看下以前单布局的Adapter是如何去写的:

    我们继承了RecyclerView.Adapter,泛型是MyAdapter 中继承的ViewHolder类,这样写后我们的onCreateViewHolder和onBindViewHolder的方法参数都是我们的ViewHolder了,是无法做到区分多布局的。

    class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder>{
            @Override
            public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                return null;
            }
    
            @Override
            public void onBindViewHolder(MyAdapter.MyViewHolder holder, int position) {
    
            }
    
            @Override
            public int getItemCount() {
                return 0;
            }
            //继承RecyclerView.ViewHolder
            class MyViewHolder extends  RecyclerView.ViewHolder{
    
                public MyViewHolder(View itemView) {
                    super(itemView);
                }
            }
        }
    

    (二)多布局的Adapter

    多布局的Adapter我们泛型为RecyclerView.ViewHolder,通过ViewType,我们区分使用不同的ViewHolder。

    RecyclerView提供了方法去区分ViewType

     @Override
     public int getItemViewType(int position) {
          return super.getItemViewType(position);
     }
    

    onCreateViewHolder的第二个参数int viewType就是getItemViewType的返回值

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                return null;
    }
    

    在onBindViewHolder中,我们的来绑定布局

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

    我们看看如何去实现,我们首先定义两个标志,

    public static final int ONE_ITEM = 1;
    public static final int TWO_ITEM = 2;
    

    代码奉上

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class ViewTypeActivity extends AppCompatActivity {
        private RecyclerView mRecyclerView;
        private List<String> mDatas;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            initData();
            setContentView(R.layout.activity_water_full);
            mRecyclerView = (RecyclerView) findViewById(R.id.id_recyclerview);
            mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
            mRecyclerView.addItemDecoration(new SpacesItemDecoration(12));
            mRecyclerView.setAdapter(new ViewTypeAdapter());
        }
    
        private void initData() {
            mDatas = new ArrayList<String>();
            for (int i = 0; i < 24; i++) {
                mDatas.add("位置是" + i);
            }
        }
    
        class ViewTypeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
    
            public static final int ONE_ITEM = 1;
            public static final int TWO_ITEM = 2;
    
            @Override
            public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                LayoutInflater mInflater = LayoutInflater.from(ViewTypeActivity.this);
                RecyclerView.ViewHolder holder = null;
                if(ONE_ITEM == viewType){
                    View v = mInflater.inflate(R.layout.item_linear,parent,false);
                    holder = new OneViewHolder(v);
                }else{
                    View v = mInflater.inflate(R.layout.item_two,parent,false);
                    holder = new TwoViewHolder(v);
                }
                return holder;
            }
    
            @Override
            public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
                if(holder instanceof OneViewHolder){
                    ((OneViewHolder) holder).tv.setText(mDatas.get(position));
                }else {
                    ((TwoViewHolder) holder).tv1.setText(mDatas.get(position));
                    ((TwoViewHolder) holder).tv2.setText(mDatas.get(position));
                }
            }
    
            @Override
            public int getItemViewType(int position) {
                if(position % 3 == 0){
                    return TWO_ITEM;
                }else{
                    return ONE_ITEM;
                }
            }
    
            @Override
            public int getItemCount() {
                return mDatas.size();
            }
    
            class OneViewHolder extends RecyclerView.ViewHolder{
                TextView tv;
                public OneViewHolder(View itemView) {
                    super(itemView);
                    tv = (TextView) itemView.findViewById(R.id.adapter_linear_text);
                }
            }
    
            class TwoViewHolder extends RecyclerView.ViewHolder{
                TextView tv1,tv2;
                public TwoViewHolder(View itemView) {
                    super(itemView);
                    tv1 = (TextView) itemView.findViewById(R.id.adapter_two_1);
                    tv2 = (TextView) itemView.findViewById(R.id.adapter_two_2);
                }
            }
        }
    }
    

    两个XML

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal">
    
        <TextView
            android:id="@+id/adapter_two_1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_marginRight="5dp"
            android:background="#3FFF99"
            android:gravity="center" />
    
        <TextView
            android:id="@+id/adapter_two_2"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_marginLeft="5dp"
            android:layout_height="match_parent"
            android:background="#3FFF99"
            android:gravity="center" />
    </LinearLayout>
    
    <?xml version="1.0" encoding="utf-8"?>
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/adapter_linear_text"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#a9a8ff"
        android:gravity="center"/>
    

    间距的设定请看第一篇,效果图如下:

    这里写图片描述

    通过RecyclerView.Adapter设置多布局是非常方便的,而且大家可以发现当RecyclerView为LinearLayoutManager,我们可以通过viewType给RecyclerView来设置Header和Footer,如

    public static final int HEADER = 1;
            public static final int FOOTER = 2;
            public static final int ITEM = 3;
            @Override
            public int getItemViewType(int position) {
                if(position == 0){
                    return HEADER;
                }else if(position == getItemCount() -1){
                    return FOOTER;
                }else{
                    return ITEM;
                }
            }
    
            @Override
            public int getItemCount() {
                return mDatas.size() + 2;//Header和Footer
            }
    

    但是当布局管理器为GridLayoutManager和StaggeredGridLayoutManager时就要不能这样去做了。
    如何去加我会放在后面去写。

    相关文章

      网友评论

      • 155a80cb3970:66666666666666666666666,奶次啊,马飞
      • 闷声色狼:问一下。我里面加了3个布局,中间是一个线性布局里面有GridVIew,第三个布局就是最后一个布局盖住了,盖住了第二个布局底部一点位置,请问怎样修改??
      • 3e5f8438aaca:请问可以分享一下源码地址吗
      • 064b9db7d22c:楼主,我按照这上面来的,报空指针不知道咋回事????
        buder:博主的两个布局文件R.layout.item_two和item_linear写反了,报空指针,
      • 布谷鸟也会编程:使用gridmanager使用setSpanSizeLookup这个方法吧

      本文标题:RecyclerView的使用(三)多布局

      本文链接:https://www.haomeiwen.com/subject/ztqputtx.html