Adding Click listener to Generic Adapter

落爺英雄遲暮 提交于 2020-01-25 06:47:12

问题


I have created a Generic Adapter which will be used for any type of Data and any number of items (row_layout) in RecyclerView.

I have added clicklistener to it's GeneralViewHolder .Is there any flaw or issue in this adapter. And is there any callback which reduces recyclerView ?

GenericAdapter.kt

class GenericAdapter<T>(protected val items: List<T>, val adapterBind: OnAdapterBind<T>)
    : RecyclerView.Adapter<GeneralViewHolder<T>>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): GeneralViewHolder<T> =
            GeneralViewHolder(parent, viewType) { position, mView ->
                adapterBind.onItemClicked(position, mView)
            }

    override fun getItemCount(): Int = items.size
    override fun getItemViewType(position: Int): Int = adapterBind.setLayoutId(position, items[position]);

    override fun onBindViewHolder(holder: GeneralViewHolder<T>, position: Int) {
        adapterBind.onBindData(holder.itemView, items[position])
    }
}

GeneralViewHolder.kt

class GeneralViewHolder<T>(parent: ViewGroup, layoutId: Int,clicklistener : (pos: Int, aView: View) -> Unit) : RecyclerView.ViewHolder(
        LayoutInflater.from(parent.context).inflate(layoutId, parent, false)){

    init {
        itemView.setOnClickListener{
            clicklistener.invoke(adapterPosition,itemView)
        }
    }
}

OnAdapterBind.class

public interface OnAdapterBind<T> {
    ///GeneralViewHolder setViewHolder(ViewGroup parent,int viewType);
    void onBindData(View itemView, T data);
    int setLayoutId(int position,T obj);
    void onItemClicked(int position,View mView);
}

For single Type of item call it like

val carList = listOf<Car>(Car("Moras", Color.BLACK),Car("honda", Color.BLUE),Car("audi", Color.BLACK),
                    Car("Moras", Color.BLACK),Car("honda", Color.BLUE),Car("audi", Color.BLACK),
                    Car("Moras", Color.BLACK),Car("honda", Color.BLUE),Car("audi", Color.BLACK))
            recyclerView.adapter = GenericAdapter<Car>( carList, object : OnAdapterBind<Car> {

                override fun onBindData(itemView: View?, data: Car?) {
                    itemView?.fruitTv?.text = data?.name
                }

                override fun setLayoutId(position: Int, obj: Car?): Int = R.layout.item_fruit

                override fun onItemClicked(position: Int, mView: View?) {
                    Toast.makeText(this@FruitActivity,"Clicked -> " + position,Toast.LENGTH_LONG).show()
                }
            })

And for MultipleType of ViewType use..

val list= listOf<Any>(Car("audi", Color.BLACK),Bus("Eicher",Color.BLUE),Bus("Benz",Color.BLACK),Car("Suzuki",Color.WHITE)
            ,Car("Moras", Color.BLACK),Car("honda", Color.BLUE),Car("audi", Color.BLACK))




recyclerView.adapter = GenericAdapter<Any>( list, object : OnAdapterBind<Any> {

            override fun onBindData(itemView: View?, data: Any?) {
                when(data){
                    is Car -> itemView?.fruitTv?.text = "Car Rasha"
                    is Bus -> itemView?.vegetableTv?.text = "Bus Bus"
                }
            }

            override fun setLayoutId(position: Int, obj: Any?): Int {
                when(obj){
                    is Car -> R.layout.item_fruit
                    is Bus -> R.layout.item_vegetable
                }
                return R.layout.item_fruit
            }

            override fun onItemClicked(position: Int, mView: View?) {

            }
        })

来源:https://stackoverflow.com/questions/58693741/adding-click-listener-to-generic-adapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!