BaseRecyclerViewAdapterHelper之BaseMultiItemQuickAdapter实现自定义item类型

1. 一个实体类RectclerItem

public class RecyclerItem implements MultiItemEntity {

    private String title;
    private String content;
    private int item_type;

    public RecyclerItem(int item_type){
        this.item_type = item_type;
    }

    public RecyclerItem(String title, String content){
        this.title = title;
        this.content = content;
    }

    public RecyclerItem(String title, String content, int item_type){
        this.title = title;
        this.content = content;
        this.item_type = item_type;
    }

    public int getItem_type() {
        return item_type;
    }

    public void setItem_type(int item_type) {
        this.item_type = item_type;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    @Override
    public int getItemType() {
        return item_type;
    }
}

2. 三个item视图文件(view_multiltem_def_item、view_list_item和view_home_list_item)

  • view_multiltem_def_item.xml

xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content">

            android:id="@+id/multiltem_def_tv"
        style="@style/TextStyle_14sp_666"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:paddingLeft="10dp"
        android:gravity="center"
        android:text="这是默认的布局"/>


  • view_list_item.xml(同上)

  • view_home_list_item.xml

xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content">

            style="@style/TextStyle_14sp_666"
        android:id="@+id/content_text_tv"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center"
        android:text="这是一条内容" />


3. 适配器MultiItemQuickAdapter

public class MultiItemQuickAdapter extends BaseMultiItemQuickAdapter,BaseViewHolder> {

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public MultiItemQuickAdapter(List data) {
        super(data);
        addItemType(0, R.layout.view_multiltem_def_item);
        addItemType(1,R.layout.view_list_item);
        addItemType(2,R.layout.view_home_list_item);
    }

    @Override
    protected void convert(BaseViewHolder helper, RecyclerItem item) {
        switch (helper.getItemViewType()){
            case 0:
                helper.setText(R.id.multiltem_def_tv,"悯农---唐代诗人李绅");
                break;
            case 1:
                helper.setText(R.id.list_item_title_tv,item.getTitle())
                        .setText(R.id.list_item_content_tv,item.getContent());
                break;
            case 2:
                helper.setText(R.id.content_text_tv,item.getTitle() + item.getContent());
                break;
        }
    }
}


4. 实现类MultiItemQuickActivity

public class MultiItemQuickActivity extends BaseRecyclerViewActivity {

    private MultiItemQuickAdapter multiItemQuickAdapter;

    @Override
    protected int getLayoutResId() {
        return super.getLayoutResId();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        titleNameTv.setText("BaseMultiItemQuickAdapter实现自定义item");

        multiItemQuickAdapter = new MultiItemQuickAdapter(getListData(20));
        multiItemQuickAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_RIGHT);
        recyclerView.setAdapter(multiItemQuickAdapter);
    }

    @Override
    protected void onRefreshComplete() {
        multiItemQuickAdapter.addData(0,getListData(3));
        recyclerView.scrollToPosition(0);
    }
}

效果图如下:

BaseRecyclerViewAdapterHelper之BaseMultiItemQuickAdapter实现自定义item类型_第1张图片

你可能感兴趣的:(Android)