如何使用 RecyclerView 构建水平 ListView [英] How to build a horizontal ListView with RecyclerView
问题描述
我需要在我的 Android 应用程序中实现一个水平列表视图.我做了一些研究,发现了怎么能我在 Android 中制作了水平 ListView? 和 Android 中的水平 ListView?.然而,这些问题都是在 Recyclerview 发布之前提出的.现在使用 Recyclerview 有没有更好的方法来实现这一点?
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?
推荐答案
现在使用 RecyclerView 是否有更好的方法来实现这一点?
Is there a better way to implement this now with RecyclerView now?
是的.
当您使用 RecyclerView
时,您需要指定一个 LayoutManager
负责布局视图中的每个项目.LinearLayoutManager
允许您指定方向,就像普通的 LinearLayout
一样.
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.
要使用 RecyclerView
创建水平列表,您可以执行以下操作:
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);
这篇关于如何使用 RecyclerView 构建水平 ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!