使用viewtype时,onBindViewHolder和onCreateViewHolder是不同的。

huangapple go评论89阅读模式
英文:

using viewtype , onBindViewHolder and onCreateViewHolder are different

问题

我正在使用RecyclerView中的ViewType进行研究。
我使用了两种ViewType。当我测试更改ViewType时,没有发生变化。
我插入了ViewType1,但结果返回到ViewType0。
日志如下所示:

D/TAG: ProfileViewHolder onCreateViewHolder
D/TAG: ItemViewHolder onBindViewHolder

onCreateViewHolder是正确的,但ItemViewHolder不正确。彼此不同。
我应该如何修复?以下是我的代码。

class RecyclerViewAdapter(private val context: Context, private val items: ArrayList<DetailModel>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {

        if (holder is ProfileViewHolder) {
            holder.name.text = items[position].login
            Glide.with(context)
                .load(items[position].profileImage)
                .centerCrop()
                .override(200, 200)
                .error(R.drawable.image_not_found)
                .into(holder.profileImage)
            Log.d("TAG", "ProfileViewHolder onBindViewHolder")
        } else if (holder is ItemViewHolder) {
            holder.name.text = items[position].name
            holder.description.text = items[position].description
            holder.stargazersCount.text = items[position].stargazersCount.toString()
            Log.d("TAG", "ItemViewHolder onBindViewHolder")
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        if (viewType == 0) {
            val inflatedView: View = LayoutInflater.from(parent.context)
                .inflate(R.layout.detail_item, parent, false)
            Log.d("TAG", "ProfileViewHolder onCreateViewHolder")
            return ItemViewHolder(inflatedView)
        } else {
            val inflatedView: View = LayoutInflater.from(parent.context)
                .inflate(R.layout.profile_item, parent, false)
            Log.d("TAG", "ItemViewHolder onCreateViewHolder")
            return ProfileViewHolder(inflatedView)
        }
    }

    override fun getItemCount() = items.size

    override fun getItemId(position: Int): Long {
        return items[position].id.toLong()
    }

    class ItemViewHolder(v: View) : RecyclerView.ViewHolder(v) {
        val name: TextView = itemView.findViewById(R.id.name)
        val description: TextView = itemView.findViewById(R.id.description)
        val stargazersCount: TextView = itemView.findViewById(R.id.stargazersCount)
    }

    class ProfileViewHolder(v: View) : RecyclerView.ViewHolder(v) {
        val name: TextView = itemView.findViewById(R.id.login)
        val profileImage: ImageView = itemView.findViewById(R.id.profileImage)
    }
}
// 在 onCreate 中
var detail = DetailModel()
detail.login = userId
detail.viewType = 1
itemList.add(detail)

setAdapter()

希望这能帮助你解决问题。如果还有其他问题,请随时提出。

英文:

I'm studying using viewtype in recyclerview.
I use two viewtypes. when I tested for changing viewtype, there is no change.
I inserted viewtype1 but the result is returned to viewtype0.
the log is like below D/TAG: ProfileViewHolder onCreateViewHolder
D/TAG: ItemViewHolder onBindViewHolder

onCreateViewHolder is right, but the ItemViewHolder is not right. each other different.
what should I fix? here is my code.

class RecyclerViewAdapter(private val context: Context, private val items : ArrayList&lt;DetailModel&gt;) : RecyclerView.Adapter&lt;RecyclerView.ViewHolder&gt;(){

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {

        if(holder is ProfileViewHolder) {
            holder.name.text = items[position].login
            Glide.with(context)
                .load(items[position].profileImage)
                .centerCrop()
                .override(200,200)
                .error(R.drawable.image_not_found)
                .into(holder.profileImage)
            Log.d(&quot;TAG&quot;,&quot;ProfileViewHolder onBindViewHolder&quot;)
        }else if(holder is ItemViewHolder){
            holder.name.text = items[position].name
            holder.description.text = items[position].description
            holder.stargazersCount.text = items[position].stargazersCount.toString()
            Log.d(&quot;TAG&quot;,&quot;ItemViewHolder onBindViewHolder&quot;)
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        if(viewType == 0) {
            val inflatedView: View = LayoutInflater.from(parent.context)
                .inflate(R.layout.detail_item, parent, false)
            Log.d(&quot;TAG&quot;,&quot;ProfileViewHolder onCreateViewHolder&quot;)
            return ItemViewHolder(inflatedView)
        }
        else  {
            val inflatedView: View = LayoutInflater.from(parent.context)
                .inflate(R.layout.profile_item, parent, false)
            Log.d(&quot;TAG&quot;,&quot;ItemViewHolder onCreateViewHolder&quot;)
            return ProfileViewHolder(inflatedView)
        }
    }

    override fun getItemCount()=items.size

    override fun getItemId(position: Int): Long {

        return items.get(position).id.toLong()
    }

    class ItemViewHolder(v: View) : RecyclerView.ViewHolder(v) {


        val name :TextView = itemView.findViewById(R.id.name)
        val description : TextView = itemView.findViewById(R.id.description)
        val stargazersCount :TextView = itemView.findViewById(R.id.stargazersCount)

    }

    class ProfileViewHolder(v: View) : RecyclerView.ViewHolder(v) {
        val name :TextView = itemView.findViewById(R.id.login)
        val profileImage : ImageView = itemView.findViewById(R.id.profileImage)

    }



}

OnCreate

   var detail = DetailModel()
                detail.login= userId
                detail.viewType=1
                itemList.add(detail)

 setAdapter()

答案1

得分: 2

onCreateViewHolder 中的 viewType 是由 RecyclerView.Adapter 返回的,为了为每个位置分配 viewType 给这个适配器,您需要重写 getItemViewType

在您的情况下,可以这样做。

override fun getItemViewType(position: Int): Int {
    val detail = items[position]
    return detail.viewType
}
英文:

viewType in onCreateViewHolder is returned by RecyclerView.Adapter, To assign viewType to this adapter for each position, you need to override getItemViewType.

In your case it might be like this.

override fun getItemViewType(position: Int): Int {
    val detail = items[position]
    return detail.viewType
}

huangapple
  • 本文由 发表于 2020年1月4日 12:04:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/59587777.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定