android-fragments 我该怎么办?我在TabLayout片段中的片段上使用的RecyclerView无法显示

yk9xbfzb  于 2022-11-13  发布在  Android
关注(0)|答案(1)|浏览(208)

我试图在TabLayout中的一个片段上显示RecyclerView,而该TabLayout也是一个片段。但是,即使没有错误,RecyclerView仍然拒绝显示。根据我收集的信息,TabLayout的一个片段应该有可能具有RecyclerView。但是,我不知道问题是否在于我在一个片段上构建了我的TabLayout,导致recyclerview没有显示。我想我一定是做错了什么,我需要一些帮助。下面是我的代码:

表格布局片段

var viewPager = root.findViewById(R.id.viewPager) as ViewPager
    var tablayout = root.findViewById(R.id.tabLayout) as TabLayout

    val fragmentAdapter = FragmentAdapter(requireActivity().supportFragmentManager)
    fragmentAdapter.addFragment(OpenLoansFragment(), "Active")
    fragmentAdapter.addFragment(ClosedLoansFragment(), "Closed")

    viewPager.adapter = fragmentAdapter
    tablayout.setupWithViewPager(viewPager)

回收视图片段

private lateinit var swipeRefreshLayout:SwipeRefreshLayout
private lateinit var loanRecRcv:RecyclerView
private var list: ArrayList<LoanRecordModel> = ArrayList()
private var loanRecRcvAdapter = LoanRecRcvAdapter(list, this)

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {

    _binding = FragmentOpenLoansBinding.inflate(inflater, container, false)
    val root: View = binding.root

    return root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    list = arrayListOf(
        LoanRecordModel("", "Petty Cash Loan", "12/05/2022", "Open"),
        LoanRecordModel("", "Petty Cash Loan", "12/05/2022", "Open"),
        LoanRecordModel("", "Petty Cash Loan", "12/05/2022", "Open"),
        LoanRecordModel("", "Petty Cash Loan", "12/05/2022", "Open"),
        LoanRecordModel("", "Petty Cash Loan", "12/05/2022", "Open")

    )

    initView()

    loanRecRcv.apply{
        layoutManager = LinearLayoutManager(activity)
        adapter = loanRecRcvAdapter
    }
}

private fun initView() {

    swipeRefreshLayout = requireActivity().findViewById(R.id.swipeRefresh)
    loanRecRcv = requireActivity().findViewById(R.id.loanRecRcv)

}
eblbsuwk

eblbsuwk1#

您在RecyclerView中显示了一个空列表。首先,您在构建时创建了一个空适配器:

// empty list
private var list: ArrayList<LoanRecordModel> = ArrayList()
// adapter created with the empty list
private var loanRecRcvAdapter = LoanRecRcvAdapter(list, this)

然后将该适配器应用到您的RV上:

loanRecRcv.apply{
    ...
    adapter = loanRecRcvAdapter
}

您正在onViewCreated中创建项目列表

list = arrayListOf(
    LoanRecordModel("", "Petty Cash Loan", "12/05/2022", "Open"),
    ...
)

但这是在 * 替换 * 存储在list变量中的列表。之前(在构造时)已经使用原始列表创建了Adapter,所以它仍然在内部使用那个空列表。它没有引用list变量。
两个基本选项-将list设为MutableList并更新其内容而不是替换它:

// empty mutable list - you never need to actually use ArrayLists or whatever
// made it a val so you can't replace it!
private val list = mutableListOf<LoanRecordModel>()
// pass that list object to the adapter, so it's shared
private var loanRecRcvAdapter = LoanRecRcvAdapter(list, this)

...

// update your list contents
list.addAll(listOf(
    LoanRecordModel("", "Petty Cash Loan", "12/05/2022", "Open"),
    ...
))
// notify the adapter that it needs to refresh since its data set (the shared list) has changed
adapter.notifyDataSetChanged()
  • 或 *(最好)将新数据传递给适配器,让它在内部存储数据,处理自身更新等:
// in your adapter

// store the current data here - initially an empty list, so it can still display
private var list: List<LoanRecordModel> = emptyList()

fun setData(data: List<LoanRecordModel>) {
    // calling toList makes a new list object - even if the 'data' list object gets
    // modified later, we have our own internal list object that won't be affected
    list = data.toList()
    // update the display however you like
    notifyDataSetChanged()
}

// in your fragment

adapter.setData(listOf(
    LoanRecordModel("", "Petty Cash Loan", "12/05/2022", "Open"),
    ...
))

这种方法更好,因为适配器的数据集是内部保存的,而不是在片段中。您有一个简单的setData函数,它接受新数据,并根据需要处理适配器的更新。调用者不需要知道这些信息。它只传递数据。你也可以从Adapter的构造函数中移除你的List参数--你并没有将它初始化为空列表--这是内部list属性的默认值
顺便说一句,要初始化相同内容的列表,您可以

// create an immutable list
val list = List(5) { LoanRecordModel("", "Petty Cash Loan", "12/05/2022", "Open") }

// or add to a mutable list
list.addAll(List(5) { LoanRecordModel("", "Petty Cash Loan", "12/05/2022", "Open") })

// or another way
repeat(5) {
    list.add(LoanRecordModel("", "Petty Cash Loan", "12/05/2022", "Open"))
}

如果你愿意话

相关问题