Based on the multi-entry Adapter encapsulated by BaseRecyclerViewAdapterHelper, it is convenient, convenient, and easy to maintain,

Source: Internet
Author: User

Based on the multi-entry Adapter encapsulated by BaseRecyclerViewAdapterHelper, it is convenient, convenient, and easy to maintain,

I have already done a copy of toutiao.com. There are several la s in the News list:

Project Introduction

MultipleItemRvAdapterThe BaseQuickAdapter is encapsulated based on BaseRecyclerViewAdapterHelper.BaseRecyclerViewAdapterHelperBased on all functions, the multi-entry layout processing logic is encapsulated to separate each sub-entry in the adapter with the corresponding ItemProvider, so that the corresponding entries can be used for related business logic.

BaseRecyclerViewAdapterHelper

BaseRecyclerViewAdapterHelper is an encapsulated universal RecyclerView adapter that allows you to conveniently and quickly write the adapter. It contains one or more processing seeds and has many powerful functions, for example, you can add more stars, add animations, add sub-entries, and add long-pressed events. You can now add more stars than 12.6K, I have been favored by many Android Developers. I have also used BaseRecyclerViewAdapterHelper in project development. It is a loyal fan.

Limitations of BaseRecyclerViewAdapterHelper multi-entry layout

BaseRecyclerViewAdapterHelper stores the logic of multi-entry layout in the convert () method and processes corresponding sub-entries by judging the corresponding itemViewTyper. When there are many entries and complicated business logic, convert () processes a lot, which is not easy for future project maintenance, such as message lists in IM, including text, images, voice, location, red packets, and transfers, there are many different la s and many corresponding logics. When BaseRecyclerViewAdapterHelper is used only, the adapter code for the corresponding message list has broken through one thousand lines, the logic of all entries is in one adapter, which is not particularly reasonable. The following is the adapter of the message list:

Public class MessageListAdapter extends BaseQuickAdapter
  
   
{Public static final int TYPE_TEXT = 0; public static final int TYPE_IMG = 1; public MessageListAdapter (int layoutResId, @ Nullable List
   
    
Data) {super (layoutResId, data); setMultiTypeDelegate (new MultiTypeDelegate
    
     
() {@ Override protected int getItemType (Message message) {if (message instanceof TextMessage) {return TYPE_TEXT;} else if (message instanceof ImageMessage) {return TYPE_IMG ;} // else if () {// other message types, such as voice, location, red packet, transfer, etc. //} return 0 ;}); getMultiTypeDelegate (). registerItemType (TYPE_TEXT, R. layout. item_text_message ). registerItemType (TYPE_IMG, R. layout. item_image_message); // There are other Message types, such as voice, location, red packet, transfer, etc.} @ Override protected void convert (BaseViewHolder helper, Message item) {int viewType = helper. getItemViewType (); switch (viewType) {case TYPE_TEXT: // business logic break of text messages; case TYPE_IMG: // business logic break of image messages; // case speech, location, red packet, transfer, etc. // break ;}}}
    
   
  

When there are many sub-entries, there will be a lot of logic in convert (), which is not convenient for project maintenance. Therefore, the MultipleItemRvAdapter is encapsulated, the logical processing of each entry is handed over to the created ItemProvider for processing. In this way, the corresponding logic can be written in the ItemProvider of the corresponding entry for easy maintenance.

How to Use MultipleItemRvAdapter

1. Create the ItemProvider of the corresponding item, inherit the BaseItemProvider, and fill in the corresponding viewType value in the ItemProviderTag annotation (it is recommended to define a constant in the corresponding adapter, and the viewType of each item must be unique ), layout specifies the layout id of the corresponding entry.

For example, the provider of a text message entry:

/*** @ Author ChayChan * @ description provider * @ date 2018/3/21 */@ ItemProviderTag (viewType = MessageListAdapter. TYPE_TEXT, layout = R. layout. item_text_message) public class TextMessageItemProvider extends BaseItemProvider  {@ Override public void convert (BaseViewHolder helper, TextMessage data, int position) {// process related business logic helper. setText (R. id. TV _text, data. text) ;}@ Override public void onClick (BaseViewHolder helper, TextMessage data, int position) {// Click Event Toast. makeText (mContext, "Click:" + data. text, Toast. LENGTH_SHORT ). show () ;}@ Override public boolean onLongClick (BaseViewHolder helper, TextMessage data, int position) {// Toast. makeText (mContext, "longClick:" + data. text, Toast. LENGTH_SHORT ). show (); return true ;}} 

Provider of the image message entry:

/*** @ 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  {@ Override public void convert (BaseViewHolder helper, ImageMessage data, int position) {// process related business logic ImageView iv = helper. getView (R. id. iv_img); Glide. with (mContext ). load (data. imgUrl ). into (iv) ;}@ Override public void onClick (BaseViewHolder helper, ImageMessage data, int position) {// Click Event Toast. makeText (mContext, "Click:" + data. imgUrl, Toast. LENGTH_SHORT ). show () ;}@ Override public boolean onLongClick (BaseViewHolder helper, ImageMessage data, int position) {// Toast. makeText (mContext, "longClick:" + data. imgUrl, Toast. LENGTH_SHORT ). show (); return true ;}} 

The convert () method of ItemProvider is used to process the corresponding business logic, the Click Event in onClick () is used to process the entry, and the long-press event in onLongClick () is used to process the entry; although you can use ssetOnItemClick () and setOnItemLongClick () to process the click and long-press event adapter, there are still two events that are handed over to itemProvider, developers can choose one of the methods as needed.

2. Create the corresponding adapter and inherit the MultiItemRvAdapter, such as MessageListAdapter:

/*** @ Author ChayChan * @ description: The adapter of the message list * @ date 2018/3/21 */public class MessageListAdapter extends MultipleItemRvAdapter  {Public static final int TYPE_TEXT = 0; public static final int TYPE_IMG = 1; public MessageListAdapter (@ Nullable List  Data) {super (data); // The constructor can assign values to the global variables getViewType () and registerItemProvider () before calling finishInitialize () you can obtain the passed value // getViewType (). It may be because of some business logic. You need to pass a value for judgment and return the corresponding viewType // registerItemProvider () you can pass the value to ItemProvider finishInitialize (); // call this method to inform MultipleItemRvAdapter1 that the parameters of the constructor have been passed through initialization} @ 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 () {// register the related entry provider mProviderDelegate. registerProvider (new TextMessageItemProvider (); // register the itemProvider mProviderDelegate of the text message entry. registerProvider (new ImageMessageItemProvider (); // register itemProvider for an image message entry }}  

The getViewType () and registerItemProvider () methods must be implemented in the adapter. GetViewType () returns different viewtypes by judging the relevant logic. registerItemProvider () is used to register the ItemProvider of all the sub-entries defined by the user.

3. Set the adapter for RecyclerView:

MRvMessages = findViewById (R. id. rv_message); mRvMessages. setHasFixedSize (true); mRvMessages. setLayoutManager (new LinearLayoutManager (this); String imgUrl1 = "https://timgsa.baidu.com/timgimage&quality=80&size=b9999_10000&sec=1521627479112&di=7b109af49f8c1c193c3173306cf58680&imgtype=0&src=http%3A%2F%2Fimg.xgo-img.com.cn%2Fpics%2F1538%2F1537620.jpg"; String imgUrl2 = "https://timgsa.baidu.com/timgim Age & quality = 80 & size = b0000_10000 & sec = 1521617426 & di = d6537bb0ef71984e4a1d14e4b18ba169 & src = https://www.2cto.com/uploadfile/2018/0323/20180323091743356.jpg "; mMessages. add (new TextMessage ("Hello, hahaha, "); mMessages. add (new ImageMessage (imgUrl1); mMessages. add (new TextMessage ("well, today's weather is really good! "); MMessages. add (new ImageMessage (imgUrl2); mRvMessages. setAdapter (new MessageListAdapter (mMessages ));

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

Import Method

Add the jitpack repository address to allprojects {} In build. gradle under the project root directory, as shown below:

Allprojects {repositories {jcenter () maven {url 'https: // jitpack. io '} // Add a jitpack repository address }}

Open build. gradle in the module of the app, and add dependencies in dependencies {}, as shown below:

Dependencies {compile 'com. github. chaychan: MultipleItemRvAdapter: 1.0.2' // The latest version is recommended}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.