CymChad / CymChad/BaseRecyclerViewAdapterHelper

4.0版本 MutiItemAdapter中添加Holder的方式是否可以更简练些,下面是我的一些尝试

Open
#3,772 9 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Kotlin
Stars
24.6k
Forks
5.2k
PR merge metrics
No merged PRs in 30d

Description

其实在MultiItemAdapter添加各种addItemType会导致代码冗余的很,适当的抽离和使用反射会使代码简介很多很多,比如我在项目中的尝试:

class DetailsAdapter : BaseMultiItemAdapter<Floors>() {
    init {
        // 根据type添加返回type 滑动时会根据对应额Position去创建对应的Holder
        onItemViewType { position, list ->
            list[position].itemType
        }

        
        addDetailsVH(T_BRAND_HEADER.hashCode(), GDBrandHeaderVH::class.java, R.layout.item_vh_details_brand_header)
        addDetailsVH(T_TOP_BANNER.hashCode(), GDTopBannerVH::class.java, R.layout.item_vh_details_top_banner) 
        addDetailsVH(T_PRESALE_PRICE.hashCode(), GDPresalePricesVH::class.java,R.layout.item_vh_details_presale_prices)
        addDetailsVH(T_ACTIVITY_PRICE.hashCode(), GDActivityPricesVH::class.java, R.layout.item_vh_activity_prices)
       ......
    }


   /**
     * 通过传递的过来的Class类型,创建出对应的ViewHolder
     */
    private fun <E : DetailsEntity, T : BaseGDVH<E>> addDetailsVH(
        itemType: Int, // holder类型
        vh: Class<T>,  // holder类名
        @LayoutRes layoutId: Int // holder布局
    ) {
        addItemType(itemType, object : OnMultiItemAdapterListener<DetailsEntity?, T> {
            override fun onCreate(context: Context, parent: ViewGroup, viewType: Int): T {
                // 获取到默认的构造方法
                val constructors = vh.declaredConstructors[0]
                // 获取到绑定的view
                val view = LayoutInflater.from(context).inflate(layoutId, parent, false)
                return constructors.newInstance(view) as T
            }

            override fun onBind(holder: T, position: Int, item: DetailsEntity?) {
                if (item == null) {
                    holder.onHiddenVh()
                    return
                }

                holder.onBindData(item as E)
            }

            // 商详的holder都是跨满屏幕
            override fun isFullSpanItem(itemType: Int): Boolean {
                return true
            }
        })
    }
}

/**
* 抽离出来的一个基类,限定绑定类型,方便以后扩展
*/
abstract class BaseGDVH<T : DetailsEntity>(view: View) : RecyclerView.ViewHolder(view) {
    abstract fun onBindData(data: T)

    // 当类型解析失败(不匹配),或者是数据为空时,需要将当前的Holder直接隐藏掉
    fun onHiddenVh() {
        itemView.layoutParams.apply {
            width = 0
            height = 0
        }
        itemView.visibility = View.GONE
    }
}

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing MultiItemAdapter's addItemType API and the DetailsAdapter example, including addDetailsVH and BaseGDVH. Define the desired registration approach while preserving holder creation, binding, null handling, and full-span behavior; the issue is done when the maintainers agree on and document the resulting API.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, kotlin
Domain
mobile
Issue type
Refactor
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.