HorizontalScrollView+recyclerView

HorizontlScrollView+recyclerView

最近工作中,遇到一些需求,需要recyclerView竖着展示数据的同时,可以左右滑动,刚开始,我觉得这个可以用recyclerView嵌套一下就可以做,但是发现自己的水平不是很够,就在大佬的指导下,找到了HorizontlScrollView这个横向滑动的控件,找到这个后觉得接下来就和容易了,但是问题还是不少,不多说废话,接下来,来说一下我的使用过程吧,

<HorizontalScrollView
                    android:id="@+id/scroll"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    app:layout_constraintHorizontal_weight="3"
                    app:layout_constraintLeft_toRightOf="@+id/time"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintRight_toRightOf="parent"
                    android:scrollbars="none"
                    android:fillViewport="true">
<!--                    android:fillViewport="true"-->
                    <androidx.recyclerview.widget.RecyclerView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:id="@+id/hor_recycler" />
                </HorizontalScrollView>

这是布局,fillViewport=“true” 这个属性熟悉scrollView的朋友都知道,他是让scrollView的子控件在内容不足时能够显示,这里就不多做解释了,

        data.add("¥10000000")
        data.add("¥10000000")
        data.add("¥10000000")
        data.add("¥10000000")
        data.add("¥10000000")
        data.add("¥10000000")

        times.add("06-01")
        times.add("06-02")
        times.add("06-03")
        times.add("06-04")
        times.add("06-05")
        times.add("06-06")


        times.add(0,"")
        data.add(0,"")

        recyclerview.init(
                LinearLayoutManager(context),
                timeAdapter
        )

        recyclerViewAll.init(
                GridLayoutManager(context, data.size, RecyclerView.HORIZONTAL, false),
//                LinearLayoutManager(context),
                testAdapter
        )
        testAdapter.setNewData(data)
        timeAdapter.setNewData(times)

这个就是给recyclerView设置适配器,接下来重点就来了,

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingLeft="10dp"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    android:clipChildren="false">

    <TextView
        android:id="@+id/text"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:text="目标数"
        android:textSize="15sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/text2"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:text="达成数"
        android:textSize="15sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/text3"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:text="完成率"
        android:textSize="15sp"
        android:textStyle="bold" />

</LinearLayout>

这个是recyclerView适配器的布局,为了能让我们的内容能够超出屏幕,让我们整个布局滑动起来,所以给text设置宽度为150dp

class TestAdapter: BaseQuickAdapter<String,BaseViewHolder>(R.layout.adapter_test) {
    override fun convert(helper: BaseViewHolder, item: String?) {
        val adapterPosition = helper.adapterPosition
        if (adapterPosition == 0){

        }else{
            helper.setText(R.id.text,item)
            helper.setText(R.id.text2,"吃吃喝喝")
            helper.setText(R.id.text3,"芜湖")
        }
    }
}

这就是适配器的内容,效果我这是在是不会整一个Gif图,所以就不发效果图了,如果有不明白或者有问题的可以直接留言或者私信,我看到就会第一时间回复

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值