当前位置:   article > 正文

RecyclerView中的多种布局_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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  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;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
<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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

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);
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82

最后在主活动中
recyclerView.setAdapter(new ComplexRecyclerViewAdapter(getSampleArrayList()));

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/174803
推荐阅读
相关标签
  

闽ICP备14008679号