1

I have n items in a list. i.e Item1 ,Item2,Item3..Item-n. I want to open them on the click of a button and make them visible in a horizontal manner. For Example

ITEM1 ITEM2 ITEM3......ITEM-N

How to draw the same in xml design in android. Please help to solve the same.

Gurpreet
  • 27
  • 4
  • use `recyclerview` with horizontal linear layout manager – Ajay S Oct 24 '16 at 06:57
  • Check example from the link : http://blog.nkdroidsolutions.com/android-horizontal-vertical-recyclerview-example/ – Janak Oct 24 '16 at 06:58
  • try TwoWayView lib https://github.com/lucasr/twoway-view – Brijesh Kumar Oct 24 '16 at 07:00
  • as said above use `RecyclerView` instead of `ListView`. It would be very easy to implement with RecyclerView as compared to LIstView. Check this link for implementation. http://stackoverflow.com/questions/28460300/how-to-build-a-horizontal-listview-with-recyclerview – Vivek Mishra Oct 24 '16 at 07:00
  • @Janak: I have implemented the same. But i want to open that recycleview on the click of a button and make it visible and invisible accordingly. – Gurpreet Oct 24 '16 at 07:30
  • @Gurpreet...... Yes, you are able to do same using on Click event of Button change Visibility of RecyclerView. – Janak Oct 24 '16 at 07:37
  • I have implemented the code given below. It has added a list but i want to display a list on the click of a button and then make it hide/visible according. Kindly help me getting it done. – Gurpreet Oct 25 '16 at 06:32

2 Answers2

1

Use like it

xml layout

<android.support.v7.widget.RecyclerView
     android:id="@+id/recyclerView_category"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:focusableInTouchMode="true"
     app:layout_behavior="@string/appbar_scrolling_view_behavior" />

Use in Activity class:

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView_home_10);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(MainActivity.this, LinearLayoutManager.HORIZONTAL, false);
recyclerView.setLayoutManager(layoutManager);
// If using recylerview in srollview 
recyclerView.setNestedScrollingEnabled(false);
// set Adapter
recyclerView.setAdapter(adapter);
Garg
  • 2,731
  • 2
  • 36
  • 47
0

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,false);

//when you want vertical
layoutManager.setOrientation(context,LinearLayoutManager.VERTICAL,false);