【问题标题】:Recyclerview Binding in Another Fragment另一个片段中的 Recyclerview 绑定
【发布时间】:2020-10-24 07:58:28
【问题描述】:

在房间的帮助下,我正在测试一个简单的应用程序来保存单词。我有 2 个布局片段 - home(main) 和 myword(second)。我正在尝试将以下代码放入 myword 片段中以初始化 recyclerview,但出现未解决错误。我哪里错了?

frag_mywords.xml

    <androidx.recyclerview.widget.RecyclerView
    android:id="@+id/words_dail"
    android:layout_width="0dp"
    android:layout_height="0dp"
    tools:listitem="@layout/recykel_list"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

TakerFragment.kt

class TakerFragment : Fragment() {

private lateinit var takerViewModel: TakerViewModel
private var _binding: FragMywordsBinding? = null

private val binding get() = _binding!!

override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    takerViewModel =
        ViewModelProvider(this).get(TakerViewModel::class.java)

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

    val recyclerView = findViewById<RecyclerView>(R.id.words_dail)
    val adapter = WordListAdapter(this)
    recyclerView.adapter = adapter
    recyclerView.layoutManager = LinearLayoutManager(this)

    return root
}

override fun onDestroyView() {
    super.onDestroyView()
    _binding = null
   }
}

我如何正确解决这条线?

val recyclerView = findViewById<RecyclerView>(R.id.words_dail)
    val adapter = WordListAdapter(this)
    recyclerView.adapter = adapter
    recyclerView.layoutManager = LinearLayoutManager(this)

【问题讨论】:

  • 你能发布确切的堆栈跟踪吗? findViewById&lt;RecyclerView&gt;(R.id.recyclerview) 可能不正确,因为您的 RecyclerView ID 是 words_dail。所以应该是findViewById&lt;RecyclerView&gt; (R.id.words_dail)。但是你不再需要使用findViewById,使用 kotlin syntethics。所以只需要写val recyclerview = words_dail 然后导入syntethics
  • 是的,我更正了 words_dail。这是堆栈跟踪pastebin.com/hL0nHpUJ

标签: kotlin android-fragments data-binding


【解决方案1】:

这里有多个问题:

首先将val recyclerView = findViewById&lt;RecyclerView&gt;(R.id.words_dail) 更改为val recyclerView = words_dail

那么,你的 WorldListAdapter 的上下文是错误的,应该是val adapter = WordListAdapter(requireContext()) 而不是this

第三,你可以在xml中设置layoutManager:

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/words_dail"
android:layout_width="0dp"
android:layout_height="0dp"
tools:listitem="@layout/recykel_list"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" <--
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

使用数据绑定,您还可以在 xml 中设置适配器,但我不知道您是否在适配器中使用数据绑定。

【讨论】:

  • 感谢您的回答。 val recyclerView = wordds_dail 仍然给出未引用错误。不,我没有在适配器中使用数据绑定。 pastebin.com/JkFaDK2U
  • 你导入合成器了吗?
  • 似乎我通过将其映射到新活动而不是片段来临时修复它。我仍然无法让片段工作。
猜你喜欢
  • 2021-12-26
  • 2018-04-24
  • 2018-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-07
  • 1970-01-01
相关资源
最近更新 更多