-
Notifications
You must be signed in to change notification settings - Fork 35
Home
Step1: Create an ItemProvider for the corresponding entry, inherit the BaseItemProvider, fill in the value of the corresponding viewType in the ItemProviderTag (it is recommended to define the constant in the corresponding adapter, the viewType of each entry must be unique), layout specifies the layout id of the corresponding entry.
For example, a provider of text entries:
/**
* https://github.com/chaychan
* @author ChayChan
* @description: Text ItemProvider
* @date 2018/3/30 11:39
*/
@ItemProviderTag(
viewType = DemoMultipleItemRvAdapter.TYPE_TEXT,
layout = R.layout.item_text_view
)
public class TextItemProvider extends BaseItemProvider<NormalMultipleEntity> {
@Override
public void convert(BaseViewHolder helper, NormalMultipleEntity data, int position) {
helper.setText(R.id.tv, data.content);
}
@Override
public void onClick(BaseViewHolder helper, NormalMultipleEntity data, int position) {
Toast.makeText(mContext, "click", Toast.LENGTH_SHORT).show();
}
@Override
public boolean onLongClick(BaseViewHolder helper, NormalMultipleEntity data, int position) {
Toast.makeText(mContext, "longClick", Toast.LENGTH_SHORT).show();
return true;
}
}
A provider of image entries:
/**
* https://github.com/chaychan
*
* @author ChayChan
* @description: Img ItemProvider
* @date 2018/3/30 11:39
*/
@ItemProviderTag(
viewType = DemoMultipleItemRvAdapter.TYPE_IMG,
layout = R.layout.item_image_view
)
public class ImgItemProvider extends BaseItemProvider<NormalMultipleEntity> {
@Override
public void convert(BaseViewHolder helper, NormalMultipleEntity data, int position) {
if (position % 2 == 0) {
helper.setImageResource(R.id.iv, R.mipmap.animation_img1);
}else{
helper.setImageResource(R.id.iv, R.mipmap.animation_img2);
}
}
@Override
public void onClick(BaseViewHolder helper, NormalMultipleEntity data, int position) {
Toast.makeText(mContext, "click", Toast.LENGTH_SHORT).show();
}
@Override
public boolean onLongClick(BaseViewHolder helper, NormalMultipleEntity data, int position) {
Toast.makeText(mContext, "longClick", Toast.LENGTH_SHORT).show();
return true;
}
}
A provider of text and image entries:
/**
* https://github.com/chaychan
* @author ChayChan
* @description: Text Img ItemProvider
* @date 2018/3/30 11:39
*/
@ItemProviderTag(
viewType = DemoMultipleItemRvAdapter.TYPE_TEXT_IMG,
layout = R.layout.item_img_text_view
)
public class TextImgItemProvider extends BaseItemProvider<NormalMultipleEntity> {
@Override
public void convert(BaseViewHolder helper, NormalMultipleEntity data, int position) {
helper.setText(R.id.tv, data.content);
if (position % 2 == 0) {
helper.setImageResource(R.id.iv, R.mipmap.animation_img1);
}else{
helper.setImageResource(R.id.iv, R.mipmap.animation_img2);
}
}
@Override
public void onClick(BaseViewHolder helper, NormalMultipleEntity data, int position) {
Toast.makeText(mContext, "click", Toast.LENGTH_SHORT).show();
}
@Override
public boolean onLongClick(BaseViewHolder helper, NormalMultipleEntity data, int position) {
Toast.makeText(mContext, "longClick", Toast.LENGTH_SHORT).show();
return true;
}
}
ItemProvider's convert() method is used to handle the corresponding business logic, onClick() is used to process the item's click event, and onLongClick() is used to hold the item's long press event; although click and long press event adapters Can be handled by setOnItemClickListener () and setOnItemLongClickListener (), but here are still the two events in the itemProvider, developers can choose to use one of the ways according to specific needs.
**Step2:**Create a corresponding adapter, inheriting MultipleItemRvAdapter:
/**
* https://github.com/chaychan
* @author ChayChan
* @description: MultipleItemRvAdapter demo
* @date 2018/3/30 11:28
*/
public class DemoMultipleItemRvAdapter extends MultipleItemRvAdapter<NormalMultipleEntity> {
public static final int TYPE_TEXT = 100;
public static final int TYPE_IMG = 200;
public static final int TYPE_TEXT_IMG = 300;
public DemoMultipleItemRvAdapter(@Nullable List<NormalMultipleEntity> data) {
super(data);
//If the constructor has other parameters, it needs to be assigned before calling finishInitialize() and assigned to the global variable
// This getViewType () and registerItemProvider () method can get the value passed over
// getViewType () may be due to some business logic, you need to pass a value to judge, return the corresponding viewType
//RegisterItemProvider() can pass value to ItemProvider
finishInitialize();
}
@Override
protected int getViewType(NormalMultipleEntity entity) {
//According to the entity class to determine and return the corresponding viewType,
//the specific judgment logic is different because of the business, here is simply by judging the type attribute
if (entity.type == NormalMultipleEntity.SINGLE_TEXT){
return TYPE_TEXT;
}else if (entity.type == NormalMultipleEntity.SINGLE_IMG){
return TYPE_IMG;
}else if (entity.type == NormalMultipleEntity.TEXT_IMG){
return TYPE_TEXT_IMG;
}
return 0;
}
@Override
public void registerItemProvider() {
//Register related entries provider
mProviderDelegate.registerProvider(new TextItemProvider());
mProviderDelegate.registerProvider(new ImgItemProvider());
mProviderDelegate.registerProvider(new TextImgItemProvider());
}
}
Adapter need to implement getViewType () and registerItemProvider () two methods. getViewType() returns a different viewType by judging related logic, and registerItemProvider() is used to register ItemProviders for all sub-items defined by itself.
**Step3:**Set the adapter for RecyclerView:
RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.rv_list);
GridLayoutManager manager = new GridLayoutManager(this, 4);
mRecyclerView.setLayoutManager(manager);
mData = DataServer.getNormalMultipleEntities();
DemoMultipleItemRvAdapter multipleItemAdapter = new DemoMultipleItemRvAdapter(mData);
multipleItemAdapter.setSpanSizeLookup(new BaseQuickAdapter.SpanSizeLookup() {
@Override
public int getSpanSize(GridLayoutManager gridLayoutManager, int position) {
int type = mData.get(position).type;
if (type == NormalMultipleEntity.SINGLE_TEXT){
return MultipleItem.TEXT_SPAN_SIZE;
}else if (type == NormalMultipleEntity.SINGLE_IMG){
return MultipleItem.IMG_SPAN_SIZE;
}else{
return MultipleItem.IMG_TEXT_SPAN_SIZE;
}
}
});
mRecyclerView.setAdapter(multipleItemAdapter);
Completed!