简体   繁体   中英

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. 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.

RecyclerView in RecyclerView isn't the way to go. Instead you need a nestedView inside a RecyclerView . I found a good example for that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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