RecyclerView中的多种布局

概念

RecyclerView可以用于加载不同的视图类型,基于服务器的反馈,可能会要求加载不同的布局。我们需要在适配器中重写的方法:getItemViewType(),onCreatViewHolder(),onBindViewHolder()

实现

首先在上一节的基础上我们要替换掉基础的SimpleItemRecyclerViewAdapter 而是用ComplexRecyclerViewAdapter ,下面的例子将会包含两种布局Layout_viewHolder1.xml 用来存储User对象,而layout_viewHolder2.xml将会用来存储String对象。

  1. 在主活动中创建一个数据集合分别是文字和图像
 private ArrayList getSampleArrayList() {
      ArrayList 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中展示的项
    private List items;
    //为了区分加入标签
    private final int USER = 0 , IMAGE = 1;

    public ComplexRecyclerViewAdapter(List 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()));

你可能感兴趣的:(android学习)