两个ViewHolders一个适配器

时间:2019-09-29 17:39:13

标签: java android list kotlin adapter

我有两个不同的RecyclerViews片段,我对两个片段都做了ViewHolders

class RecyclerOneViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
    val title = itemView.list_tv
    val image = itemView.list_img
}

class RecyclerTwoViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) {
    val title = itemView.tv_card
    val image = itemView.iv_card
    val date = itemView.tv_card_date
}

我有适配器,但仅适用于第一个ViewHolder。我可以同时分享吗?怎么样?

class RecyclerAdapter(val movies: List<Movie>):
RecyclerView.Adapter<RecyclerOneViewHolder>() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerOneViewHolder {
    var itemView = LayoutInflater.from(parent.context).inflate(
        R.layout.layout_list, parent, false)
    return RecyclerOneViewHolder(itemView)
}

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

override fun onBindViewHolder(holderOne: RecyclerOneViewHolder, position: Int) {
    holderOne.title.text = movies.get(position).title
    Picasso.get().load("https://image.tmdb.org/t/p/original"+movies.get(position).img)
        .into(holderOne.image)
}

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

3 个答案:

答案 0 :(得分:0)

您应该更改此设置:

class RecyclerAdapter(val movies: List<Movie>): RecyclerView.Adapter<RecyclerOneViewHolder>()

对此:

class RecyclerAdapter(val movies: List<Movie>): RecyclerView.Adapter<RecyclerView.ViewHolder>()

您可以将视图持有者转换为视图持有者,但是您也需要实现getItemViewType()方法。

答案 1 :(得分:0)

我想您是在问您有两个RecyclerViews,两个ViewHolders,一个适配器,并将那个适配器用于两个RecyclerViews。

  • 首先,您必须创建一个公共ViewHolder并将其扩展到其他ViewHolders。
  • 适配器类中应该有一个变量,以识别视图持有者的类型。
  • 那应该作为构造函数变量传递。
class RecyclerAdapter(val movies: List<Movie>, val type: String):
        RecyclerView.Adapter<CommonViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CommonViewHolder{

        if(type.equals("type1"){
            var itemView = LayoutInflater.from(parent.context).inflate(R.layout.layout_list, parent, false)
            return RecyclerOneViewHolder(itemView)
        } else {
            var itemView = LayoutInflater.from(parent.context).inflate(R.layout.layout_list2, parent, false)
            return RecyclerTwoViewHolder(itemView)
        }
    }
}

由于变量不同,您还需要在onBindViewHolder方法中检查viewHolder的类型

答案 2 :(得分:0)

您可以在Movie数据类上添加“视图类型”整数属性

    data class Movie(
        val title: String,
        val image: String,
        val date: String,
        val viewType: Int = 0
    )

然后在您的getItemViewType上从电影数据类返回视图类型

override fun getItemViewType(position: Int): Int {
    val movie = getItemByPosition(position) as Movie
    return movie.viewType
}

在您的onCreateViewHolder上,您可以使用它来确定您的视图类型

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): androidx.recyclerview.widget.RecyclerView.ViewHolder {
    when (viewType) {
        0 -> {
            val v = inflater.inflate(R.layout. layout_list, parent, false)
            return RecyclerOneViewHolder(v)
        }
        else -> {
            val v = inflater.inflate(R.layout. layout_list2, parent, false)
            return RecyclerTwoViewHolder(v)
        }
    }
}

最后,您可以在自己的onBindViewHolder

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val position = holder.adapterPosition
        when (getItemViewType(position)) {
            0 -> {
                holder as RecyclerOneViewHolder
                // set view type 1 content here
            }
            else -> {
                holder as RecyclerTwoViewHolder
                // set view type 2 content here
            } 
    }