Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Horizontal and vertical scrolling in android recycler view

I have a requirement where I need to have horizontal scrolling and vertical scrolling in a recycler view. It is based on the type of data that is coming from server. If the response from server is having first element as an array, i need that to be in a horizontal scrolling list and if the second element is a single object, then i need to show it in card. Similarly the order changes and should reflect in the UI. How can I achieve this.

like image 358
Harikrishnan CV Avatar asked Jul 10 '15 10:07

Harikrishnan CV


People also ask

What is horizontal scroll view in Android?

HorizontalScrollView is used to scroll the child elements or views in a horizontal direction. HorizontalScrollView only supports horizontal scrolling. For vertical scroll, android uses ScrollView.


1 Answers

LayoutManager is the class that layout views in RecyclerView. So change recyclerView.setLayoutManager(LayoutManager) if you want to change layout. In your case, if you use LinearLayoutManager, do this by calling:

LinearLayoutManager layoutManager = ...
recyclerView.setLayoutManager(layoutManager);

//when you want horizontal
layoutManager.setOrientation(context, LinearLayoutManager.HORIZONTAL);

//when you want vertical
layoutManager.setOrientation(context, LinearLayoutManager.VERTICAL);
like image 152
pjanecze Avatar answered Sep 18 '22 19:09

pjanecze