Включение в recycler view фрагмента (item)
Имеется отдельный фрагмент с Recycler View. Необходимо "включить (include)" в recycler view некоторые итемы, чтобы он их отображал. Как правильно это можно написать? Ниже код fragment'a с RecyclerView:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.example.android.CenteredToolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/toolbarNotes"
android:background="@color/formRecyclerViewInMainRegistration" />
</RelativeLayout>
Ниже фрагмент item, который должен отображаться в фрагменте
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/rlItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_notes_pick"
android:elevation="20dp">
<ImageView
android:id="@+id/ivBtnPlay"
android:layout_width="28dp"
android:layout_height="28dp"
android:layout_marginLeft="42dp"
android:layout_centerVertical="true"
android:clickable="true"
android:focusable="true"
android:src="@drawable/ic_play_button">
</ImageView>
</RelativeLayout>
</LinearLayout>
Ответы (1 шт):
Автор решения: bigmeco
→ Ссылка
Вот пример адаптера в котором можно произвести заполнение элементами, примеров вариаций в сети много.
class CustomAdapter(val userList: ArrayList<User>) : RecyclerView.Adapter<CustomAdapter.ViewHolder>() {
//this method is returning the view for each item in the list
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomAdapter.ViewHolder {
val v = LayoutInflater.from(parent.context).inflate(R.layout.list_layout, parent, false)
return ViewHolder(v)
}
//this method is binding the data on the list
override fun onBindViewHolder(holder: CustomAdapter.ViewHolder, position: Int) {
holder.bindItems(userList[position])
}
//this method is giving the size of the list
override fun getItemCount(): Int {
return userList.size
}
//the class is hodling the list view
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bindItems(user: User) {
val textViewName = itemView.findViewById(R.id.textViewUsername) as TextView
val textViewAddress = itemView.findViewById(R.id.textViewAddress) as TextView
textViewName.text = user.name
textViewAddress.text = user.address
}
} }