I need to implement a horizontal listview in my Android application. I did a bit of research and came across How can I make a horizontal ListView in Android? and Horizontal ListView in Android?. However, these questions were asked before Recyclerview was released. Is there a better way to implement this now with Recyclerview?
To implement a horizontal recycler view, create an instance of LinearLayoutManager with orientation HORIZONTAL and assign it to the recycler view. The below code would make the Recyclerview to show the elements in HORIZONTAL orientation.
You can add an indicator by using RecyclerView. ItemDecoration. Just draw some lines or circles at the bottom and use layoutManager. findFirstVisibleItemPosition() to get the current active item.
LayoutManager implementations that lays out items in a grid. WearableLinearLayoutManager. This wear-specific implementation of LinearLayoutManager provides basic offsetting logic for updating child layout. A RecyclerView. LayoutManager implementation which provides similar functionality to ListView .
When you use a RecyclerView, you need to specify a LayoutManagerthat is responsible for laying out each item in the view. The LinearLayoutManagerallows you to specify an orientation, just like a normal LinearLayoutwould. To create a horizontal list with RecyclerView, you might do something like this:
Recycler view is more advanced version of list view and it works based on View holder design pattern. Using recycler view we can show grids and list of items. This example demonstrate about how to build a horizontal list view with Recycler View by creating a beautiful student records app that displays student name with age.
Android | Horizontal RecyclerView with Examples Last Updated: 16-12-2019 RecyclerView is a ViewGroup added to the android studio as a successor of the GridView and ListView. It is an improvement on both of them and can be found in the latest v-7 support packages.
Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Open build.gradle and add Recycler view & Card view library dependencies. Step 3 − Add the following code to res/layout/activity_main.xml.
Is there a better way to implement this now with RecyclerView now?
Yes.
When you use a RecyclerView
, you need to specify a LayoutManager
that is responsible for laying out each item in the view. The LinearLayoutManager
allows you to specify an orientation, just like a normal LinearLayout
would.
To create a horizontal list with RecyclerView
, you might do something like this:
LinearLayoutManager layoutManager = new LinearLayoutManager(requireContext(), LinearLayoutManager.HORIZONTAL, false); RecyclerView myList = (RecyclerView) findViewById(R.id.my_recycler_view); myList.setLayoutManager(layoutManager);
<android.support.v7.widget.RecyclerView android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" app:layoutManager="android.support.v7.widget.LinearLayoutManager" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With