Android中RecyclerView Adapter的骚操作

SimpleRecycler

GitHub传送门:SimpleRecycle on Github
Demo apk下载:apk dowload

step:

步骤1.将JitPack存储库添加到构建文件中

allprojects {
    repositories {
        maven { url 'https://jitpack.io' }
    }
}

步骤2.添加依赖项

dependencies {
    implementation 'com.github.xiaoqinghong:SimpleRecycler:last-version'
}

SimpleAdapter(只支持一种itemView)

public class TestAdapter extends SimpleRecyclerAdapter {

    public TestAdapter(List list) {
        super(R.layout.item_test, list);
    }

    @Override
    protected void bind(SimpleViewHolder helper, String item) {
        helper.setText(R.id.tv_test_item, item)
                .addClickListener(R.id.tv_test_item) // 设置子项的点击事件监听
                .addLongClickListener(R.id.tv_test_item) // 设置子项的长按事件监听
                .getView(R.id.tv_test_item);
    }
}

MultipleAdapter(支持多种itemView)

/**
* bean类需要实现SimpleMultipleAdapter.TypeBind接口。每个bean类都可能属于不同的type
*/
public class MultipleBean implements SimpleMultipleAdapter.TypeBind {
    @Override
    public int type() {
        return 0;
    }
}
/**
* MultipleTestAdapter
*/
public class MultipleTestAdapter extends SimpleMultipleAdapter {
    public MultipleTestAdapter(List list) {
        super(list);
        // 添加多种item对应的layout。
        addLayout(R.layout.multiple_item_a, 0);
        addLayout(R.layout.multiple_item_b, 1);
        addLayout(R.layout.multiple_item_c, 2);
    }

    @Override
    protected void multipleBind(SimpleViewHolder helper, MultipleBean item, int itemType) {
        switch (itemType) {
            case 0:
                // do something...
                break;
            case 1:
                 // do something...
                break;
            case 2:
                 // do something...
                break;
            default:
                 // do something...
                break;
        }
    }
}

adapter暴露的接口

mAdapter.bindRecyclerView(recyclerView); // 内部默认使用LinearLayoutManager
mAdapter.addHeaderView(header);
mAdapter.addFooterView(footer);
// click
mAdapter.setOnItemClickListener(new SimpleRecyclerAdapter.OnItemClickListener() {
    @Override
    public void onClick(View v, int position) {
        
    }
});

mAdapter.setOnSubViewClickListener(new SimpleRecyclerAdapter.OnSubViewClickListener() {
    @Override
    public void onClick(View v, int position) {
        
    }
});

// long click
mAdapter.setOnItemLongClickListener(new SimpleRecyclerAdapter.OnItemLongClickListener() {
    @Override
    public void onLongClick(View v, int position) {

    }
});

mAdapter.setOnSubViewLongClickListener(new SimpleRecyclerAdapter.OnSubViewLongClickListener() {
    @Override
    public void onLongClick(View v, int position) {

    }
});

最后
方法不止一种,这样地操作方式,仅仅这只是我的一种思考。

你可能感兴趣的:(Android中RecyclerView Adapter的骚操作)