【问题标题】:Android Kotlin. Troubles with databinding to recyclerview using fragments安卓科特林。使用片段将数据绑定到 recyclerview 的问题
【发布时间】:2018-11-18 08:15:38
【问题描述】:

我正在尝试学习如何在 Android 应用中实现数据绑定。我有一个正在使用的小应用程序来学习这一点。虽然我有数据绑定为应用程序的一部分工作。我在尝试实施回收站视图时遇到了麻烦。我似乎无法理解。折腾了两三天,心里很郁闷。我想我会问你们。

这个应用程序现在超级简单。

我坚持的部分是从 MainFragment.kt 中的 .xml 布局访问我的 recyclerview

起初我尝试使用绑定,但感到沮丧并回到尝试使用 findViewById,但这也给我带来了问题。我开始想,我对数据绑定的掌握并没有我想象的那么牢固。

这是来自保存 recyclerView 的片段:

fragment_main.xml

<androidx.recyclerview.widget.RecyclerView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginTop="8dp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            android:layout_marginStart="8dp"
            android:layout_marginBottom="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            android:layout_marginEnd="8dp"
            android:id="@+id/job_recyclerView"/>

我有另一个小布局文件,它使用 Cardview 来显示 recyclerview 中的每个单独项目

一个超级简单的模型:

JobData.kt

data class JobData(val companyName: String, val location: String)

适配器:

JobAdapter.kt

class CustomAdapter(val userList: ArrayList<JobData>) : RecyclerView.Adapter<CustomAdapter.ViewHolder>() {

//Returning view for each item in the list
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomAdapter.ViewHolder {
    val v = LayoutInflater.from(parent.context).inflate(R.layout.job_item_layout, parent, false)
    return ViewHolder(v)
}

//Binding the data on the list
override fun onBindViewHolder(holder: CustomAdapter.ViewHolder, position: Int) {
    holder.bindItems(userList[position])
}

override fun getItemCount(): Int {
    return userList.size
}

//Class holds the job list view
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

    fun bindItems(job: JobData) {
        val textViewName = itemView.findViewById(R.id.tv_company_name) as TextView
        val textViewAddress  = itemView.findViewById(R.id.tv_Location) as TextView
        textViewName.text = job.companyName
        textViewAddress.text = job.location
    }
}
}

然后是我的 MainFragment 中的代码来处理这一切,但它没有这样做。我已经尝试了一切,它变得丑陋。正如你在下面看到的。绑定已就位并适用于我的 FloatingActionButton。但由于某种原因,我无法弄清楚如何访问该 recylerview。代码在下面,我以为我只是使用 findViewById 访问,但这也不起作用。

MainFragment.kt 类 MainFragment : Fragment() {

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    val binding: FragmentMainBinding = DataBindingUtil.inflate(
        inflater, R.layout.fragment_main, container, false)

    //Setting onClickListener for FAB(floating action button) using Navigation
    binding.createNewJobFAB.setOnClickListener { v: View ->
        v.findNavController().navigate(R.id.action_mainFragment_to_createNewJobFragment)
    }

    //getting recyclerview from xml
    val recyclerView = findViewById(R.id.job_recyclerView) as RecyclerView

    //adding a layoutmanager
    recyclerView.layoutManager = LinearLayoutManager(this, RecyclerView.VERTICAL, false)


    //Arraylist to store jobs using the data class JobData
    val jobs = ArrayList<JobData>()

    //add dummy data to list
    jobs.add(JobData("A Company", "Town A"))
    jobs.add(JobData("B Company", "Town B"))
    jobs.add(JobData("C Company", "Town C"))
    jobs.add(JobData("D Company", "Town D"))

    //creating adapter
    val adapter = CustomAdapter(jobs)

    //add adapter to recyclerView
    recyclerView.adapter = adapter
    return binding.root
}
}

上面编译失败有两个原因:

  1. findViewById 显示为“未解决的参考”。
  2. 添加 layoutManager 时,“this”显示为“类型不匹配”

我认为这是因为 Fragments 没有上下文。或者,我认为无论如何。但是不知道怎么解决?也许会覆盖其他一些方法,但我似乎无法弄清楚哪个或如何?

哦,MainActivity 看起来像:

MainActivity.kt

class MainActivity : AppCompatActivity() {

//private lateinit var binding: ActivityMainBinding

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    @Suppress("UNUSED_VARIABLE")
    val binding = DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)
}

//Ensures back button works as it should
override fun onSupportNavigateUp() = findNavController(this, R.id.navHostFragment).navigateUp()
}

指向 Android Navigation 的 Nav_Graph(JetPack 的一部分)。这一点很好,可以工作。

添加 gradle 文件以显示我的依赖项设置正确,如下所示。

应用程序/gradle

android {
compileSdkVersion 28
dataBinding {
    enabled = true
}
...
}

kapt {
generateStubs = true
correctErrorTypes = true
}

dependencies {
...
kapt "com.android.databinding:compiler:$gradle_version"

...
}

【问题讨论】:

  • 要使findViewById()LinearLayout 构造函数工作使用Fragment#getContext()。要解决数据绑定问题,请确保 app/build.gradle 中有 apply plugin: 'kotlin-kapt'app/build.gradle 的依赖项部分中的 kapt "com.android.databinding:compiler:version"
  • 抱歉,我忘了显示或提及我的依赖项。这些都设置好了。我将编辑主要帖子以显示该信息。如果我无法让绑定工作,我会考虑使用 Fragment#getContext()。
  • 你已经解决了这个问题吗?
  • 没有还在工作。
  • $gradle_version 的值是多少?

标签: android android-fragments android-recyclerview kotlin android-databinding


【解决方案1】:

&lt;layout&gt;..&lt;layout/&gt;封装你的xml

private lateinit var binding: FragmentXXXBinding

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        binding = FragmentXXXBinding.inflate(inflater)
        return binding.root
    }

然后你可以通过binding.jobRecyclerview调用recyclerview

尝试在片段的onViewCreated 而不是onCreateView 上设置所有点击侦听器等

【讨论】:

  • 你的例子就是我想这样做。这是我前几天尝试的方法之一。但由于我现在还无法弄清楚的原因,它不起作用。我的 .xml 包含在 .. 中,并且刚刚将 lateinit 添加到 FragmentMainBinding。但是调用 binding.job_recyclerView 是它失败的地方。 job_recyclerView 仍未解决?这就是我对绑定感到困惑的地方。
  • binding.jobRecyclerview 应该是,不是自动填充吗?如果它不是自动填充,我会是一个干净/重建的项目
  • 同意。我已经清理/重建了项目,甚至重新启动了 Android Studio。它不喜欢它。
  • 能否请您编辑并发布所有 xml,我感觉这会很愚蠢,就像名字错误啊哈
  • 对不起,对不起。我胖了手指。它现在看到 binding.jobRecyclerView。
【解决方案2】:

从 Fragment 中查找ViewById 的方法是错误的(这是 Activity 的好技术):

val recyclerView = findViewById(R.id.job_recyclerView) as RecyclerView

首先,片段的布局必须通过 onCreateView() 方法返回。

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
  return inflater.inflate(R.layout.fragment_main, container, false)
}

我个人喜欢在 onViewCreated() 中做所有 Fragment 的业务逻辑

override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    //Now, we can use views by kotlinx
    //val recyclerView = job_recyclerView
    //Or old-fashioned way
    val recyclerView = getView()!!.findViewById(R.id.job_recyclerView) as RecyclerView
}

RecylerView 可以通过以下方式从片段的布局中访问:getView()!!.findViewById 或通过 kotlinx 内部的 onViewCreated():job_recyclerView

【讨论】:

  • 抱歉,您的格式让我感到困惑?您似乎建议同时使用“onCreateView”和“onViewCreated”,还是我读错了?
  • 同意 koliczyna,onViewCreated() 应该处理业务逻辑,而不是 oncreateView(),这应该只是为了增加布局
  • @CB Midkiff 你说得对,我建议两个都用。
【解决方案3】:

好的,首先您在findViewById 上遇到错误,因为您的片段不知道包含recyclerView 的视图

你应该做的是,获取一个你正在为这个片段膨胀的视图实例(将视图声明为一个全局变量,用这个替换你的膨胀线)。

var rootView

// Inside onCreateView
var rootView = inflater?.inflate(R.layout.fragment, container, false) 

现在将 findViewById() 替换为 rootView.findViewById()

另一个错误是因为片段没有任何自己的上下文,所以将this替换为activity!!

通过编写activity!!,您正在调用getActicity() 方法,该方法返回父活动的上下文。

【讨论】:

  • 我听从了您的建议,它确实解决了编译器错误,应用程序现在可以运行了。 RecyclerView 没有按应有的方式填充。我根本没有出现。我认为这就是充气机的名称。但我只有一点点时间来处理它。我整天都在移民办公室处理签证问题。今天下午和今晚我会继续努力。
猜你喜欢
  • 1970-01-01
  • 2023-02-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-23
相关资源
最近更新 更多