QuickAdapter uses Explanation-Quick Construction of Recycler View Adapter

Posted by dagnasty on Wed, 15 May 2019 23:07:56 +0200

What is QuickAdapter? As its name implies, it's a quick way to build RecyclerViewAdapter.

What are the characteristics of this component?

Quickly build Adapter, integrate Quick kView Holder, quickly bind data, configure itemView, margin, padding flexibly, set OnItemClickListener, OnItemLong Lister, and can set View Click Event OnClickListener in ItemView separately, expand flexibly, code compatible.

Next let's look at the normal way of writing:

class Adapter2 : RecyclerView.Adapter<Adapter2.ViewHolder>() {
        private val dataList = mutableListOf<String>()
        private var onClickListener: ((position: Int, holder: ViewHolder) -> Unit?)? = null

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
            return ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.dialog_test, null))
        }

        override fun getItemCount(): Int = dataList.size

        override fun onBindViewHolder(holder: ViewHolder, position: Int) {
            holder.titleTv.text = dataList[position]
            /*Setting Click Events*/
            holder.itemView.setOnClickListener {
                if (onClickListener != null) onClickListener?.invoke(position, holder)
            }
        }

        fun setOnItemClickListener(onClickListener: (position: Int, holder: ViewHolder) -> Unit?) {
            this.onClickListener = onClickListener
        }

        inner class ViewHolder : RecyclerView.ViewHolder {
            lateinit var titleTv: TextView
            lateinit var userNameTv: TextView
            lateinit var ageTv: TextView
            lateinit var contentTv: TextView
            lateinit var coverIv: ImageView
            ...

            constructor(itemView: View) : super(itemView) {
                titleTv = itemView.findViewById(R.id.titleTv)
                userNameTv
                ageTv
                contentTv
                coverIv
                ...
            }
        }

    }

Simple lists, standard writing is probably the case.

Next, look at Quick Adapter, and you'll be surprised.

class Adapter : QuickAdapter<Int, QuickViewHolder>() {
        override fun onBindData(holder: QuickViewHolder, position: Int, itemData: Int,viewType: Int) {
            holder.setImgCircle(R.id.iv, itemData)
        }

        override fun onResultLayoutResId(viewType: Int): Int = R.layout.item_rv_list
    }

It only needs to rewrite the two necessary methods, so that an Adapter can be completed. The amount of code is very small, which saves effort and time.

Next, let's assume several usage scenarios

If the list is CardView, you need to set margin to match it.

        override fun onResultItemMargin(): Float {
            return super.onResultItemMargin()
        }

Just rewrite this method to set up margin s up and down. Like this

If there is a requirement to display multiple columns, they need to have intervals before, then they need to set up margin separately, otherwise the display effect will be crowded together.

        override fun onResultItemMarginLeft(position: Int): Float {
            return super.onResultItemMarginLeft(position)
        }

        override fun onResultItemMarginRight(position: Int): Float {
            return super.onResultItemMarginRight(position)
        }

        override fun onResultItemMarginTop(position: Int): Float {
            return super.onResultItemMarginTop(position)
        }

        override fun onResultItemMarginBottom(position: Int): Float {
            return super.onResultItemMarginBottom(position)
        }

Like this

Now that we're all here, let's expand the list to show a variety of Layout s. What should we do about that? Code directly

class Adapter : QuickAdapter<Int, QuickViewHolder>() {

        override fun onBindData(holder: QuickViewHolder, position: Int, itemData: Int, viewType: Int) {
            when (viewType) {
                1 -> {//Binding book data

                }
                2 -> {//Binding Author's Data

                }
            }
        }

        override fun getItemViewType(position: Int): Int = when (getItem(position)) {
            123 -> {//Exhibition book
                1
            }
            456 -> {//Show author
                2
            }
            else -> 1
        }
        

        override fun onResultLayoutResId(viewType: Int): Int = when (viewType) {
            1 -> {//Layout of Binding Book
                R.layout.app_toast
            }
            2 -> {//Layout Binding Authors
                R.layout.dialog_test
            }
            else -> {//Layout of Binding Book
                R.layout.app_toast
            }
        }
    }

We retrieve the data in the model by rewriting getItemViewType, determine the current type of Layout, then return the corresponding Layout in onResultLayoutResId, and finally bind the data according to the type in onBindData. Complete this complex usage scenario in three steps

The next step is to customize and change to what you like.

Just inherit QuickAdapter and rewrite OnResultViewHolder

abstract class BaseAdapter<M> : QuickAdapter<M, BaseViewHolder>() {
    override fun onResultViewHolder(itemView: View): BaseViewHolder = BaseViewHolder(itemView)
}

In this way, you can save one generic type and replace ViewHolder with your own custom

How to set up the monitor? Code directly

1. Setting Click Events for the Whole item

//Set the click event for the entire item
adapter.setOnItemClickListener { view, viewHolder, position, itemData ->

}

2. Setting long press events for the entire item

//Setting the long press event for the entire item
adapter.setOnItemLongClickListener { view, viewHolder, position, itemData ->

    true
}

3. Setting View Click Events in item

//Setting View Click Events in item
adapter.setOnClickListener({ view, viewHolder, position, itemData ->

}, R.id.checkBox1, R.id.checkBox2, R.id.checkBox3)

4. Setting View Selection Events in item

//Setting View Selection Events in item
adapter.setOnCheckedChangedListener({ view, viewHolder, isChecked, position, itemData ->
            
}, R.id.btn1, R.id.btn2, R.id.btn3)

What about setting up data?

1. Initialization data

adapter.setDataList(dataList)

2. Adding individual data

adapter.add(item)

3. Adding Data List

adapter.addDataList(dataList)

4. Remove data

adapter.remove(1)
adapter.removeAll()

Simple use is not very simple and convenient, and complex use is more flexible. The amount of code saved a lot, save worry.

end---------

Recommended use of gradle access

implementation 'org.quick.android:quick-component:latest.release'

Source address: https://github.com/SpringSmell/quick-component

Okay, that's the end. Good stuff, of course, needs to be shared. Tell your buddy right away that I found a cool thing..

Quick Android family bucket

Topics: Android Gradle github