williamyyu / SimpleRatingBar

A simple RatingBar that you can easier to customize image and animations
MIT License
1.37k stars 152 forks source link

Issue with recyclerView #59

Closed Mina-Mikhail closed 6 years ago

Mina-Mikhail commented 6 years ago

I have an issue when use this library inside recyclerView.

This is my item: `<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:orientation="vertical"

<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?android:selectableItemBackground" android:clickable="true" android:focusable="true" android:orientation="horizontal" android:padding="16dp"

<com.makeramen.roundedimageview.RoundedImageView
    android:id="@+id/iv_user_image"
    android:layout_width="55dp"
    android:layout_height="55dp"
    android:layout_marginEnd="16dp"
    android:scaleType="centerCrop"
    android:src="@drawable/ic_user_round"
    app:riv_border_color="@color/colorPrimaryDark"
    app:riv_border_width="2dp"
    app:riv_mutate_background="true"
    app:riv_oval="true"
    app:riv_tile_mode="clamp"
    />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    >

  <TextView
      android:id="@+id/tv_username"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Username"
      android:textColor="@color/black"
      android:textSize="14sp"
      style="@style/TextBoldFont"
      />

  <com.willy.ratingbar.ScaleRatingBar
      android:id="@+id/place_rate_amount"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      app:srb_clickable="false"
      app:srb_drawableEmpty="@drawable/ic_not_rated"
      app:srb_drawableFilled="@drawable/ic_top_rated"
      app:srb_isIndicator="true"
      app:srb_scrollable="false"
      app:srb_starHeight="15dp"
      app:srb_starPadding="3dp"
      app:srb_starWidth="15dp"
      app:srb_stepSize="1"
      app:srb_clearRatingEnabled="false"
      />

  <TextView
      android:id="@+id/tv_review"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Good Restaurant"
      android:textColor="@color/gray"
      android:textSize="12sp"
      />

</LinearLayout>

<View android:layout_width="match_parent" android:layout_height="1dp" android:alpha="0.4" android:background="@color/gray" />

`

and this is my adapter: `public void onBindViewHolder(CustomViewHolder holder, int i) { holder.placeRateAmount.setNumStars(5); holder.placeRateAmount.setRating(arrayDetails.get(i).getRate());

holder.placeRateAmount.setIsIndicator(true);
holder.placeRateAmount.setClickable(false);
holder.placeRateAmount.setScrollable(false);
holder.placeRateAmount.setClearRatingEnabled(false);

}`

When scroll on recyclerView and return back to item 1, the rating bar was cleared !! how to solve this issue ?!!

williamyyu commented 6 years ago

Hello @Mina-Mikhail, You need to handle the onRatingChange event to update your list(arrayDetails) value, for example:

holder.ratingBar.setOnRatingChangeListener(new BaseRatingBar.OnRatingChangeListener() {
            @Override
            public void onRatingChange(BaseRatingBar ratingBar, float rating) {
                list.remove(holder.getAdapterPosition());
                list.add(holder.getAdapterPosition(), (int) rating);
            }
        });

if still can't solve this problem, please let me know, thanks!

Mina-Mikhail commented 6 years ago

Hello @ome450901 I think i didn't need to implement onRatingChange , because i use rating bar to show rate amount only, and it not Clickable ?? why use onRatingChange

Mina-Mikhail commented 6 years ago

37808086_1825454057546907_258650395218280448_n

This is the case, i use the library to display amount of rate only, the first comment have 1 star and second have 3 stars, when i scroll down to see other comments and back again to top , the stars was cleared. and first , second other comments have no stars (not rated) !!

williamyyu commented 6 years ago

Sorry, I misunderstood the problem. I find out the problem is holder.placeRateAmount.setNumStars(5); because it will remove all views and regenerate it, so the rating will be cleared.

seems your Num of Stars won't change at all, so I think you should set this value in XML not in the onBindViewHolder.

just modify your xml to the following code and remove holder.placeRateAmount.setNumStars(5);:

  <com.willy.ratingbar.ScaleRatingBar
      android:id="@+id/place_rate_amount"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      app:srb_clickable="false"
      app:srb_drawableEmpty="@drawable/ic_not_rated"
      app:srb_drawableFilled="@drawable/ic_top_rated"
      app:srb_isIndicator="true"
      app:srb_scrollable="false"
      app:srb_starHeight="15dp"
      app:srb_starPadding="3dp"
      app:srb_starWidth="15dp"
      app:srb_stepSize="1"
      app:srb_clearRatingEnabled="false"
      app:srb_numStars="5"
      />