Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 439 Vote(s) - 3.43 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can i set layoutmanager to RecycleView using kotlin

#11
You can try using below solution

val mRecyclerView= v.findViewById<RecyclerView>(R.id.rec) //id RecyclerView
mRecyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL,false)
Reply

#12
use RecyclerView.HORIZONTAL for AndroidX instead of LinearLayoutManager.HORIZONTAL


var vegetableList: RecyclerView = findViewById(R.id.list_vegetable)
vegetableList.layoutManager = LinearLayoutManager(this,
RecyclerView.HORIZONTAL, false)
Reply

#13
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][1] and [here][2].


[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#14
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) {
[email protected]!!.notifyDataSetChanged()
smoothScroller.setTargetPosition([email protected]);
dataBinding!!.rvTemplate.layoutManager!!.startSmoothScroll([email protected]);
}

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.
Reply

#15
**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)

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through