问题
I'm using rb.setNumStars(int) to change the stars form a given default value (say 5) to a new one with app preferences (say 4). The code performs Ok and after refresh the fragment, I can see the change in the stars number to any integer I set (the bar has a StepSize of 1).
The problem is that when I try to set the rating on that bar, the filled stars does not coincide with the rating value, and I have to touch in strange places to get come values, i.e. if I touch the fist star, I get a rating = 1, If I touch the second, I still have a rating = 1 and the filled stars are one and a fragment of the second. If I touch between the second and the third star, I get rating = 2 and I get two and a fragment filled stars.
Imagine this: if F is a full star, P is a partial (but not as in a half star) and E is an empty star, if I touch the second star, and between the 2nd and the 3rd I get:
F P E E Rating = 1 F F P E rating = 2
Edit: The images can be found here: http://1drv.ms/1pyq3gS
If I return the value to 5, the bar behaves as expected, with the filled stars coinciding with the rating.
I can not insert images, to explain this behavior, but I hope it's clear enough.
Does anybody knows why is this happening and how to correct it?
回答1:
It seems that I was able to answer my own question.
As this is an extension of the progress bar, I´m guessing that when I change the number of stars, a progress bar is stretching to the new width, but keeping it's original values, not reflecting the portion that must fill. By using setMax for the Rating Bar, I think I´m setting the new limits for this bar, so, I changed the number of stars and set the max value to that number, like this:
RatingBar rb = (RatingBar) view.findViewById(rbars[r]);
rb.setNumStars(Integer.valueOf(SP.getString(rchars[r], "3")));
rb.setMax(Integer.valueOf(SP.getString(rchars[r],"3")));
rb.setRating(2);
In this way the rating coincide with the filled stars.
来源:https://stackoverflow.com/questions/23282712/android-using-setnumstars-to-change-stars-during-execution-make-the-ratings-act