Я настраиваю два вложенных переработанных вида: Расположение сетки внутри линейного.И я использую два массива: ряд для линейных предметов.обои для Грид предметов.Я создал вложенные адаптеры сеточный адаптер внутри линейного адаптера.Таким образом, приложение работает, но каждый раз повторяет сетку с позиции 0. Как я могу изменить это, чтобы отображать 3 элемента в каждой строке, не начиная с элемента 0?
Структура моего приложения:
adapters: RecyclerViewGridAdapter.kt - RecyclerViewLinearAdapter.kt
fragments: NewFragment.kt
models: Row.kt - Wallpaper.kt
interfaces: OnLoadMoreListener.kt - OnWallpaperListener.kt
-MainActivity.kt
Layouts:
activity_main.xml
row_item.xml
wallpaper_item.xml // contain wallpaperRecyclerView
loading_item.xml
RecyclerViewLinearAdapter.kt
class RecyclerViewLinearAdapter(
var recyclerView: RecyclerView,
private var fragment: Fragment,
private var rows: MutableList<Row>,
private var wallpapers: MutableList<Wallpaper>,
private var onWallpaperListener: OnWallpaperListener
): RecyclerView.Adapter<RecyclerView.ViewHolder>() {
private val viewRowView = 0
private val viewLoadingView = 1
internal var loadMore: OnLoadMoreListener? = null
internal var isLoading: Boolean = false
internal var visibleThreshold: Int = 5
internal var lastVisibleItem: Int = 0
internal var totalItemCount: Int = 0
init {
val linearLayoutManager = recyclerView.layoutManager as LinearLayoutManager
recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
totalItemCount = linearLayoutManager.itemCount
lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition()
if (!isLoading && totalItemCount <= lastVisibleItem + visibleThreshold) {
loadMore?.onLoadMore()
isLoading = true
}
}
})
}
override fun onCreateViewHolder(p0: ViewGroup, p1: Int): RecyclerView.ViewHolder {
return when (p1) {
viewLoadingView -> {
val layoutInflater = LayoutInflater.from(fragment.context)
val inflater = layoutInflater.inflate(R.layout.loading_item, p0, false)
LoadingViewHolder(inflater)
}
viewRowView -> {
val layoutInflater = LayoutInflater.from(fragment.context)
val inflater = layoutInflater.inflate(R.layout.row_item, p0, false)
RowViewHolder(inflater)
}
else -> {
val layoutInflater = LayoutInflater.from(fragment.context)
val inflater = layoutInflater.inflate(R.layout.row_item, p0, false)
RowViewHolder(inflater)
}
}
}
override fun getItemCount(): Int {
return rows.size
}
override fun onBindViewHolder(p0: RecyclerView.ViewHolder, position: Int) {
when (p0) {
is RowViewHolder -> {
// Setting Grid Adapter Here
p0.wallpaperRecyclerView.layoutManager = GridLayoutManager(fragment.context, 3)
val count = when (position) {
0 -> 0
1 -> 3
else -> position*3
}
p0.wallpaperRecyclerView.adapter = RecyclerViewGridAdapter(fragment, wallpapers, onWallpaperListener)
}
is LoadingViewHolder -> {
p0.progressBar.isIndeterminate = true
}
}
}
override fun getItemViewType(position: Int): Int {
return if (rows[position] == Row(-1)) viewLoadingView else viewRowView
}
fun setLoaded() {
isLoading = false
}
fun setLoadMore(onLoadMoreListener: OnLoadMoreListener) {
this.loadMore = onLoadMoreListener
}
}
internal class LoadingViewHolder(view: View): RecyclerView.ViewHolder(view) {
var progressBar = view.progressBar!!
}
internal class RowViewHolder(
view: View
): RecyclerView.ViewHolder(view) {
var wallpaperRecyclerView = view.wallpaperRecyclerView!!
}
RecyclerViewGridAdapter.kt
class RecyclerViewGridAdapter(
private var fragment: Fragment,
private var wallpapers: MutableList<Wallpaper>,
private var onWallpaperListener: OnWallpaperListener
): RecyclerView.Adapter<RecyclerView.ViewHolder>() {
override fun onCreateViewHolder(p0: ViewGroup, p1: Int): RecyclerView.ViewHolder {
val layoutInflater = LayoutInflater.from(fragment.context)
val inflater = layoutInflater.inflate(R.layout.wallpaper_item, p0, false)
WallpaperViewHolder(inflater, onWallpaperListener)
return WallpaperViewHolder(inflater, onWallpaperListener)
}
override fun getItemCount(): Int {
return wallpapers.size
}
override fun onBindViewHolder(p0: RecyclerView.ViewHolder, p1: Int) {
p0.itemView.title.text = wallpapers[p1].title
p0.itemView.url.text = wallpapers[p1].url
}
}
internal class WallpaperViewHolder(
view: View,
private val onWallpaperListener: OnWallpaperListener
): RecyclerView.ViewHolder(view), View.OnClickListener {
var title = view.title
var url = view.url
init {
// OnClick
view.setOnClickListener(this)
}
override fun onClick(v: View?) {
onWallpaperListener.onWallpaperClick(adapterPosition)
}
}
Я хочу получить тот же результат, что и «GridLayoutManager (this, 3)», но внутри «LinearLayoutManager (this)».
Единственная причина, по которой я добавляю это, заключается в добавлении значка загрузки при прокрутке пользователя.