I have created a RecyclerView with some components inside it. Everything works normally, and I think I have written everything properly, but some items have different size although all the data inside this rows are similar, but different in size. Here is my code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_height="40dp">
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="@color/label_text_grey"
/>
<LinearLayout
android:gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingHorizontal="16dp"
>
<TextView
android:id="@+id/cargo"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="Alma"
android:layout_gravity="center_vertical"
android:textColor="@color/label_text_heavy_black"
android:fontFamily="@font/main_helvetica_neue_medium"
/>
<TextView
android:id="@+id/metric"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="Ton"
android:layout_gravity="center_vertical"
android:gravity="center"
android:textColor="@color/label_text_heavy_black"
android:fontFamily="@font/main_helvetica_neue_medium"
/>
<TextView
android:id="@+id/quantity"
android:layout_weight="1"
android:gravity="center"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="10000"
android:layout_gravity="center_vertical"
android:textColor="@color/label_text_heavy_black"
android:fontFamily="@font/main_helvetica_neue_medium"
/>
<ImageView
android:id="@+id/remove"
android:layout_weight="0.3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:scaleType="center"
android:paddingVertical="10dp"
android:layout_gravity="center_vertical"
android:foregroundGravity="center"
android:src="@drawable/ic_close"
app:tint="@color/close_icon_red"
/>
</LinearLayout>
</LinearLayout>
And here is my Fragment:
class AddItemsFragment(val truck: Truck) : BaseFragment() {
private lateinit var binding: FragmentAddItemsBinding
private lateinit var items: MutableList<AddedItem>
private lateinit var adapter: AddedItemsAdapter
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = FragmentAddItemsBinding.inflate(inflater, container, false)
binding.documentNo.text = truck.documentNo
val formatter = SimpleDateFormat("dd.MM.yyyy HH:mm:ss")
val date = Date()
binding.date.text = formatter.format(date)
items = listOf(
AddedItem("Alma", Metric("Ton", 3)),
AddedItem("Armud", Metric("Sentner", 11)),
AddedItem("Heyva", Metric("Sentner", 1)),
AddedItem("Nar", Metric("KG", 980)),
AddedItem("Pomidor", Metric("Ton", 2)),
AddedItem("Pomidor", Metric("Ton", 2)),
AddedItem("Pomidor", Metric("Ton", 2)),
AddedItem("Kartof", Metric("Ton", 1)),
AddedItem("Kartof", Metric("Ton", 1)),
AddedItem("Pomidor", Metric("Ton", 1)),
AddedItem("Pomidor", Metric("Ton", 1)),
AddedItem("Kartof", Metric("Ton", 1)),
AddedItem("Kartof", Metric("Ton", 1))
) as MutableList<AddedItem>
val sampleMetrics = listOf(
Metric("KG", 1),
Metric("SENTNER", 100),
Metric("TON", 1000)
)
adapter = AddedItemsAdapter(requireContext(), items)
binding.items.adapter = adapter
binding.metricItems.setAdapter(
ArrayAdapter(
requireContext(),
android.R.layout.simple_spinner_dropdown_item,
sampleMetrics.map {
it.name
})
)
binding.scanIcon.setOnClickListener {
//TODO open the scanner
Toast.makeText(requireContext(), "clicked!", Toast.LENGTH_SHORT).show()
}
return binding.root
}
@Subscribe(threadMode = ThreadMode.MAIN)
fun onAddedItemRemovedEvent(event: AddedItemRemovedEvent){
items.removeAt(event.position)
adapter.itemRemoved(items, event.position)
}
}
And here is my Adapter:
class AddedItemsAdapter(val context: Context, var items: MutableList<AddedItem>): RecyclerView.Adapter<AddedItemsViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AddedItemsViewHolder {
return AddedItemsViewHolder(ListitemAddedItemsBinding.inflate(LayoutInflater.from(context)))
}
override fun onBindViewHolder(holder: AddedItemsViewHolder, position: Int) {
val item = items[position]
holder.cargo.text = item.cargo
holder.metric.text = item.metric.name
holder.quantity.text = item.metric.size.toString()
holder.remove.setOnClickListener {
EventBus.getDefault().post(AddedItemRemovedEvent(position))
}
}
override fun getItemCount(): Int {
return items.size
}
fun itemRemoved(newItems: MutableList<AddedItem>, position: Int){
items = newItems
notifyItemRemoved(position)
}
}
Preview on phone