Rating Cleared onClick item Recyclerview
ahmaadyunus opened this issue · 1 comments
Haloo,
I have problem wite scaleratingbar. In my case I set ratingNumStar according position-- (if position 0 numStar = 5, position 1 numStart =4). But, when I clicked item it was cleared the rating to. I don't want in. I want the rating stil freeze. what should I do ?
Here's the code
`public class PopularityAdapter extends RecyclerView.Adapter<PopularityAdapter.PopularityViewHolder> {
private PopularityClicked listener;
private Context context;
private List popularityList;
public PopularityAdapter(Context context, PopularityClicked listener) {
this.listener = listener;
this.context = context;
popularityList = new ArrayList<>();
for (int i = 5; i > 0; i--) {
popularityList.add(new Popularity(false, i));
}
}
public void setSelected(int position) {
for (int i = 0; i < 5; i++) {
if (i <= position) {
popularityList.get(i).setSelected(true);
} else {
popularityList.get(i).setSelected(false);
}
}
notifyDataSetChanged();
}
public List<Popularity> getData() {
return popularityList;
}
@Override
public PopularityViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_popularity, parent, false);
return new PopularityViewHolder(view, listener);
}
@Override
public void onBindViewHolder(PopularityViewHolder holder, int position) {
Popularity popularity = popularityList.get(position);
holder.bindData(context, position, popularity);
}
@Override
public int getItemCount() {
return popularityList.size();
}
public static class PopularityViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
@BindView(R.id.rating_popularity)
ScaleRatingBar ratingBar;
@BindView(R.id.img_check_popularity)
ImageView imgCheckPopularity;
private PopularityClicked listener;
private Popularity popularity;
private int position;
public PopularityViewHolder(View itemView, PopularityClicked listener) {
super(itemView);
ButterKnife.bind(this, itemView);
this.listener = listener;
itemView.setOnClickListener(this);
ratingBar.setIsIndicator(false);
ratingBar.setClickable(false);
ratingBar.setScrollable(false);
ratingBar.setClearRatingEnabled(false);
}
public void bindData(Context context, int position, Popularity popularity) {
this.popularity = popularity;
this.position = position;
ratingBar.setNumStars(popularity.getValue());
ratingBar.setRating(popularity.getValue());
if (popularity.isSelected()) {
imgCheckPopularity.setVisibility(View.VISIBLE);
} else {
imgCheckPopularity.setVisibility(View.GONE);
}
}
@Override
public void onClick(View v) {
listener.onPopularitySelected(popularity, position);
}
}
}
`
Hello @ahmaadyunus
I copy your logic in my demo code and seems work fine.
Maybe you should check your code & xml and make sure you are processing the correct object,
or you take a look with my demo code and try to modify it to what you want.
BTW, you only need to set the attribute isIndicator
to true to achieve this feature.