我有两个不同的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)
}
答案 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。
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
}
}