How to create a RecyclerView with horizontal scroll? - Kotlin

329 views Asked by At

set this android:orientation="horizontal" app:reverseLayout="true" in the layout doesn't work.

2

There are 2 answers

0
ToxicFlame427 On BEST ANSWER

When setting your layout manager set it like so in your Activity.kt file:

mRecyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, true);
0
Duilio Caicedo On
    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.