android_recyclerView万能适配器多种item的使用

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/iamchan/article/details/81237340

GitHub https://github.com/hongyangAndroid/baseAdapter

CSDN https://blog.csdn.net/lmj623565791/article/details/51118836

效果图

layout布局

com.scwang.smartrefresh.layout.SmartRefreshLayout//smart可以实现上拉加载下拉刷新
    android:id="@+id/smart"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView//recyclerview
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"></android.support.v7.widget.RecyclerView>
</com.scwang.smartrefresh.layout.SmartRefreshLayout>

item布局

item1

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"//多种item 写了两个item item1
    android:orientation="vertical" android:layout_width="match_parent"
    android:background="#e0e0e0"
    android:layout_height="40dp">
    <TextView
        android:text="aa"
        android:textColor="#fff"
        android:gravity="center_vertical"
        android:layout_weight="1"
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="0dp" />
    <View
        android:background="#e0e0e0"
        android:layout_width="match_parent"
        android:layout_height="1dp"></View>
</LinearLayout>

item2

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"//item2
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="140dp">
    <ImageView
        android:id="@+id/img"
        android:src="@mipmap/ic_launcher"
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="0dp" />
    <View
        android:background="#e0e0e0"
        android:layout_width="match_parent"
        android:layout_height="1dp"></View>
</LinearLayout>

activity

MultiItemTypeAdapter adapter;//多种item适配器
List<Object> list;//数据源
private SmartRefreshLayout smart;//分页用到 这里分页没写 参照之前文章的分页
private RecyclerView recyclerView;//recyclerview
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_third);

    recyclerView= (RecyclerView) findViewById(R.id.recyclerView);
    smart= (SmartRefreshLayout) findViewById(R.id.smart);

    list= new ArrayList<>();
    adapter = new MultiItemTypeAdapter(this,getListData());//有两个参数 上下文 数据源
    adapter.addItemViewDelegate(new OneDelegate());//往多种item适配器中放适配器1
    adapter.addItemViewDelegate(new TwoDelegate());//往多种item适配器中放适配器2
    recyclerView.setAdapter(adapter);
    adapter.notifyDataSetChanged();
    LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
    linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    recyclerView.setLayoutManager(linearLayoutManager);


}


public List<Object> getListData(){

    for(int i=0;i<10;i++){//数据源 注意是object List<Object> list
        list.add(new S("position"+i));
        for(int j=0;j<2;j++) {
            list.add(new I(R.mipmap.ic_launcher));
        }
    }
    return list;
}
public class OneDelegate implements ItemViewDelegate<Object> {//注意继承ItemViewDelegate object可填bean

    @Override
    public int getItemViewLayoutId() {//获取item布局
        return R.layout.item_string;
    }

    @Override
    public boolean isForViewType(Object item, int position) {
        return item instanceof S ;//可以直接返回true
    }

    @Override
    public void convert(ViewHolder holder, Object o, final int position) {
        holder.setText(R.id.text,((S)o).getText());//跟之前android_recyclerView万能适配器的使用那篇文章一样
        holder.getView(R.id.text).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(ThirdActivity.this, "text"+position, Toast.LENGTH_SHORT).show();
            }
        });
    }
}

public class TwoDelegate implements ItemViewDelegate {//同上

    @Override
    public int getItemViewLayoutId() {
        return R.layout.item_img;
    }

    @Override
    public boolean isForViewType(Object item, int position) {
        return item instanceof I;
    }

    @Override
    public void convert(ViewHolder holder, Object o, final int position) {

        holder.setImageResource(R.id.img,((I)o).getImg());
        holder.getView(R.id.img).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(ThirdActivity.this, "img"+position, Toast.LENGTH_SHORT).show();
            }
        });
    }
}


public class S{//bean1
    String text;

    public S(String text) {
        this.text = text;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }
}

public class I {//bean2
   int img;

    public I(int img) {
        this.img = img;
    }

    public int getImg() {
        return img;
    }

    public void setImg(int img) {
        this.img = img;
    }
}
展开阅读全文

没有更多推荐了,返回首页