Я так понимаю проблема с отображением recyclerView, и в интернете все предлагают подключить старую библиотеку. Вот у меня с новой библиотекой. Сам код на Kotlin
RecyclerView крутится в фрагменте
build gradle:
implementation 'androidx.recyclerview:recyclerview:1.1.0'
xml фрагмента:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/constraintLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
код в фрагменте:
import androidx.recyclerview.widget.RecyclerView
val recyclerView: RecyclerView = view.findViewById(R.id.recyclerView1)
recyclerView.layoutManager = LinearLayoutManager(activity)
recyclerView.adapter = MyAdapter(list)
Адаптер в фрагменте:
class MyAdapter(private val values: MutableList<String>) : RecyclerView.Adapter<Adapter.ViewHolder>() {
override fun getItemCount() = values.size
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val itemView = LayoutInflater.from(parent.context).inflate(R.layout.list_two, parent, false)
return ViewHolder(itemView)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.textView?.text = values[position].toString()
holder.textView?.setOnClickListener {
Toast.makeText(it.context, values[position], Toast.LENGTH_LONG).show()
}
}
class ViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView!!) {
var textView: TextView? = null
init {
textView = itemView?.findViewById(R.id.textView4)
}
}
}
list_two
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageView2"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_weight="1"
android:maxWidth="40dp"
android:maxHeight="40dp"
android:minWidth="40dp"
android:minHeight="40dp"
app:srcCompat="@drawable/temporary" />
<TextView
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
android:layout_weight="100"
android:text="TextView"
android:textColor="#000000"
android:textSize="16sp" />
</LinearLayout>