How can i set layoutmanager to RecycleView using kotlin as java code below:
mRecyclerView.setLayoutManager(mLinearLayoutManager);
To implement a horizontal recycler view, create an instance of LinearLayoutManager with orientation HORIZONTAL and assign it to the recycler view. The below code would make the Recyclerview to show the elements in HORIZONTAL orientation.
Go to app > java > package name > right-click > New > Kotlin class/file and name that file as CustomAdapter and then click on OK. After this add the code provided below. Comments are added inside the code to understand the code in more detail.
A ViewHolder describes an item view and metadata about its place within the RecyclerView. Adapter implementations should subclass ViewHolder and add fields for caching potentially expensive View#findViewById(int) results. While LayoutParams belong to the LayoutManager , ViewHolders belong to the adapter.
Following two lines sets orientation to vertical
mRecyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL ,false)
OR
mRecyclerView.layoutManager = LinearLayoutManager(this)
mRecyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL ,false)
sets horizontal orientation
To set grid layout,
mRecyclerView.layoutManager = GridLayoutManager(this, spanCount)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With