17

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

mRecyclerView.setLayoutManager(mLinearLayoutManager);
SMR
  • 6,568
  • 2
  • 34
  • 56
Daniel
  • 313
  • 2
  • 3
  • 14

13 Answers13

31

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)
Malik Motani
  • 3,835
  • 4
  • 31
  • 51
Kashifa
  • 376
  • 3
  • 7
13

You can use

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

// if you want horizontal recyclerview
// recyclerView.layoutManager = LinearLayoutManager(this, RecyclerView.HORIZONTAL, false)
Linh
  • 51,033
  • 19
  • 228
  • 256
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,842
  • 1
  • 26
  • 44
erfanhy
  • 51
  • 1
  • 2
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,559
  • 4
  • 27
  • 41
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
  • 95
  • 1
  • 1
  • 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,079
  • 4
  • 25
  • 47
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,485
  • 7
  • 34
  • 71
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,187
  • 4
  • 34
  • 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
  • 8,935
  • 2
  • 45
  • 40
0
private var mRecyclerView: RecyclerView? = null

mRecyclerView?.layoutManager = LinearLayoutManager(activity)

Shahbaz Ahmed
  • 439
  • 6
  • 9
0

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,800
  • 1
  • 21
  • 21
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
  • 31
  • 3
-1

recyclerView.layoutManager = LinearLayoutManager(context)

or

recyclerView.layoutManager = GridLayoutManager(context, spanCount)

Kay Saith
  • 11
  • 1