Android recyclerView 多布局

1.首先引入我们的RecyclerView的包

2.在布局文件中使用他

<android.support.v7.widget.RecyclerView  
    android:id="@+id/myRecycler"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"/>  

设置他的多布局样式
item_a.xml布局

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#65edfc" android:layout_margin="5dp">  
    <TextView android:id="@+id/item_a_text1" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:textSize="30sp"/>  
    <TextView android:id="@+id/item_a_text2" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:background="#f5e593" android:textSize="30sp"/>  
</LinearLayout>  

item_b.xml布局

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#b0ed65" android:layout_margin="5dp">  
    <ImageView android:id="@+id/item_b_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/image"/>  
    <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">  
        <TextView android:id="@+id/item_b_text1" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center" android:textSize="30sp"/>  
        <TextView android:id="@+id/item_b_text2" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="center" android:textSize="30sp"/>  
    </LinearLayout>  

</LinearLayout>  

a.新建一个接口,我们的Bean类会实现他的一个返回条目类别的接口

public interface TypeInterf {  
    public int getType();  
}  

b.新建两个子类来实现我们的接口,这里我就以一个为例子了

public class Item_a implements TypeInterf {  
    String text1;  
    String text2;  

    public String getText1() {  
        return text1;  
    }  

    public void setText1(String text1) {  
        this.text1 = text1;  
    }  

    public String getText2() {  
        return text2;  
    }  

    public void setText2(String text2) {  
        this.text2 = text2;  
    }  

    @Override  
    public int getType() {  
        return R.layout.item_a;  
    }  
}  

可以看到我们的返回类型是他的布局文件,这样做会在他返回的时候逻辑比较清楚

c.将这两个子类的多个对象放入数据源中就好

(4)接收Context和数据源,其中数据源的泛型要是你的接口

private Context context;  
   private List<TypeInterf> list;  

   public MyRecyclerAdapter(Context context, List<TypeInterf> list) {  
       this.context = context;  
       this.list = list;  
   }  

(5)重写getItemViewType方法

@Override  
public int getItemViewType(int position) {  
    return list.get(position).getType();  
}  

这个方法的值会在onCreateViewHoldere中的到,然后我们根据不同的type来创建不同的ViewHolder

(6)根据不同的type实现不同的ViewHolder

@Override  
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {  
    RecyclerView.ViewHolder holder = null;  
    View view = LayoutInflater.from(context).inflate(viewType, parent, false);  
    if(viewType==R.layout.item_a){  
        holder = new ViewHolder_a(view);  
    }else{  
        holder = new ViewHolder_b(view);  
    }  
    return holder;  
}  

(7)绑定数据

@Override  
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {  
    if(holder instanceof ViewHolder_a){  
        ((ViewHolder_a) holder).textView1.setText(((Item_a)list.get(position)).getText1()); 
        ((ViewHolder_a) holder).textView2.setText(((Item_a)list.get(position)).getText2()); 
    }else{  
        ((ViewHolder_b) holder).textView1.setText(((Item_b)list.get(position)).getText1()); 
        ((ViewHolder_b) holder).textView2.setText(((Item_b)list.get(position)).getText2()); 
    }  
}  

(8)设置我们的数据源

private void initData() {  
    list = new ArrayList<>();  
    Item_a item_a = null;  
    Item_b item_b = null;  
    for (int i = 0; i < 100; i++) {  
        if (i%3==0) {  
            item_a = new Item_a();  
            item_a.setText1("ItemA的第"+i+"个text1");  
            item_a.setText2("ItemA的第"+i+"个text2");  
            list.add(item_a);  
        }else{  
            item_b = new Item_b();  
            item_b.setText1("ItemB的第"+i+"个text1");  
            item_b.setText2("ItemB的第"+i+"个text2");  
            list.add(item_b);  
        }  
    }  
}  
    原文作者:一朵红杏
    原文地址: https://blog.csdn.net/java_goodstudy/article/details/54996867
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞