Can anyone help me, the problem is in the android display, why doesn't it appear, is there something wrong in my code, even though I have used recyleview correctly, can someone help me? the problem is I am so troubled why the recyleview doesn't appear on the Android screen
HomeFragment.kt
class HomeFragment : Fragment() {
private lateinit var homeViewModel: HomeViewModel
companion object {
@JvmStatic
fun newInstance() =
HomeFragment().apply {
arguments = Bundle().apply {
// putInt(ARG_COLUMN_COUNT, columnCount)
}
}
}
private lateinit var adapter: MyAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
// columnCount = it.getInt(ARG_COLUMN_COUNT)
}
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
homeViewModel =
ViewModelProviders.of(this).get(HomeViewModel::class.java)
val root = inflater.inflate(R.layout.fragment_home, container, false)
val linearLayout: LinearLayout = root.findViewById(R.id.linear_home)
homeViewModel.text.observe(viewLifecycleOwner, Observer {
linearLayout.background
})
return root
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
val items = listOf(
Model("Premature optimization is the root of all evil", "blalalal", R.drawable.film_1),
Model(
"Any sufficiently advanced technology is indistinguishable from magic.",
"Arthur C. Clarke",
R.drawable.film_1
),
Model("Content 01", "Source", R.drawable.film_1),
Model("Content 02", "Source", R.drawable.film_1),
Model("Content 03", "Source", R.drawable.film_1),
Model("Content 04", "Source", R.drawable.film_1),
Model("Content 05", "Source", R.drawable.film_1)
)
adapter = MyAdapter()
adapter.replaceItems(items)
recylerViewFilm.adapter = adapter
}
class MyAdapter : RecyclerView.Adapter<MyAdapter.ViewHolder>() {
private var items = listOf<Model>()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.row_film, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val item = items[position]
holder.titleTv.text = item.title
holder.descriptionTv.text = item.desc
holder.imageTv.setImageResource(item.image)
}
fun replaceItems(items: List<Model>) {
this.items = items
notifyDataSetChanged()
}
override fun getItemCount(): Int = items.size
inner class ViewHolder(override val containerView: View) :
RecyclerView.ViewHolder(containerView),
LayoutContainer
}
}
fragment_home.xml
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recylerViewFilm"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</androidx.recyclerview.widget.RecyclerView>
row_film.xml
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp">
<ImageView
android:id="@+id/imageTv"
android:layout_width="150dp"
android:layout_height="120dp"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/titleTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_toEndOf="@id/imageTv"
android:layout_toRightOf="@id/imageTv"
android:text="Title"
android:textColor="@color/black"
android:textSize="22sp"
android:textStyle="bold" />
<TextView
android:id="@+id/descriptionTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_toEndOf="@id/imageTv"
android:layout_toRightOf="@id/imageTv"
android:text="Description"
android:textColor="@color/black"
android:textSize="18sp"
android:layout_below="@id/titleTv"/>
</RelativeLayout>