Adding N number of textview dynamically inside view holder item while the recyclerview is setting up

44 Views Asked by At

In this part of my app, I am trying to add n (number) of textview based on number of orders to the linearlayout inside recycler view item, but the problem is textviews some times not showing, and I think the cause of that may be because the text views bing adding in the loop while the recycler view item creating

this my order item layout

<?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"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="4dp"
    android:layout_marginBottom="4dp"
    android:background="@color/white"
    android:elevation="3dp">


    <LinearLayout
        android:id="@+id/productNamesContainer"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:orientation="vertical"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">


    </LinearLayout>

    <!-- Product names will be dynamically added here -->

    <com.google.android.material.imageview.ShapeableImageView
        android:id="@+id/order_indicator"
        android:layout_width="@dimen/_7sdp"
        android:layout_height="@dimen/_7sdp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="16dp"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/productNamesContainer"
        app:shapeAppearanceOverlay="@style/circleImageViewStyle"
        app:srcCompat="@drawable/ellipse12"
        app:tint="@color/success" />

    <TextView
        android:id="@+id/order_delivered_date"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="16dp"
        android:text="Delivered on Mon, 17th Jan 2017"
        android:textColor="@color/dimgrey"
        android:textSize="@dimen/_12ssp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toEndOf="@+id/order_indicator"
        app:layout_constraintTop_toBottomOf="@+id/productNamesContainer" />

</androidx.constraintlayout.widget.ConstraintLayout>

this MyOrderAdapter class, inside the viewHolder bind method I loop on the product list and create TextView for each order name then added it to productNamesContainer the container linearlayout

class MyOrderAdapter : RecyclerView.Adapter<MyOrderAdapter.ViewHolder>() {
    


    private val diffCallback = object : DiffUtil.ItemCallback<MyOrderItemModel>() {
        override fun areItemsTheSame(
            oldItem: MyOrderItemModel,
            newItem: MyOrderItemModel
        ): Boolean {
            return oldItem.orderID == newItem.orderID
        }

        override fun areContentsTheSame(
            oldItem: MyOrderItemModel,
            newItem: MyOrderItemModel
        ): Boolean {
            return oldItem == newItem
        }

    }

    val asyncListDiffer = AsyncListDiffer(this, diffCallback)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val myOrderItemBinding: MyOrderItemBinding = MyOrderItemBinding.inflate(
            LayoutInflater.from(parent.context), parent, false
        )
        return ViewHolder(myOrderItemBinding)
    }
    

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val itemModel: MyOrderItemModel = asyncListDiffer.currentList[position]
        holder.bind(itemModel)
    }

    override fun getItemCount(): Int {
        return asyncListDiffer.currentList.size
    }

    inner class ViewHolder(private val binding: MyOrderItemBinding) :
        RecyclerView.ViewHolder(binding.root) {


        fun bind(myOrderItemModel: MyOrderItemModel) {

            val layoutParams = LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT
            )

// Set margin values in pixels
            val marginStart = binding.root.resources.getDimensionPixelSize(R.dimen.my_orders_text_views_margin_start)
            val marginTop = binding.root.resources.getDimensionPixelSize(R.dimen.my_orders_text_views_margin_top)
            val marginEnd = binding.root.resources.getDimensionPixelSize(R.dimen.my_orders_text_views_margin_end)
            val marginBottom = binding.root.resources.getDimensionPixelSize(R.dimen.my_orders_text_views_margin_bottom)

                 // Loop through the product list
            for (product in myOrderItemModel.productList) {
                val productNameTextView = TextView(binding.root.context).apply {
                    text = product.productName
                    ellipsize = TextUtils.TruncateAt.END
                    maxLines = 2
                    setTextColor(ContextCompat.getColor(context, R.color.black))
                    setTextSize(TypedValue.COMPLEX_UNIT_PX, resources.getDimension(R.dimen._12ssp))
                    setTypeface(typeface, Typeface.BOLD)
                    layoutParams.setMargins(marginStart, marginTop, marginEnd, marginBottom)
                }
                binding.productNamesContainer.addView(productNameTextView, layoutParams)
            }

            val date: Date? = when (myOrderItemModel.orderStatus) {
                "ORDERED" -> myOrderItemModel.orderDate!!
                "PACKED" -> myOrderItemModel.packedDate!!
                "SHIPPED" -> myOrderItemModel.shippedDate!!
                "DELIVERED" -> myOrderItemModel.deliveredDate!!
                "CANCELLED" -> myOrderItemModel.cancelledDate!!
                else -> {
                    null
                }
            }

            val simpleDateFormat = SimpleDateFormat("EEE, d MMM yyyy", Locale.ENGLISH)

            val formattedDate = simpleDateFormat.format(date!!)


            binding.orderDeliveredDate.text = "${myOrderItemModel.orderStatus} on " + formattedDate

            if (myOrderItemModel.orderStatus == "CANCELLED") {
                binding.orderIndicator.imageTintList =
                    ColorStateList.valueOf(
                        ContextCompat.getColor(
                            binding.root.context,
                            R.color.btnRed
                        )
                    )

            } else {
                binding.orderIndicator.imageTintList =
                    ColorStateList.valueOf(
                        ContextCompat.getColor(
                            binding.root.context,
                            R.color.success
                        )
                    )
            }
//            binding.ratingBar.rating = myOrderItemModel.productRating



            binding.root.setOnClickListener { view ->
                findNavController(view).navigate(
                    MyOrdersFragmentDirections.actionNavMyOrdersToOrderDetailsFragment(
                        myOrderItemModel
                    )
                )
            }
        }


    }
}

the result

enter image description here

0

There are 0 best solutions below