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?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
If you wish to use the Horizontal Recycler View to act as a ViewPager then it's possible now with the help of
LinearSnapHelper
which is added in Support Library version 24.2.0.Firstly Add RecyclerView to your Activity/Fragment
In my case I have used a
CardView
inside theRecyclerView
blog_row.xml
In your Activity/Fragment
Last Step is to set adapter to RecyclerView
There is a RecyclerView subclass named HorizontalGridView you can use it to have horizontal direction. VerticalGridView for vertical direction
Complete example
The only real difference between a vertical
RecyclerView
and a horizontal one is how you set up theLinearLayoutManager
. Here is the code snippet. The full example is below.This fuller example is modeled after my vertical
RecyclerView
answer.Update Gradle dependencies
Make sure the following dependencies are in your app
gradle.build
file:You can update the version numbers to whatever is the most current.
Create activity layout
Add the
RecyclerView
to your xml layout.activity_main.xml
Create item layout
Each item in our
RecyclerView
is going to have a single a coloredView
over aTextView
. Create a new layout resource file.recyclerview_item.xml
Create the adapter
The
RecyclerView
needs an adapter to populate the views in each row (horizontal item) with your data. Create a new java file.MyRecyclerViewAdapter.java
Notes
ListViews
and is a common need. You can remove this code if you don't need it.Initialize RecyclerView in Activity
Add the following code to your main activity.
MainActivity.java
Notes
ItemClickListener
that we defined in our adapter. This allows us to handle item click events inonItemClick
.Finished
That's it. You should be able to run your project now and get something similar to the image at the top.
Notes
Try this:
only in case you got a recycler view with some fragments on it.
With the release of RecyclerView library, now you can align a list of images bind with text easily. You can use LinearLayoutManager to specify the direction in which you would like to orient your list, either vertical or horizontal as shown below.
You can download a full working demo from this post
Yes.
When you use a
RecyclerView
, you need to specify aLayoutManager
that is responsible for laying out each item in the view. TheLinearLayoutManager
allows you to specify an orientation, just like a normalLinearLayout
would.To create a horizontal list with
RecyclerView
, you might do something like this: