使用Groupie简化RecyclerView。

RecyclerViewAdapter can be very complex especially when it comes to dealing with different types of items. Groupie simplifies that for us.

RecyclerViewAdapter可能非常复杂,尤其是在处理不同类型的项目时。 Groupie为我们简化了这一过程。

Groupie is a simple, flexible library for complex RecyclerView layouts. It makes it easy to handle asynchronous content updates and insertions and user-driven content changes. At the item level, It abstracts the boilerplate of item view types, item layouts, viewholders, and span sizes.

Groupie是一个简单,灵活的库,用于复杂的RecyclerView布局。 它使处理异步内容更新和插入以及用户驱动的内容更改变得容易。 在项目级别,它抽象化了项目视图类型,项目布局,视图持有者和跨度大小的样板。

Here is a simple example while using Groupie.

这是使用Groupie时的一个简单示例。

Let’s get started!

让我们开始吧!

在Gradle Build中添加依赖项(模块:应用程序) (Add dependencies in Gradle Build(Module: app))

implementation "com.xwray:groupie:$groupie_version"
implementation "com.xwray:groupie-kotlin-android-extensions:$groupie_version"implementation ("io.coil-kt:coil:0.11.0")

We will be adding the Groupie dependencies only( you can check more)for this post we would only be needing these, and we will be using coil, an image loading library for Android.

对于这篇文章,我们将仅添加Groupie依赖项(您可以检查更多),我们只需要这些依赖项,并且我们将使用线圈,即Android的图像加载库。

创建布局 (Create Layouts)

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/peopleRV"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

We created the activity_main.xml which contains our RecyclerView (set the layout manager attribute to LinearLayout) and the people_layout.xml which is the layout for the RecyclerView items.

我们创建了activity_main.xml,其中包含我们的RecyclerView(将布局管理器属性设置为LinearLayout)和people_layout.xml ,这是RecyclerView项的布局。

RecyclerView适配器 (RecyclerView Adapter)

GroupieAdapter has taken care of this for us, Our only concern now is populate the adapter using adapter.add() That’s all we need; you can now add your new item directly to a GroupAdapter and then attach it to our RecyclerView.

GroupieAdapter已经为我们解决了这个问题,我们现在唯一需要关心的就是使用adapter.add()填充适配器。 您现在可以将新项目直接添加到GroupAdapter,然后将其附加到我们的RecyclerView。

val adapter = GroupAdapter<GroupieViewholder>()

val adapter = GroupAdapter <GroupieViewholder>()

class MainActivity : AppCompatActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setSupportActionBar(toolbar)


        val adapter = GroupAdapter<GroupieViewHolder>()


        adapter.add(PeopleItem("Black Panther", R.drawable.blackpanther))
        adapter.add(PeopleItem("Black Widow",R.drawable.blackwidow))
        adapter.add(PeopleItem("Captain America", R.drawable.captainamerica))
        adapter.add(PeopleItem("Hulk", R.drawable.hulk))


        peopleRV.adapter = adapter




    }
}

创建项目类 (Creating the Item Class)

Groupie abstracts away the complexity of multiple item view types. Each Item declares a view layout id, and gets a callback to bind the inflated layout.

Groupie消除了多种项目视图类型的复杂性。 每个Item声明一个视图布局ID,并获取一个回调以bind膨胀的布局。

class PeopleItem(val name: String, val image: Int) : Item<GroupieViewHolder>() {


        override fun getLayout() = R.layout.people_layout


        override fun bind(viewHolder: GroupieViewHolder, position: Int) {
            viewHolder.itemView.peopleTxt.text = name
            viewHolder.itemView.peopleImage.load(image)


        }
    }

The PeopleItem class gives you simple callbacks to bind your model object to the generated fields. Because of Kotlin Android extensions, there’s no need to write a ViewHolder Class. Now we will override two methods bind() and getLayout(). The getLayout() will return the layout i.e people_layout.xml and the bind() method will bind our views to the data.

PeopleItem类为您提供了简单的回调,以将模型对象绑定到生成的字段。 由于Kotlin Android扩展,因此无需编写ViewHolder类。 现在,我们将重写两个方法bind()getLayout()getLayout()将返回布局,即people_layout.xmlbind()方法会将我们的视图绑定到数据。

And thats it!

就是这样!

Groupie helps with more complex implementation of RecyclerView like treating your content as logical groups and handles change notifications for you, Think of sections with headers and footers, expandable groups, blocks of vertical columns, and much more.

Groupie有助于RecyclerView的更复杂的实现,例如将您的内容视为逻辑组并为您处理更改通知,考虑具有页眉和页脚,可扩展组,垂直列的块等。

Hope this helps, Thank you.

希望对您有所帮助,谢谢。

You can follow me on twitter @iamsoosy

您可以在Twitter上关注我@iamsoosy

翻译自: https://medium.com/@soosyamoora/simplifying-recyclerview-using-groupie-19ef361e52b5

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值