关闭

RecyclerView的使用(三)多布局

标签: Recycler多布局header
675人阅读 评论(0) 收藏 举报
分类:

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时就要不能这样去做了。
如何去加我会放在后面去写。

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:14710次
    • 积分:278
    • 等级:
    • 排名:千里之外
    • 原创:13篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条