简体   繁体   English

RecyclerView 里面的 RecyclerView 在 kotlin

[英]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
    }
}

This is the adapter of my parent Recycler View in which I am taking lifeCycleOwner as a parameter in the constructor and then setting adapter of child recycler view in the bindViewHolder.这是我的父 Recycler View 的适配器,我将 lifeCycleOwner 作为构造函数中的参数,然后在 bindViewHolder 中设置子 Recycler 视图的适配器。 The problem is that the child recycler view is not being shown.问题是没有显示子回收站视图。 According to me, the mistake is in the lifeCycleOwner but still, I couldn't rectify the issue.据我说,错误出在 lifeCycleOwner 但我仍然无法纠正这个问题。

RecyclerView in RecyclerView isn't the way to go. RecyclerView中的RecyclerView不是 go 的方式。 Instead you need a nestedView inside a RecyclerView .相反,您需要在RecyclerView中使用nestedView I found a good example for that.我为此找到了一个很好的例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM