1. 程式人生 > >RecyclerView中的多種佈局

RecyclerView中的多種佈局

概念

RecyclerView可以用於載入不同的檢視型別,基於伺服器的反饋,可能會要求載入不同的佈局。我們需要在介面卡中重寫的方法:getItemViewType(),onCreatViewHolder(),onBindViewHolder()

實現

首先在上一節的基礎上我們要替換掉基礎的SimpleItemRecyclerViewAdapter 而是用ComplexRecyclerViewAdapter ,下面的例子將會包含兩種佈局Layout_viewHolder1.xml 用來儲存User物件,而layout_viewHolder2.xml將會用來儲存String物件。

  1. 在主活動中建立一個數據集合分別是文字和影象
 private ArrayList<Object> getSampleArrayList() {
      ArrayList<Object> items = new ArrayList<>();
      items.add(new User("Dany Targaryen", "Valyria"));
      items.add(new User("Rob Stark", "Winterfell"));
      items.add("image");
      items.add(new User("Jon Snow", "Castle Black"
)); items.add("image"); items.add(new User("Tyrion Lanister", "King's Landing")); return items; }
  1. 配置兩種viewHolder用來展示資料
public class ViewHolder1 extends RecyclerView.ViewHolder {

    private TextView label1, label2;

    public ViewHolder1(View v) {
        super(v);
        label1 = (TextView) v.findViewById(R.id.text1);
        label2 = (TextView) v.findViewById(R.id.text2);
    }

    public
TextView getLabel1() { return label1; } public void setLabel1(TextView label1) { this.label1 = label1; } public TextView getLabel2() { return label2; } public void setLabel2(TextView label2) { this.label2 = label2; } }
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/llContainer"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="5dp">

    <TextView
        android:id="@+id/text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textAppearance="?android:attr/textAppearanceListItemSmall"
        android:gravity="center_vertical"/>

    <TextView
        android:id="@+id/text2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceListItemSmall"
        android:gravity="center_vertical" />

</LinearLayout>

3.建立複雜的介面卡ComplexRecyclerViewAdapter

public class ComplexRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    //在RecyclerView中展示的項
    private List<Object> items;
    //為了區分加入標籤
    private final int USER = 0 , IMAGE = 1;

    public ComplexRecyclerViewAdapter(List<Object> items) {
        this.items = items;
    }
    //獲得介面卡中的項的數量和型別

    @Override
    public int getItemCount() {
        return this.items.size();
    }
//獲得項的型別
    @Override
    public int getItemViewType(int position) {
        if (items.get(position) instanceof User) {
            return USER;
        } else if (items.get(position) instanceof String) {
            return IMAGE;
        }
        return -1;
    }
//將資料填充進viewHolder來展示
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (RecyclerView.ViewHolder.getItemViewType()) {
            case USER:
                ViewHolder1 vh1 = (ViewHolder1) holder;//轉型
                configureViewHolder1(vh1, position);
                break;
            case IMAGE:
                ViewHolder2 vh2 = (ViewHolder2) holder;
                configureViewHolder2(vh2);
                break;
            default:
                RecyclerViewSimpleTextViewHolder vh = (RecyclerViewSimpleTextViewHolder) holder;
                confgureDefaultViewHolder(vh, position);
                break;
        }

    }

    //基於項不同的型別來獲得不同的viewholder,

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder viewHolder;
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        switch (viewType) {
            case USER:
                View v1 =inflater.inflate(R.layout.layout_viewholder1, parent, false);
                viewHolder = new ViewHolder1(v1);
                break;
            case IMAGE:
                View v2 = inflater.inflate(R.layout.layout_viewholder2, parent, false);
                break;
            default:
                View v = inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
                viewHolder = new RecyclerViewSimpleTextViewHolder(v);
                break;
        }
        return viewHolder;
    }
    //配置viewHolder展示資料
    private void configureDefaultViewHolder(RecyclerViewSimpleTextViewHolder vh, int position) {
        vh.getlabel().setText((CharSequence) items.get(position));
    }
    private void configureViewHolder1(ViewHolder1 vh1, int position) {
        User user = (User) items.get(position);
        if (user != null) {
            vh1.getLabel1().setText("Name:" + user.name);
            vh1.getLabel2().setText("Hometown" + user.hometown);
        }
    }
    private void configureViewHolder2(ViewHolder2 vh2) {
        vh2.getImagView().setImageResource(R.mipmap.ic_launcher);
    }

}

最後在主活動中
recyclerView.setAdapter(new ComplexRecyclerViewAdapter(getSampleArrayList()));