我想使用DataBinding在Kotlin中创建具有多个布局的RecyclerviewAdapter

时间:2018-04-21 17:54:05

标签: android android-recyclerview kotlin android-databinding

我的要求是根据设置为recyclerview的LayoutManager设置不同的布局。代码已经使用Kotlin和DataBinding完成。我定义了 getItemViewType()来设置布局类型,但却停留在数据绑定部分。请帮我做。

import android.os.Build
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup


class WallRecyclerAdapter(private var images: List<Image>, var listener: OnItemClickListener, var type: Int) : RecyclerView.Adapter<WallRecyclerAdapter.WallViewHolder>() {

    fun replaceData(items: List<Image>) {
        setupData(items)
    }

    private fun setupData(items: List<Image>) {
        this.images = items
        notifyDataSetChanged()
    }

    override fun getItemViewType(position: Int): Int {
        return if(type == HomeFragment.RECYCLER_TYPE_GRID){
            R.layout.wall_recycler_item
        }else{
            R.layout.wall_recycler_list_item
        }
    }

    override fun onBindViewHolder(holder: WallViewHolder, position: Int) {
        val data = images[position]
        holder.binding.wallpaper = data

        holder.binding.maincardcontainer.setOnClickListener({
            listener.onClick(holder.binding.imgRecyclerWallpaper,position,data)
        })
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): WallViewHolder {
        val layoutInflater = LayoutInflater.from(parent.context)
        val binding = WallRecyclerItemBinding.inflate(layoutInflater, parent, false)
        return WallViewHolder(binding)
    }

    override fun getItemCount(): Int {
        return images.size
    }

    interface OnItemClickListener {
        fun onClick(view: View,position: Int, wallpaper : Image)
    }

    class WallViewHolder(var binding: WallRecyclerItemBinding) : RecyclerView.ViewHolder(binding.root)
}

1 个答案:

答案 0 :(得分:0)

根据@pskink的回答,我更改了我的适配器并且工作正常。

class WallRecyclerAdapter(private var images: List<Image>, private var type: Int, var listener: WallRecyclerAdapter.OnItemClickListener) : RecyclerView.Adapt.AutoCompleteViewHolder>() {


    fun replaceData(items: List<Image>) {
        setupData(items)
    }

    private fun setupData(items: List<Image>) {
        this.images = items
        notifyDataSetChanged()
    }


    override fun getItemViewType(position: Int): Int {
        if (type == HomeFragment.RECYCLER_TYPE_LIST) {
            return R.layout.wall_recycler_list_item
        } else if (type == HomeFragment.RECYCLER_TYPE_GRID) {
            return R.layout.wall_recycler_item
        }
        throw RuntimeException("invalid obj")
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AutoCompleteViewHolder {
        return AutoCompleteViewHolder.create(LayoutInflater.from(parent.context), parent, viewType)
    }

    override fun onBindViewHolder(holder: AutoCompleteViewHolder, position: Int) {
        holder.bindTo(images[position])
    }

    override fun getItemCount(): Int {
        return images.size
    }

    class AutoCompleteViewHolder(val binding: ViewDataBinding) : RecyclerView.ViewHolder(binding.root) {

        fun bindTo(data: Any) {
            binding.setVariable(BR.Image, data)
            binding.executePendingBindings()
        }

        companion object {

            fun create(inflater: LayoutInflater, parent: ViewGroup, viewType: Int): AutoCompleteViewHolder {
                val binding = DataBindingUtil.inflate<ViewDataBinding>(inflater, viewType, parent, false)

                return AutoCompleteViewHolder(binding)
            }
        }
    }
}