英文:
How to create a RecyclerView with horizontal scroll? - Kotlin
问题
在布局中设置这个 android:orientation="horizontal"
和 app:reverseLayout="true"
的部分不起作用。
英文:
set this
android:orientation="horizontal"
app:reverseLayout="true"
in the layout doesn't work.
答案1
得分: 1
在设置布局管理器时,可以在Activity.kt文件中这样设置:
mRecyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, true);
英文:
When setting your layout manager set it like so in your Activity.kt file:
mRecyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, true);
答案2
得分: 1
class HorizontalRecyclerViewActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_horizontal_recycler_view)
val recyclerView = findViewById<RecyclerView>(R.id.recycler_view)
recyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
recyclerView.adapter = YourAdapter()
}
}
将**RecyclerView**的**layoutManager**属性设置为**LinearLayoutManager**,其方向设置为**HORIZONTAL**。**LinearLayoutManager**构造函数的第三个参数设置了反向布局,在这种情况下为false,意味着项目将从左到右排列。
然后,将**RecyclerView**的**adapter**属性设置为**YourAdapter**的实例,它是一个自定义的适配器,为**RecyclerView**提供数据。
英文:
class HorizontalRecyclerViewActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_horizontal_recycler_view)
val recyclerView = findViewById<RecyclerView>(R.id.recycler_view)
recyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
recyclerView.adapter = YourAdapter()
}
}
The layoutManager property of the RecyclerView to a LinearLayoutManager with its orientation set to HORIZONTAL. The third argument of the LinearLayoutManager constructor sets the reverse layout, in this case false, meaning that the items will be laid out from left to right.
Then, you set the adapter property of the RecyclerView to an instance of YourAdapter, which is a custom adapter that provides data to the RecyclerView.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论