RecyclerView inside RecyclerView in kotlin


package com.example.expensemanager.ui.monthlyCards
import android.app.Activity
import android.content.Context
import android.content.SharedPreferences
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.example.expensemanager.R
import com.example.expensemanager.ui.TransactionAdapter
import kotlinx.android.extensions.LayoutContainer
import kotlinx.android.synthetic.main.fragment_monthly_cards.*
import kotlinx.android.synthetic.main.monthly_card_item.*
class MonthCardsAdapter(private val listener: (Long) -> Unit ,val sharedPreferences: SharedPreferences , val activity:FragmentActivity?, val viewModel: MonthlyCardsViewModel,val lifecycleOwner: LifecycleOwner):
ListAdapter<Long, MonthCardsAdapter.ViewHolder>(
DiffCallback1()
){
override fun onCreateViewHolder(parent: ViewGroup,
viewType: Int): ViewHolder {
val itemLayout = LayoutInflater.from(parent.context)
.inflate(R.layout.monthly_card_item, parent, false)
return ViewHolder(itemLayout)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.bind(getItem(position))
}
inner class ViewHolder (override val containerView: View) : RecyclerView.ViewHolder(containerView),
LayoutContainer {
init{
itemView.setOnClickListener{
listener.invoke(getItem(adapterPosition))
}
}
fun bind(monthYear: Long){
month_name.text = monthYear.toString()
year_name.text = "2001"
val monthlyBudget = sharedPreferences.getFloat("monthlyBudget",0F)
monthly_budget.text = monthlyBudget.toString()
child_recycler_view.apply {
layoutManager = LinearLayoutManager(activity)
adapter = TransactionAdapter({})
}
viewModel.transactionMonth.observe(lifecycleOwner, Observer {
(child_recycler_view.adapter as TransactionAdapter).submitList(it)
})
}
}
}
class DiffCallback1 : DiffUtil.ItemCallback<Long>() {
override fun areItemsTheSame(oldItem: Long, newItem: Long): Boolean {
return oldItem == newItem
}
override fun areContentsTheSame(oldItem: Long, newItem: Long): Boolean {
return oldItem == newItem
}
}

这是我的父Recycler视图的适配器,我在构造函数中使用lifeCycleOwner作为参数,然后在bindViewHolder中设置子Recycle视图的适配器。问题是没有显示儿童回收者的观点。据我说,这个错误发生在生命周期所有者身上,但我仍然无法纠正这个问题。

RecyclerView中的RecyclerView不是一条路。相反,您需要RecyclerView中的nestedView。我找到了一个很好的例子。

相关内容

  • 没有找到相关文章

最新更新