20

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

mRecyclerView.setLayoutManager(mLinearLayoutManager);
SMR
  • 6,628
  • 2
  • 35
  • 56
Daniel
  • 355
  • 2
  • 3
  • 15

14 Answers14

34

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)
Maddy
  • 4,525
  • 4
  • 33
  • 53
Kashifa
  • 406
  • 4
  • 7
14

You can use

recyclerView.layoutManager = LinearLayoutManager(context) // default orientation is vertical

// if you want horizontal recyclerview
// recyclerView.layoutManager = LinearLayoutManager(this, RecyclerView.HORIZONTAL, false)
Linh
  • 57,942
  • 23
  • 262
  • 279
3

You can try using below solution

val mRecyclerView= v.findViewById<RecyclerView>(R.id.rec) //id RecyclerView    
mRecyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL,false)
MashukKhan
  • 1,946
  • 1
  • 27
  • 46
erfanhy
  • 51
  • 1
  • 2
2

Choose the layout:

  • LinearLayoutManager(context). // vertical
  • LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false) // horizontal
  • GridLayoutManager(context, numberOfColumns) // grid

Then apply the layout using Kotlin's apply() which removes repetition.

val rv = view.findViewById(R.id.recyclerView) as RecyclerView
rv.apply {
    layoutManager = LinearLayoutManager(context)
    adapter = recyclerViewAdapter()
    setHasFixedSize(true)
    ...
}

It can also be set in XML like this:

app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"

For more info see: here and here.

Jeffrey
  • 1,998
  • 1
  • 25
  • 22
1

You can do like this

val linearLayoutManager = LinearLayoutManager(this)
linearLayoutManager.orientation = LinearLayoutManager.VERTICAL
recyclerview!!.layoutManager = linearLayoutManager
recyclerview!!.isNestedScrollingEnabled = true
recyclerview!!.setHasFixedSize(true)
ZeroOne
  • 8,996
  • 4
  • 27
  • 45
1

use RecyclerView.HORIZONTAL for AndroidX instead of LinearLayoutManager.HORIZONTAL

var vegetableList: RecyclerView = findViewById(R.id.list_vegetable)
            vegetableList.layoutManager = LinearLayoutManager(this,
            RecyclerView.HORIZONTAL, false)
Saugat Rai
  • 105
  • 1
  • 2
  • 10
0

Simply write this to set LayoutManager

 // Define this globally
 lateinit var recyclerView: RecyclerView

 // Initialize this after `activity` or `fragment` is created
 recyclerView = findViewById(R.id.recyclerView) as RecyclerView

 recyclerView.setHasFixedSize(true)
 recyclerView.layoutManager = LinearLayoutManager(activity!!) as RecyclerView.LayoutManager
Patel Pinkal
  • 8,984
  • 4
  • 28
  • 50
0

I had same issue, reason was I had initialize recyclerView as

var recyclerView = findViewById<View>(R.id.recycleView)

Make sure you initialize as below

var recyclerView = findViewById<View>(R.id.recycleView) as RecyclerView
user2661518
  • 2,677
  • 9
  • 42
  • 79
0

Apply plugin in your app build

 apply plugin: 'kotlin-android-extensions'

For my case view id of RecyclerView is my_recycler_view.

In your java file write -

my_recycler_view.layoutManager = LinearLayoutManager(context)

By default LinearLayoutManager(context) will set vertical orientation, update it as per need.

Rahul
  • 10,457
  • 4
  • 35
  • 55
0

You can set using this code:

binding.recyclerView.setHasFixedSize(true)
binding.recyclerView.layoutManager = LinearLayoutManager(this ,LinearLayoutManager.VERTICAL ,false)
binding.recyclerView.adapter = customAdapter(this ,getList())
Dhaval Jivani
  • 9,467
  • 2
  • 48
  • 42
0
private var mRecyclerView: RecyclerView? = null

mRecyclerView?.layoutManager = LinearLayoutManager(activity)

Shahbaz Ahmed
  • 459
  • 6
  • 9
0

If you are working with Kotlin android.

Declare lateinit variable smoothScroller

lateinit var smoothScroller: SmoothScroller

Within OnCreate Mehtod Initilize the smoothScroller

 override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    smoothScroller = object : LinearSmoothScroller(context) {
        override fun getVerticalSnapPreference(): Int {
            return SNAP_TO_START
        }
    }
}

Finally, Check if the Adopter is Initialized or not? Commit Old dataset changes if there are any. Set the position. Start scroller.

if (this@ChooseTemplate::genericAdapter.isInitialized && this@ChooseTemplate::smoothScroller.isInitialized) {
        this@ChooseTemplate.genericAdapter!!.notifyDataSetChanged()
        smoothScroller.setTargetPosition(this@ChooseTemplate.templatesrowIndex);
        dataBinding!!.rvTemplate.layoutManager!!.startSmoothScroll(this@ChooseTemplate.smoothScroller);
    }

dataBinding is an binding object. rvTemplate is a Recycler View. genericadapter is an adopter for rvTemplate. templatesrowIndex is the index to check which row item is currently selected.

Ali Usman
  • 71
  • 4
0

Handling Errors while Creating a layoutManager with recyclerView in mainActivity.kt

-> Add android:id="@+id/recyclerView"

-> In build.gradle(Module :app) add this line id 'kotlin-android-extensions under plugin then click on Sync Now

-> add below line in MainActivity.kt

val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
recyclerView.layoutManager = LinearLayoutManager(this)
Mayank
  • 1
  • 1
-1

recyclerView.layoutManager = LinearLayoutManager(context)

or

recyclerView.layoutManager = GridLayoutManager(context, spanCount)

Kay Saith
  • 11
  • 1