Issue
I'm gonna add a button at the end of my recycler view and i don't know how to do it . i will appreciate you if you teach me .
this is my main activity code :
class MainActivity : AppCompatActivity() {
private var number: Int = 0
private lateinit var name: String
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val myRecyclerView = findViewById<RecyclerView>(R.id.recyclerView)
val myViewModel = ViewModel()
myRecyclerView.layoutManager = LinearLayoutManager(this)
myViewModel.getUserData()
myViewModel.myList.observe(this) {
myRecyclerView.adapter = MyAdapter(it)
}
}
and this is my adapter code :
class MyAdapter(private val myList: List<Player>) : RecyclerView.Adapter<MyHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyHolder {
val myLayoutInflater = LayoutInflater.from(parent.context)
val myView = myLayoutInflater.inflate(R.layout.view_for_rv, parent, false)
return MyHolder(myView)
}
override fun onBindViewHolder(holder: MyHolder, position: Int) {
val item = myList[position]
holder.bind(item)
}
override fun getItemCount(): Int {
return myList.size
}
and my holder class :
class MyHolder(view: View) : RecyclerView.ViewHolder(view) {
private val playerNumber = view.findViewById<TextView>(R.id.tvNumber)
private val playerName = view.findViewById<TextView>(R.id.tvName)
fun bind(item : Player){
playerName.text=item.name
playerNumber.text=item.number.toString()
}
Solution
You will achieve this by adding the RecyclerView
in a parent NestedScrollView
and then place the Button
below the RecyclerView
.
So your code should look like this
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
NOTE
Add
recyclerView.nestedScrollingEnabled = false
for scroll to work for sdk 21
Answered By - Kartik
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.