RecyclerView多布局的实现

2019-03-01  本文已影响0人  Sakumi

近期在阅读《Android Programming : The Big Nerd Ranch Guide (3rd Edition)》,在介绍RecyclerView时,作者提出了一个challenge,利用getItemViewType(int)方法实现RecyclerView的多布局。



我在书本项目示例CriminalIntent上进行了代码修改,主要代码块如下:
在Crime模型中添加新的属性mRequiresPolice,并为它添加get、set方法

 private int mRequirePolice;
 public int getmRequirePolice() {
        return mRequirePolice;
    }

 public void setmRequirePolice(int mRequirePolice) {
        this.mRequirePolice = mRequirePolice;
    }

在CrimeLab的构造函数中,设置新增属性的值

private CrimeLab(Context context) {
        mCrimes = new ArrayList<>();
        for (int i = 0; i < 100; i++){
            Crime crime = new Crime();
            crime.setmTitle("Crime #" + i);
            crime.setmStatus(i % 2 == 0);
            crime.setmRequirePolice(i % 13 == 0 ? 1 : 0);
            mCrimes.add(crime);
        }
    }

增加一个新布局,命名为list_item_police

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="Call Police"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

增加新布局的ViewHolder

    private class PoliceHolder extends RecyclerView.ViewHolder{

        public PoliceHolder(LayoutInflater inflater, ViewGroup parent) {
            super(inflater.inflate(R.layout.list_item_police, parent, false));

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(getActivity(),"Calling police!", +Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

以下为关键代码,在adapter中重写getItemViewType方法,修改CrimeAdapter的代码

private class CrimeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
        private List<Crime> mCrimes;

        public CrimeAdapter(List<Crime> mCrimes) {
            this.mCrimes = mCrimes;
        }

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            RecyclerView.ViewHolder holder = null;
            LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
            switch (i){
                case 0:
                    holder = new CrimeHolder(layoutInflater, viewGroup);
                    break;
                case 1:
                    holder = new PoliceHolder(layoutInflater, viewGroup);
                    break;
            }
            return holder;
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
            if(mCrimes.get(i).getmRequirePolice() == 0){
                ((CrimeHolder) viewHolder).bind(mCrimes.get(i));
            }
        }

        @Override
        public int getItemCount() {
            return mCrimes.size();
        }

        @Override
        public int getItemViewType(int position) {
            return mCrimes.get(position).getmRequirePolice();
        }
    }

至此,RecyclerView的多布局就实现啦~

上一篇下一篇

猜你喜欢

热点阅读