RecyclerView之实现部分字段固定,其余部分可左滑的效果

1.实现部分字段固定,其余部分可左滑的RecyclerView需要把需要固定的字段自己分开出来使用一个RecyclerView,而其余部分最外层需要有个HorizontalScrollView包裹另一个RecyclerView

先看布局文件

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/cl"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rv1"
            android:layout_width="139dp"
            android:layout_height="wrap_content"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <View
            android:id="@+id/view"
            android:layout_width="1dp"
            android:layout_height="180dp"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toEndOf="@+id/rv1"/>

        <HorizontalScrollView
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:fillViewport="true"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toEndOf="@+id/view"
            app:layout_constraintTop_toTopOf="parent">

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/rv2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
        </HorizontalScrollView>

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

注意:给RecyclerView设置管理器,不然就要在代码中设置

2.分别给这两个RecyclerView设置item

item_name

item_score,通过LineraLayout布局中的weight权重把多个tv控件均匀放到一列中,这边只举例一个简单TextView,具体实现看自己。

3.分别再给这两个RecyclerView设置适配器

适配器的设置大同小异,如果需要有标题跟随字段内容左右滑动的话,可以声明一个新的对象,这个对象中的内容可以先随便写,放到要传入adapter中的list的第一个位置,再把接口获取到的数据还是自己设置的数据放到这个list下;待传到适配器中再取list中的第一个做修改即可。

另一个适配器根据自己内容配置即可

4.在MainActivity中,先拿到RecyclerView控件,再把获取到的数据放到适配器中

①先定义获取到的list和适配器,再把数据放到对应的适配器中
//定义获取的数据
private var nowRegularDataList = mutableListOf<NowRegularData>()
//定义分别的适配器
private val playerInfoDataNameAdapter = PlayerInfoDataNameAdapter(nowRegularDataList) private val playerInfoDataScoreAdapter = PlayerInfoDataScoreAdapter(nowRegularDataList)

②拿到这两个RecyclerView的id,这边使用Binding拿到,也可以使用findViewById拿到

③定义一个initAdapter方法放到onCreate方法的最前面

private fun initAdapter() {
    binding.playerInfoDataRvNames.adapter = playerInfoDataNameAdapter
    binding.playerInfoDataRvScores.adapter = playerInfoDataScoreAdapter
}

记得在拿到数据放入nowRegularDataList后,更新一下RecyclerView内容

playerInfoDataNameAdapter.notifyDataSetChanged()
playerInfoDataScoreAdapter.notifyDataSetChanged()

5.实现效果

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值