0

Im trying to make 2 recyclerview. And 2 of it using the same layout with Match Parent width. But the first Recyclerview display it properly but the second one doesnt.

My first Recyclerview XML :

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:gravity="center_vertical"
android:padding="10dp">

<ImageView
    android:id="@+id/ivProduct"
    android:layout_width="95dp"
    android:layout_height="95dp"
    android:scaleType="fitCenter"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:ignore="ContentDescription"
    tools:src="@drawable/ic_image" />


<TextView
    android:id="@+id/tvTitleProduct"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="12dp"
    android:fontFamily="@font/sen_bold"
    android:textColor="@color/black"
    android:textSize="15sp"
    app:layout_constraintStart_toEndOf="@id/ivProduct"
    app:layout_constraintTop_toTopOf="parent"
    tools:text="Microsoft Office 2021 Pro Plus" />

<TextView
    android:id="@+id/tvPriceProduct"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="11dp"
    android:layout_marginTop="8dp"
    android:fontFamily="@font/sen"
    android:textColor="@color/black"
    app:layout_constraintStart_toEndOf="@id/ivProduct"
    app:layout_constraintTop_toBottomOf="@id/tvTitleProduct"
    tools:text="Rp. 35.000,00" />

<com.google.android.material.button.MaterialButton
    android:id="@+id/btnEditDisplayProduct"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="12dp"
    android:layout_marginTop="8dp"
    android:backgroundTint="@color/second_color"
    android:fontFamily="sans-serif"
    android:text="Edit"
    android:textColor="@color/white"
    android:textSize="12sp"
    android:textStyle="bold"
    app:cornerRadius="5dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toEndOf="@id/ivProduct"
    app:layout_constraintTop_toBottomOf="@id/tvPriceProduct"
    tools:ignore="HardcodedText" />

<com.google.android.material.button.MaterialButton
    android:id="@+id/btnPreviewDisplayProduct"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:backgroundTint="@color/second_color"
    android:fontFamily="sans-serif"
    android:text="Preview"
    android:textColor="@color/white"
    android:textSize="11sp"
    android:textStyle="bold"
    app:cornerRadius="5dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toEndOf="@id/btnEditDisplayProduct"
    app:layout_constraintTop_toBottomOf="@id/tvPriceProduct"
    tools:ignore="HardcodedText" />
</androidx.constraintlayout.widget.ConstraintLayout>

And here is my second XML :

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_marginTop="10dp"
android:background="@color/white">

<TextView
    android:id="@+id/tvOwner"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="4dp"
    android:fontFamily="@font/roboto"
    android:textColor="@color/black"
    android:textSize="14sp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    tools:text="Buyyer\t: " />

<TextView
    android:id="@+id/tvDevice"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="4dp"
    android:layout_marginTop="4dp"
    android:fontFamily="@font/roboto"
    android:textColor="@color/black"
    android:textSize="14sp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/tvOwner"
    tools:text="Device\t: " />

<TextView
    android:id="@+id/tvType"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="4dp"
    android:layout_marginTop="4dp"
    android:fontFamily="@font/roboto"
    android:textColor="@color/black"
    android:textSize="14sp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/tvDevice"
    tools:text="Tipe\t\t\t: " />

<TextView
    app:layout_constraintHorizontal_weight="1"
    android:id="@+id/tvToken"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="4dp"
    android:layout_marginTop="4dp"
    android:fontFamily="@font/roboto"
    android:textColor="@color/black"
    android:textSize="14sp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/tvType"
    tools:text="Token\t\t:" />
</androidx.constraintlayout.widget.ConstraintLayout>

And here is my oncreateviewholder code :

class ViewHolder(val binding : LicenseRecyclerviewAdapterBinding) :
    RecyclerView.ViewHolder(binding.root)

override fun onCreateViewHolder(parent : ViewGroup, viewType : Int) : ViewHolder {
    val layoutInflater = LayoutInflater.from(parent.context)
    val binding = LicenseRecyclerviewAdapterBinding.inflate(layoutInflater)
    return ViewHolder(binding)
}

I tried to use dummyview but it still doesnt work

Recyclerview Item Screenshoot

My fragment code :

override fun onViewCreated(view : View, savedInstanceState : Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    binding = FragmentDaftarLisensiBinding.bind(view)

    val listLicense = mutableListOf<License>()
    val adapter = LicenseListAdapter(listLicense)
    binding.rvLicense.adapter = adapter
    binding.rvLicense.layoutManager = LinearLayoutManager(requireContext())
    binding.rvLicense.setHasFixedSize(true)

    viewModel.getLicenseData().observe(viewLifecycleOwner, Observer {
        it.forEach { license ->
            listLicense.add(license)
            adapter.notifyItemInserted(listLicense.size - 1)
        }
    })


}
lelestacia
  • 201
  • 2
  • 9

1 Answers1

1

If you're talking about how the widths of the ViewHolders are wrong, this:

LicenseRecyclerviewAdapterBinding.inflate(layoutInflater)

needs to be this:

LicenseRecyclerviewAdapterBinding.inflate(layoutInflater, parent, false)

You need to pass the parent View in so it do stuff like work out what match_parent means in terms of size - it needs a parent to match to when you're inflating it! The false just means it doesn't actually attach it to the parent.

This is a pretty standard call when you're inflating layouts, like when you inflate a Fragment's view in onCreateView. It's pretty rare that you don't pass the parent/container so it can work out sizing and stuff!

cactustictacs
  • 17,935
  • 2
  • 14
  • 25