如您所知,您可以按照以下步骤创建带有多个ViewHolder的RecyclerView:
getItemViewType()
onCreateViewHolder()
onBindViewHolder()
但我只能在另一行创建两个viewholder,例如: 我有一个清单(项目1 - 类型1,项目2 - 类型1,项目3 - 类型2) 所以当我使用带有span列为2的GridLayoutManager时,item-1和item-2是同一行,因为它的类型相同,
**我有另一个清单(第1项 - 第1类,第2项 - 第3类,第3项 - 第2类) **
它们将被安排3个三行,所以我想要将item1和item 2组合在一起,而不是组合布局。谁能有想法?
package info.androidhive.recyclerview
import android.annotation.TargetApi
import android.content.Context
import android.content.res.Configuration
import android.os.Build
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
class MoviesAdapter(private val moviesList: List<Movie>) : RecyclerView.Adapter<MoviesAdapter.ViewHolder>() {
var pos = true
fun notifyLayoutChanged(context: Context) {
val isLanscape = context.resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE
convert(moviesList, isLanscape)
notifyDataSetChanged()
}
@TargetApi(Build.VERSION_CODES.N)
private fun convert(moviesList: List<Movie>, isLanscape: Boolean) {
if (isLanscape) {
moviesList.forEach {
if (it.type == Movie.Type.PICKUP) {
it.type = Movie.Type.BANNER
}
}
}
}
class MyViewHolder(view: View) : ViewHolder(view) {
var title: TextView
var year: TextView? = null
var genre: TextView
init {
title = view.findViewById<View>(R.id.title) as TextView
genre = view.findViewById<View>(R.id.genre) as TextView
}
override fun bind(movie: Movie) {
title.text = movie.title
genre.text = movie.genre
}
companion object {
fun create(context: Context): MyViewHolder {
return MyViewHolder(LayoutInflater.from(context)
.inflate(R.layout.movie_list_row, null, false))
}
}
}
class BannerViewHolder(view: View) : ViewHolder(view) {
var view1: MovieView
var view2: MovieView
init {
view1 = view.findViewById<View>(R.id.item1) as MovieView
view2 = view.findViewById<View>(R.id.item2) as MovieView
}
override fun bind(movie: Movie) {
view1.bind(movie)
view2.bind(movie)
}
companion object {
fun create(context: Context): BannerViewHolder {
return BannerViewHolder(LayoutInflater.from(context)
.inflate(R.layout.banner, null, false))
}
}
}
abstract class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
abstract fun bind(movie: Movie)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
if (viewType == Movie.Type.BANNER.ordinal) {
if (pos) {
pos = false
return MyViewHolder.create(parent.context)
} else {
pos = true
return BannerViewHolder.create(parent.context)
}
} else {
return MyViewHolder.create(parent.context)
}
}
override fun onDetachedFromRecyclerView(recyclerView: RecyclerView?) {
super.onDetachedFromRecyclerView(recyclerView)
}
override fun onBindViewHolder(holder: ViewHolder?, position: Int) {
if (holder != null) {
val movie = moviesList[position]
holder.bind(movie)
}
}
fun getItem(position: Int): Movie {
return moviesList[position]
}
override fun getItemViewType(position: Int): Int {
val movie = moviesList[position]
return movie.type.ordinal
}
override fun getItemCount(): Int {
return moviesList.size
}
}