15

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.

How can I show two items with horizontal scroll with recycler view?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Chhorn Soro
  • 3,061
  • 8
  • 27
  • 43

6 Answers6

18

Alright here is a funda,

When you are using GridLayoutManager.HORIZONTAL, second parameter will be considered as number of rows

while, when you write GridLayoutManager.VERTICAL second parameter will be considered as number of columns

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.

Paresh P.
  • 6,677
  • 1
  • 14
  • 26
  • yeah the item show 2 column, but it doesn't scroll horizonal but it scroll vertically. Do you understand what I want? – Chhorn Soro Nov 15 '17 at 06:50
  • Yeah, because you have declared layoutManager as `LinearLayoutManager`. – Paresh P. Nov 15 '17 at 06:53
  • 2
    now I change it to GridLayoutManager already and set GridLayoutManager.HORIZONTAL. Now I can scroll my product left & right perfectly, but It showed one column with 2 rows. What I want is 1 row with 2 columns. Please help Thanks – Chhorn Soro Nov 15 '17 at 06:58
  • Have you found the solution? I'm facing the same problem – Taha Sami Feb 23 '22 at 12:47
6

To achieve what you need you need to combine two things: setup GridLayoutManager and set up adapter:

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:

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

in 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
Pietrek
  • 1,420
  • 14
  • 18
3

Recently faced the same problem, ended with creating a custom GridLayoutManager that allowed me to fix the row count and column count together.

Link : -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.

Kaveri
  • 1,060
  • 2
  • 12
  • 21
0

try below code

RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(this, 2);
    recyclerView.setLayoutManager(mLayoutManager);
vishal jangid
  • 2,967
  • 16
  • 22
0

All answers at Java but if someone searches for Kotlin;

val layoutManager = GridLayoutManager(requireContext(),  2)

    recyclerViewSymbol.layoutManager = symbolLayoutManager
-3
RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(activity,2,LinearLayoutManager.HORIZONTAL,false);
recyclerView.setLayoutManager(mLayoutManager);
fcdt
  • 2,371
  • 5
  • 14
  • 26
  • 3
    Welcome to Stack Overflow. Code only answers can generally be improved by adding some explanation of how and why they work. For older questions with existing answers it can also be useful to point out what new aspect of the question or improved technique your answer addresses. – Jason Aller Aug 03 '20 at 01:09