Project address: github.com/chaychan/Mu…

This is a copy of toutiao I made before. There are several layouts in the news list:

Apk download address

[Click to download experience]

Project introduction

MultipleItemRvAdapter is on the basis of BaseRecyclerViewAdapterHelper BaseQuickAdapter for encapsulation, On the basis of containing all the functions of the BaseRecyclerViewAdapterHelper, many of them entry layout processing logic encapsulation, every seed will in the adapter entry separate out corresponding ItemProvider, so convenient corresponding entries do related business logic.

BaseRecyclerViewAdapterHelper

BaseRecyclerViewAdapterHelper is a packaging good universal RecyclerView adapter, can be convenient and quick to complete the writing of the adapter, entry contains one or more seeds processing and has many powerful features, Such as tensile load more, support to add animation, can add child entries click, long press event, add the head and the bottom, the number of star now has more than 12.6 K, get the favour of many Android developers, I also use BaseRecyclerViewAdapterHelper in the development of the project, Is a loyal fan.

BaseRecyclerViewAdapterHelper entry layout more disadvantages

BaseRecyclerViewAdapterHelper layout of multiple items in the convert of logic () method, accordingly by judging the corresponding itemViewTyper subitem processing, when entry is much more special and complex business logic, Convert () in the treatment will be much more special, back is not convenient to maintenance of the project, such as IM inside the list of messages, text, images, voice, location, a red envelope, transfer, etc., there are many kinds of different layout, the corresponding logic also is more, in the case of using only BaseRecyclerViewAdapterHelper, The adapter code of the corresponding message list has broken a thousand lines. The logic of all entries is in the same Adapter, which is not particularly reasonable. Here is the Adapter of the message list:

public class MessageListAdapter extends BaseQuickAdapter<Message,BaseViewHolder>{ public static final int TYPE_TEXT = 0;  public static final int TYPE_IMG = 1; public MessageListAdapter(int layoutResId, @Nullable List<Message> data) { super(layoutResId, data); setMultiTypeDelegate(new MultiTypeDelegate<Message>() { @Override protected int getItemType(Message message) { if (message instanceof TextMessage){ return TYPE_TEXT; }else if (message instanceof ImageMessage){ return TYPE_IMG; } // else if(){// // has other message types, such as voice, location, red envelope, transfer, etc. }}); getMultiTypeDelegate() .registerItemType(TYPE_TEXT, R.layout.item_text_message) .registerItemType(TYPE_IMG, R.layout.item_image_message); // There are other message types, Override protected void convert(BaseViewHolder helper, Message item) { int viewType = helper.getItemViewType(); Switch (viewType){case TYPE_TEXT: // Business logic break for text messages; Case TYPE_IMG: // Business logic break; // case voice, position, red envelope, transfer etc. // break; }}}Copy the code

When there are many types of child items, the logic in convert() is too large to be maintained by the project. Therefore, the MultipleItemRvAdapter is encapsulated, and the logical processing of each item is handled by the created ItemProvider. In this way, the corresponding logic can be written in the ItemProvider of the corresponding item for easy maintenance.

How to use MultipleItemRvAdapter

Create an ItemProvider for the corresponding item, inherit BaseItemProvider, annotate the ItemProviderTag with the corresponding viewType value (it is recommended to define constants in the corresponding Adapter, The viewType of each entry must be unique), and layout specifies the layout ID of the corresponding entry.

For example, providers for text message entries:

/** * @author ChayChan * @description Text message entry provider * @date 2018/3/21 14:43 */ @ItemProviderTag(viewType = MessageListAdapter.TYPE_TEXT, layout = R.layout.item_text_message ) public class TextMessageItemProvider extends BaseItemProvider<TextMessage> { @Override public void convert(BaseViewHolder helper, TextMessage data, Int position) {// Handle the related business logic helper.settext (r.i.D.tw_text, data.text); } @Override public void onClick(BaseViewHolder helper, TextMessage data, Int position) {// Click toast.maketext (mContext, "Click: "+ data.text, toast.length_short).show(); } @Override public boolean onLongClick(BaseViewHolder helper, TextMessage data, Int position) {// long press toast.maketext (mContext, "longClick: "+ data.text, toast.length_short).show(); return true; }}Copy the code

Provider of image message entries:

/** * @author ChayChan * @description provider * @date 2018/3/21 14:43 */ @ItemProviderTag(viewType = MessageListAdapter.TYPE_IMG, layout = R.layout.item_image_message ) public class ImageMessageItemProvider extends BaseItemProvider<ImageMessage> { @Override public void convert(BaseViewHolder helper, ImageMessage data, Int position) {ImageView iv = helper.getView(r.i.iv_img); Glide.with(mContext).load(data.imgUrl).into(iv); } @Override public void onClick(BaseViewHolder helper, ImageMessage data, Int position) {// Click the toast.maketext (mContext, "Click: "+ data.imgurl, toast.length_short).show(); } @Override public boolean onLongClick(BaseViewHolder helper, ImageMessage data, Int position) {// Long press toast.maketext (mContext, "longClick: "+ data.imgurl, toast.length_short).show(); return true; }}Copy the code

The convert() method of ItemProvider is used to handle the corresponding business logic, onClick() is used to handle the click event of the item, and onLongClick() is used to handle the long press event of the item. Although the click and long press events are handled by ssetOnItemClick() and setOnItemLongClick() in the Adapter, they are handed over to the itemProvider and the developer can choose to use one or the other depending on their needs.

Create a MultiItemRvAdapter, such as MessageListAdapter.

/** * @author ChayChan * @description: Adapter * @date 2018/3/21 14:40 */ public class MessageListAdapter extends MultipleItemRvAdapter<Message> {public static final int TYPE_TEXT = 0; public static final int TYPE_IMG = 1; public MessageListAdapter(@Nullable List<Message> data) { super(data); // Constructors that pass parameters can be assigned before finishInitialize() is called. //getViewType() and registerItemProvider() can get the passed value. //getViewType() may need to pass a value for some business logic, Return the corresponding viewType //registerItemProvider() where the value can be passed to ItemProvider finishInitialize(); } @override protected int getViewType(Message Message) {// Return the corresponding viewType if (message instanceof TextMessage) { return TYPE_TEXT; } else if (message instanceof ImageMessage) { return TYPE_IMG; } return 0; } @ Override public void registerItemProvider () {/ / registration related item provider mProviderDelegate. RegisterProvider (new TextMessageItemProvider()); . / / registered text message entry itemProvider mProviderDelegate registerProvider (new ImageMessageItemProvider ()); // Register the image message entry itemProvider}}Copy the code

The Adapter needs to implement getViewType() and registerItemProvider() methods. GetViewType () by judging relevant logic return different viewType, registerItemProvider () used to register themselves as defined by the ItemProvider of all child items.

Three, set up the corresponding adapter for RecyclerView:

mRvMessages = findViewById(R.id.rv_message); mRvMessages.setHasFixedSize(true); mRvMessages.setLayoutManager(new LinearLayoutManager(this)); String imgUrl1 = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1521627479112&di=7b109af49f8c1c193c3173306cf58680&i mgtype=0&src=http%3A%2F%2Fimg.xgo-img.com.cn%2Fpics%2F1538%2F1537620.jpg"; String imgUrl2 = "https://timgsa.baidu.com/timg?image&quality=80&size=b10000_10000&sec=1521617426&di=d6537bb0ef71984e4a1d14e4b18ba169&src =http://img1d.xgo-img.com.cn/pics/789/788784.jpg"; Mmessages. add(new TextMessage(" Hello, hahahahahahahahaha, whoo-la-la-la ")); mMessages.add(new ImageMessage(imgUrl1)); Mmessages. add(new TextMessage(" MMMM, what a nice day!" )); mMessages.add(new ImageMessage(imgUrl2)); mRvMessages.setAdapter(new MessageListAdapter(mMessages));Copy the code

Effect:

Because the library contains BaseRecyclerViewAdapterHelper library, so it also has BaseRecyclerViewAdapterHelper have.

Import the way

Add the jitPack repository address to allProjects {} in build.gradle at the root of the project as follows:

Maven {url 'https://jitpack.io'}}Copy the code

Add dependencies to build. Gradle in the module of your app. Add dependencies to build.

Dependencies {the compile 'com. Making. Chaychan: MultipleItemRvAdapter: 1.0.2' / / it is recommended to use the latest version}Copy the code

You can view the latest release

Github.com/chaychan/Mu…

Support and encouragement

If you think it is helpful, please help star so that more people can see it. Thank you! In this way, I will be more motivated to improve the project;