如何创建一个具有水平滚动的RecyclerView? – Kotlin

huangapple go评论50阅读模式
英文:

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&lt;RecyclerView&gt;(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.

huangapple
  • 本文由 发表于 2023年2月10日 02:37:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75403035.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定