1

I have recyclerview with GridLayoutManager and I want 2 things:

  1. 3 items in screen
  2. horizontal scroll

Issue

If I don't use horizontal scroll I can have 3 items in screen but yeah it's not horizontal, but if I add horizontal option to GridLayoutManager then it does not show 3 items in screen row.

Current code that gives me 3 items in row (but it's vertical)

laundriesRecycler.layoutManager = GridLayoutManager(context, 3)

Testing code that gives me horizontal scroll but not 3 items in row

laundriesRecycler.layoutManager = GridLayoutManager(context, 3, GridLayoutManager.HORIZONTAL, false)

layoutManager code

laundriesRecycler.layoutManager = GridLayoutManager(context, 3)
laundriesRecycler.adapter = NearLaundriesAdapter(context, list, useraddress)

one

mafortis
  • 6,750
  • 23
  • 130
  • 288
  • Your question is not clear enough can you some more detail about the expected result ? Do you want to snap 3 items at once? – ADM Apr 09 '21 at 09:27
  • @ADM same as screenshot I shared, `horizontal scroll` + `3 items in row` – mafortis Apr 09 '21 at 09:28
  • For this you can set Dynamic width for each item according to parent Width . You calculate the width once and pass it to adapter the set the width of item inside onCreateViewHolder. – ADM Apr 09 '21 at 09:31
  • @ADM can you show me how? – mafortis Apr 09 '21 at 09:36
  • Its seems a [Duplicate of This](https://stackoverflow.com/questions/35153618/how-to-show-exact-number-of-items-in-recyclerview) .. try this it should work . You also might wanna consider margins during calculation of width . just a Side not do the calculation only once i.e do not do it inside `onCreateViewHolder` . Give it a try . – ADM Apr 09 '21 at 09:40
  • @ADM thanks for the link, I couldn't understand anything of it and by copying that code all were red in my android studio (either code isn't in kotlin or it's old sdk code) thanks anyway. – mafortis Apr 09 '21 at 09:44
  • 1
    You don't need grid layout manager when you're showing a single row, linear is enough. Try [this solution](https://stackoverflow.com/a/51224889/9241978) but alter it to modify width instead of height. – Pawel Apr 09 '21 at 10:48
  • @mafortis this is not a comment of answer I've linked and it does work properly. – Pawel Apr 09 '21 at 11:08
  • @Pawel my apology, I was with phone I've made mistake, it's working very well thank you. – mafortis Apr 09 '21 at 12:08

0 Answers0