I need to get the RatingBar control to redraw .
After many problems with rating and styles, I managed to get almost everything that works.
I use it in a list item. Because of how this works, you need to βfight a littleβ with his appearance and behavior. I ended up using the solution that I found on SO, where it installs it to work as an indicator, but where it manually calculates what corresponds to a click on the evaluation panel. The code always gives the correct result when ironing the code, but the paint control itself is erroneous for the first time. Here is my code in getView "part one":
final RatingBar rating = (RatingBar)view.findViewById(R.id.listitem_catalog_rating); rating.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_UP) { float touchPositionX = event.getX(); float width = rating.getWidth(); float starsf = (touchPositionX / width); starsf = starsf * param_final__data.score_max; // 5 int starsint = (int) starsf + param_final__data.score_min; byte starsbyte = (byte) starsint; param_final__data.score_cur = starsbyte; starsf = starsint; rating.setRating(starsf); rating.setVisibility(View.INVISIBLE); // force repaint and set visible - how? } else if (event.getAction() == MotionEvent.ACTION_DOWN) { param_final__view.setPressed(true); } else if (event.getAction() == MotionEvent.ACTION_CANCEL) { param_final__view.setPressed(false); } return true; } });
The problem is that when the rating panel is initially shown, the first time you click anywhere on it, it will force itself to draw, as if everyone always chose the maximum score. However, if someone hides the control and shows it again, it draws correctly. However, this is due to the "natural delay in user interaction" - for example, by pressing a button that switches the visibility state. If I try to force the code to be redrawn using invalidate or setvisibility statements, nothing happens.
This is the "part 2" code, where I initialize the evaluation panel in getView when it is displayed:
rating.setIsIndicator(true); rating.setVisibility(View.VISIBLE); rating.setStepSize(1);
And this is his XML:
<RatingBar android:id="@+id/listitem_catalog_rating" android:layout_height="wrap_content" android:layout_width="wrap_content" android:numStars="1" android:stepSize="1.0" android:rating="1.0" style="@style/MicRatingBar" android:visibility="gone" android:layout_marginTop="10dip" android:layout_marginBottom="10dip" />
...
<style name="MicRatingBar" parent="@android:style/Widget.RatingBar"> <item name="android:progressDrawable">@drawable/micratingstars</item> <item name="android:minHeight">34dip</item> <item name="android:maxHeight">34dip</item> </style>
...
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+android:id/background" android:drawable="@drawable/star_background" /> <item android:id="@+android:id/secondaryProgress" android:drawable="@drawable/star_secondaryprogress" /> <item android:id="@+android:id/progress" android:drawable="@drawable/star_progress" /> </layer-list>
For reference, these are some of the stackoverflow threads that have reached me:
(but, unfortunately, does not solve my specific problem.)
I tried many different combinations, and in my case the code posted here is the one closest to the desired behavior and look. Just with the problem, the score is not drawn correctly on the "first show".
I tried using for example. invalidate , but I believe that its internal flags force it to ignore its cancellation request.