Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i set layoutmanager to RecycleView using kotlin

Tags:

android

kotlin

How can i set layoutmanager to RecycleView using kotlin as java code below:

mRecyclerView.setLayoutManager(mLinearLayoutManager);
like image 565
Daniel Avatar asked Jun 13 '17 03:06

Daniel


People also ask

How do I set the recycler view to horizontal in Kotlin?

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.

How do I set data adapter in Kotlin?

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.

What is ViewHolder in Kotlin?

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.


1 Answers

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)
like image 129
Kashifa Avatar answered Oct 01 '22 11:10

Kashifa