简体   繁体   English

Recycler View 水平滚动 2 列

[英]Recycler View Horizontal Scroll with 2 columns

Here is I have I have tried so far这是我到目前为止尝试过的

LinearLayoutManager layoutManager =
        new GridLayoutManager(getActivity(), 2, GridLayoutManager.HORIZONTAL, false);

but It showed 2 rows instead of two columns.但它显示了 2 行而不是 2 列。

How can I show two items with horizontal scroll with recycler view?如何使用回收站视图显示两个带有水平滚动的项目?

Alright here is a funda,好的,这是一个基础,

When you are using GridLayoutManager.HORIZONTAL , second parameter will be considered as number of rows当您使用GridLayoutManager.HORIZONTAL时,第二个参数将被视为行数

while, when you write GridLayoutManager.VERTICAL second parameter will be considered as number of columns同时,当您编写GridLayoutManager.VERTICAL时,第二个参数将被视为列数

Also, this还有,这

LinearLayoutManager layoutManager =
        new GridLayoutManager(getActivity(), 2, GridLayoutManager.HORIZONTAL, false);

supposed to be,应该是,

GridLayoutManager layoutManager =
            new GridLayoutManager(getActivity(), 2, GridLayoutManager.HORIZONTAL, false);

in above code, 2 is considered as number of rows to be generated.在上面的代码中,2 被认为是要生成的行数。

To achieve what you need you need to combine two things: setup GridLayoutManager and set up adapter:要实现您需要的内容,您需要结合两件事:设置 GridLayoutManager 和设置适配器:

in Fragment/Activity:在片段/活动中:

 val snapHelper = PagerSnapHelper()
        snapHelper.attachToRecyclerView(recyclerView)

//Grid layout, 2 columns
        recyclerView.layoutManager =
            GridLayoutManager(this.context, 2, RecyclerView.HORIZONTAL, false)

and then in your adapter:然后在您的适配器中:

var displayMetrics = DisplayMetrics()
private var screenWidth = 0

in onCreateViewHolder:在 onCreateViewHolder 中:

(parent.context as MainActivity).windowManager.defaultDisplay.getMetrics(displayMetrics)
    screenWidth = displayMetrics.widthPixels

in onBindViewHolder:在 onBindViewHolder 中:

    val itemPadding = 8

    //here you may change the divide amount from 2.5 to whatever you need 
    val itemWidth = (screenWidth - itemPadding).div(2.5)

    val layoutParams = holder.itemView.layoutParams
    layoutParams.height = layoutParams.height
    layoutParams.width = itemWidth.toInt()
    holder.itemView.layoutParams = layoutParams

Recently faced the same problem, ended with creating a custom GridLayoutManager that allowed me to fix the row count and column count together.最近遇到了同样的问题,最后创建了一个自定义 GridLayoutManager,它允许我一起修复行数和列数。

Link : - https://gist.github.com/KaveriKR/04bfef5ffc9c00a8b6fca503da497322链接: - https://gist.github.com/KaveriKR/04bfef5ffc9c00a8b6fca503da497322

This custom layout manger makes use of the generateDefaultLayoutParams and two other functions to set different layout params for the layout manger.这个自定义布局管理器使用generateDefaultLayoutParams和其他两个函数来为布局管理器设置不同的布局参数。

try below code试试下面的代码

RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(this, 2);
    recyclerView.setLayoutManager(mLayoutManager);

All answers at Java but if someone searches for Kotlin; Java 的所有答案,但如果有人搜索 Kotlin;

val layoutManager = GridLayoutManager(requireContext(),  2)

    recyclerViewSymbol.layoutManager = symbolLayoutManager
RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(activity,2,LinearLayoutManager.HORIZONTAL,false);
recyclerView.setLayoutManager(mLayoutManager);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM