databinding使用_使用DataBinding的简单列表的强大且可重用的ListAdapter

databinding使用

Through the years of developing Android apps, there have been countless times that we implemented simple lists. Lists that only have these basic features:

在开发Android应用程序的多年中,无数次我们实现了简单列表。 仅具有以下基本功能的列表:

  1. Display items

    显示项目
  2. Handle item clicks

    处理项目点击

Using ListAdapter and with the help of DataBinding, we can create a powerful and reusable SimpleListAdapter which we can use every time we implement a simple list.

使用ListAdapter并在DataBinding的帮助下我们可以创建功能强大且可重用的SimpleListAdapter ,每次实现简单列表时都可以使用。

I’ll share a step by step tutorial in this article on how to achieve this.

我将在本文中分享有关如何实现此目标的分步教程。

Enough of the introduction and let’s get our hands dirty!

足够的介绍,让我们动手吧!

  1. Let’s create our item layout. For this one, let’s assume we will display a list of news articles. Let’s go ahead and create our layout called item_article.xml.

    让我们创建项目布局。 对于这个,假设我们将显示新闻列表。 让我们继续创建名为item_article.xml的布局。

...<data>
<variable
name="item"
type="com.your.package.name.Article" />
</data>...<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/itemClickable"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:padding="13dp"> ... Your layout implementation.</androidx.constraintlayout.widget.ConstraintLayout>

There are 2 things to note here:

这里有两件事要注意:

  • First, your item layout’s variable must be named item.

    首先,项目布局的变量必须命名为item。

  • Second, the view that handles the item click should have an id of itemClickable.

    其次,处理项目单击的视图应具有iditemClickable

We will need them for the reusability of our adapter.

我们将需要它们来确保适配器的可重用性。

2. Let’s create our reusable ViewHolder class. We’ll simply name it ViewHolder.

2.让我们创建可重用的ViewHolder类。 我们将其简单命名为ViewHolder

open class ViewHolder<B : ViewDataBinding>(val binding: B) : RecyclerView.ViewHolder(binding.root) {
fun bind(item: Any) {
binding.setVariable(BR.item, item)
binding.executePendingBindings()
}
}

Notice that the variable item that we required in step 1 is being used here as BR.item.

请注意,我们在步骤1中需要的变量在这里用作BR.item

Let’s just leave it like this. The parameters of this class will be explained in the later steps.

让我们这样保留它。 此类的参数将在后面的步骤中进行说明。

3. Let’s create our reusable adapter called SimpleListAdapter.

3.让我们创建一个名为SimpleListAdapter的可重用适配器

class SimpleListAdapter<T : Any, B : ViewDataBinding>(
diffUtil: DiffUtil.ItemCallback<T>,
@LayoutRes private val layoutId: Int,
private val itemClickListener: (T) -> Unit
) : ListAdapter<T, ViewHolder<B>>(diffUtil) {

// Will be implemented in the next steps...}

Let’s determine the parameters for this class. Note that this class accepts 2 type parameters and 3 constructor parameters. Let’s break them down below.

让我们确定此类的参数。 请注意,此类接受2个类型参数3个构造函数参数 。 让我们在下面细分一下。

Type parameters:

类型参数:

  • T : Any — This is the item type class. In our example, we should pass the class Article as the first type parameter.

    T : Any —这是项目类型类。 在我们的示例中,我们应该将Article类作为第一个类型参数传递。

  • B : ViewDataBinding — This is the item type binding class that has been generated by DataBinding from our item layout xml. Based on our example in step 1, the generated class should be ItemArticleBinding.

    B : ViewDataBinding —这是DataBinding从我们的项目布局xml生成的项目类型绑定类。 根据步骤1中的示例,生成的类应为ItemArticleBinding

Constructor parameters:

构造函数参数:

  • diffUtil — You should implement a DiffUtil.ItemCallback unique for your item type. This will be used by the ListAdapter to validate your list items. We will be using ArticleDiffUtil in our example.

    diffUtil —您应该为项目类型实现唯一的DiffUtil.ItemCallback 。 ListAdapter将使用它来验证您的列表项。 在示例中,我们将使用ArticleDiffUtil

  • layoutId — This is the layout id of your item layout xml, which is R.layout.item_article in our example.

    layoutId —这是项目布局xml的布局ID,在我们的示例中为R.layout.item_article

  • itemClickListener — Lastly, a callback that will be invoked every time user clicks an item from the list.

    itemClickListener —最后,一个回调,将在用户每次单击列表中的项目时调用。

4. Let’s implement onCreateViewHolder method inside our SimpleListAdapter.

4.让我们在SimpleListAdapter内部实现onCreateViewHolder方法。

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder<B> {
return ViewHolder<B>(
DataBindingUtil.inflate(
LayoutInflater.from(parent.context),
layoutId,
parent,
false
)
).apply {
binding
.root
.findViewById<View>(R.id.itemClickable)
.setOnClickListener {
itemClickListener.invoke(getItem(adapterPosition))
}
}
}

There are a few notable lines in this method:

此方法中有几行值得注意:

  • layoutId passed to our constructor parameter is being used to inflate the binding class and pass it to our ViewHolder’s constructor.

    传递给构造函数参数的layoutId用于膨胀绑定类,并将其传递给ViewHolder的构造函数。

  • itemClickListener passed to our constructor parameter is being invoked when an item click event occurs.

    当项目单击事件发生时,将调用传递到构造函数参数的itemClickListener

  • The required id from step 1, itemClickable, is being used as a subject for setting view click listener.

    步骤1中所需的id itemClickable用作设置视图点击侦听器的主题。

5. Let’s implement onBindViewHolder inside our SimpleListAdapter.

5.让我们在SimpleListAdapter中实现onBindViewHolder

override fun onBindViewHolder(holder: ViewHolder<B>, position: Int) {
holder.bind(getItem(position))
}

6. Finally, let’s use our SimpleListAdapter inside our activity/fragment.

6.最后,让我们在活动/片段内部使用SimpleListAdapter

binding.recyclerView.apply {
adapter =
SimpleListAdapter<Article, ItemArticleBinding>(
ArticleDiffUtil(),
R.layout.item_article
) { item ->
// Handle item click...
}
}

And that is basically it! We have just created our powerful and reusable SimpleListAdapter.

基本上就是这样! 我们刚刚创建了功能强大且可重用的SimpleListAdapter

This is really handy in implementing simple lists. As long as your list only has 1 item click listener, you can reuse this adapter.

这在实现简单列表中非常方便。 只要您的列表中只有1个单击侦听器,就可以重用此适配器。

奖金 (Bonus)

I have created a gist for the reusable SimpleListAdapter. Feel free to use this in your projects!

我为可重复使用的SimpleListAdapter创建了要点。 随时在您的项目中使用它!

Link: https://gist.github.com/jermainedilao/f7002e41f53bd601a3edd1437f498254

链接: https//gist.github.com/jermainedilao/f7002e41f53bd601a3edd1437f498254

Happy coding!

祝您编码愉快!

翻译自: https://medium.com/swlh/powerful-and-reusable-listadapter-using-databinding-for-simple-lists-55ac377fa750

databinding使用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值