0

As the official material.io documentation says:

Chips are compact elements that represent an input, attribute, or action.

I want to add chips as attributes (tags) for any blog post, a recyclerview is used to show the list of blog posts:

item_post.xml

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/mcv_container_item_blog"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:focusable="true"
    android:clickable="true"
    android:background="?selectableItemBackground"
    app:cardUseCompatPadding="true">

    <androidx.appcompat.widget.LinearLayoutCompat
        android:id="@+id/llc_container_item_project"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="@dimen/item_margin"
        android:orientation="vertical">

        <androidx.appcompat.widget.AppCompatImageView
            android:id="@+id/aciv_item_post_image"
            android:layout_width="match_parent"
            android:layout_height="@dimen/item_project_Image_size"
            android:scaleType="centerCrop"
            android:adjustViewBounds="true"
            app:srcCompat="@drawable/drawer_bg"/>

        <com.google.android.material.textview.MaterialTextView
            android:id="@+id/mtv_item_post_title"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="@dimen/item_margin"
            android:textAppearance="@style/TextAppearance.AppCompat.Large"
            android:textColorHighlight="@color/design_default_color_background"
            android:text="Chameleon UI Kit" />

        <com.google.android.material.textview.MaterialTextView
            android:id="@+id/mtv_item_post_date"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:textColor="@color/red_lighten"
            android:layout_marginTop="@dimen/user_name_margin_start"
            android:layout_marginEnd="@dimen/user_name_margin_start"
            android:layout_marginBottom="@dimen/user_name_margin_start"
            android:text="10 Sept"/>

        <com.google.android.material.textview.MaterialTextView
            android:id="@+id/mtv_item_post_short_description"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="@style/TextAppearance.AppCompat.Small"
            android:text="@string/about_me" />

        <include layout="@layout/item_divider"/>

        <HorizontalScrollView
            android:id="@+id/hsv_item_post_tags"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scrollbars="none">

            <com.google.android.material.chip.ChipGroup
                android:id="@+id/cg_item_post_tags"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:singleLine="true"/>

        </HorizontalScrollView>

    </androidx.appcompat.widget.LinearLayoutCompat>

</com.google.android.material.card.MaterialCardView>

then the PostAdapter class:

class PostAdapter(
    private val postList: List<Post>
): RecyclerView.Adapter<PostAdapter.PostViewHolder>()  {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PostViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_post, parent, false)
        return PostViewHolder(view)
    }

    override fun onBindViewHolder(holder: PostViewHolder, position: Int) {
        val post = postList[position]

        holder.postTitle.text = post.postTitle
        holder.postDescription.text = post.postDescription
        holder.postDate.text = post.postDate

        val tagChip = Chip(holder.itemView.context).apply {
            id = View.generateViewId()
            text = "label"
        }

        holder.postTagsChipGroup.addView(tagChip)

    }

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

    inner class PostViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        val postTitle: MaterialTextView = itemView.findViewById(R.id.mtv_item_post_title)
        val postDescription: MaterialTextView = itemView.findViewById(R.id.mtv_item_post_short_description)
        val postTagsChipGroup: ChipGroup = itemView.findViewById(R.id.cg_item_post_tags)
        val postDate: MaterialTextView = itemView.findViewById(R.id.mtv_item_post_date)

    }

}

at this stage a weird thing happens, when I 'scroll recyclerview' other Chips are added from nowhere as you can see this behavior in gif below:

how to avoid such behavior, what changes should I make in adapters onBindViewHolder method?

Arshad Ali
  • 3,082
  • 12
  • 56
  • 99
  • 2
    `onBindViewHolder` will be called multiple times and it will be using same recycled view again afterwards . So before calling `addView()` you should call `holder.postTagsChipGroup.removeAllViews()`. – ADM Sep 11 '21 at 07:39
  • @arshad ali Did you try the commented solution? – Dev Aug 09 '22 at 13:22

0 Answers0