How can i set layoutmanager to RecycleView using kotlin as java code below:
mRecyclerView.setLayoutManager(mLinearLayoutManager);
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)
You can use
recyclerView.layoutManager = LinearLayoutManager(context) // default orientation is vertical
// if you want horizontal recyclerview
// recyclerView.layoutManager = LinearLayoutManager(this, RecyclerView.HORIZONTAL, false)
You can try using below solution
val mRecyclerView= v.findViewById<RecyclerView>(R.id.rec) //id RecyclerView
mRecyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL,false)
Choose the layout:
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"
You can do like this
val linearLayoutManager = LinearLayoutManager(this)
linearLayoutManager.orientation = LinearLayoutManager.VERTICAL
recyclerview!!.layoutManager = linearLayoutManager
recyclerview!!.isNestedScrollingEnabled = true
recyclerview!!.setHasFixedSize(true)
use RecyclerView.HORIZONTAL for AndroidX instead of LinearLayoutManager.HORIZONTAL
var vegetableList: RecyclerView = findViewById(R.id.list_vegetable)
vegetableList.layoutManager = LinearLayoutManager(this,
RecyclerView.HORIZONTAL, false)
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
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
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.
You can set using this code:
binding.recyclerView.setHasFixedSize(true)
binding.recyclerView.layoutManager = LinearLayoutManager(this ,LinearLayoutManager.VERTICAL ,false)
binding.recyclerView.adapter = customAdapter(this ,getList())
private var mRecyclerView: RecyclerView? = null
mRecyclerView?.layoutManager = LinearLayoutManager(activity)
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.
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)
recyclerView.layoutManager = LinearLayoutManager(context)
or
recyclerView.layoutManager = GridLayoutManager(context, spanCount)