CymChad / CymChad/BaseRecyclerViewAdapterHelper

关于DatabindingAdatper统一基类的进一步封装

Open
#3,806 1 comment 3 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

基类封装
abstract class BaseDataBindingAdapter<T, DB : ViewDataBinding> :
    BaseQuickAdapter<T, DataBindingHolder<DB>>() {

    override fun onCreateViewHolder(
        context: Context,
        parent: ViewGroup,
        viewType: Int
    ): DataBindingHolder<DB> {
        val clazz = this.getClazz<Class<DB>>(1)
        val mBinding = clazz.getDeclaredMethod(
            "inflate",
            LayoutInflater::class.java,
            ViewGroup::class.java,
            Boolean::class.java
        ).invoke(null, LayoutInflater.from(parent.context), parent, false) as DB
        return DataBindingHolder(mBinding)
    }

    override fun onBindViewHolder(holder: DataBindingHolder<DB>, position: Int, item: T?) {
        item?.let {
            holder.binding.run {
                bindingParams(item)
                executePendingBindings()
            }
        }
    }

    protected open fun DB.bindingParams(item:T?) {}

}
/**
 * 获取当前类绑定的泛型clazz
 * @param index 泛型所处index
 */
fun <T> Any.getClazz(index: Int = 0): T {
    return (javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[index] as T
}
子类使用
public class DataBindingAdapter extends BaseDataBindingAdapter<Movie, ItemMovieBinding> {

    private final MoviePresenter mPresenter = new MoviePresenter();

    @Override
    protected void bindingParams(ItemMovieBinding binding, @Nullable Movie item) {
        binding.setMovie(item);
        binding.setPresenter(mPresenter);
    }

}

由于item是必须的,所以我们完全也可以将其规定命名后放置在基类

    override fun onBindViewHolder(holder: DataBindingHolder<DB>, position: Int, item: T?) {
        item?.let {
            holder.binding.run {
                // 规定名称
                setVariable(BR.item,item)
                bindingParams()
                executePendingBindings()
            }
        }
    }

    protected open fun DB.bindingParams() {}

基类再次精简

    /**
     * 如果没有其他业务需求,这个方法都可以不重写
     * @param binding
     */
    @Override
    protected void bindingParams(ItemMovieBinding binding) {
        binding.setPresenter(mPresenter);
    }

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 locating the existing BaseDataBindingAdapter and DataBindingHolder entry points, then review how generic binding types and onBindViewHolder are currently handled. Compare the proposed generic base-class API and bindingParams hook with current adapter usage; done means the intended common item binding behavior is supported without breaking existing adapters.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, kotlin
Domain
mobile-dev
Issue type
Refactor
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.