RSS

Search Engine

Tuesday, September 28, 2010

Fancy ListViews Redux: 0.9 SDK and RatingBar

You may remember way back when (e.g., July 2008) when Building ‘Droids featured a six-post series on creating fancy ListView implementations, culminating in a CheckListView widget that could be used as a drop-in replacement for ListView.

They’re ba-ack!

Specifically, today, let’s take a look at the 0.9 SDK’s impact on the Fancy ListView series, and update one of the examples to take advantage of a new widget: the RatingBar.

In many respects, the code and techniques introduced in the Fancy ListViews series are still valid and relevant in the world of the 0.9 SDK. Perhaps the biggest change is that ViewInflate became LayoutInflater, requiring some search-and-replace edits of your older M5 source code. But the concepts of supplying custom views, of recycling views using the ViewHolder/ViewWrapper pattern, and the like are still very useful, even after the 0.9 SDK.

Of course, as promised, the 0.9 SDK has its own take on having checkable ListView widgets; this will be covered in a future blog post.

For now, though, let’s revisit the last sample from the Fancy ListViews series and update it to eschew the checkbox, switching to the new RatingBar.

RatingBar is a widget designed to users to provide a rating to something, such as rating a music track or a blog post or an Android development book up on Amazon.com (*cough*). A rating is a float, from 0 to a specified maximum (android:numStars). You can specify what the granularity of the rating is (android:stepSize) and whether it is merely an indicator (android:isIndicator) or if it is a user-input element to allow users to set the rating by sliding their finger across the stars.

As with just about any moderately-sized widget, the RatingBar can be used as part of a row in a ListView. In fact, replacing the CheckBox in CheckListView with a RatingBar is fairly straight-forward:

  1. public class RateableWrapper extends AdapterWrapper {
  2. Context ctxt=null;
  3. float[] rates=null;
  4. public RateableWrapper(Context ctxt, ListAdapter delegate) {
  5. super(delegate);
  6. this.ctxt=ctxt;
  7. this.rates=new float[delegate.getCount()];
  8. for (int i=0;i
  9. this.rates[i]=2.0f;
  10. }
  11. }
  12. public View getView(int position, View convertView, ViewGroup parent) {
  13. ViewWrapper wrap=null;
  14. View row=convertView;
  15. if (convertView==null) {
  16. LinearLayout layout=new LinearLayout(ctxt);
  17. RatingBar rate=new RatingBar(ctxt);
  18. rate.setNumStars(3);
  19. rate.setStepSize(1.0f);
  20. View guts=delegate.getView(position, null, parent);
  21. layout.setOrientation(LinearLayout.HORIZONTAL);
  22. rate.setLayoutParams(new LinearLayout.LayoutParams(
  23. LinearLayout.LayoutParams.WRAP_CONTENT,
  24. LinearLayout.LayoutParams.FILL_PARENT));
  25. guts.setLayoutParams(new LinearLayout.LayoutParams(
  26. LinearLayout.LayoutParams.FILL_PARENT,
  27. LinearLayout.LayoutParams.FILL_PARENT));
  28. RatingBar.OnRatingBarChangeListener l=new RatingBar.OnRatingBarChangeListener() {
  29. public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromTouch) {
  30. rates[(Integer)ratingBar.getTag()]=rating;
  31. }
  32. };
  33. rate.setOnRatingBarChangeListener(l);
  34. layout.addView(rate);
  35. layout.addView(guts);
  36. wrap=new ViewWrapper(layout);
  37. wrap.setGuts(guts);
  38. layout.setTag(wrap);
  39. rate.setTag(new Integer(position));
  40. rate.setRating(rates[position]);
  41. row=layout;
  42. }
  43. else {
  44. wrap=(ViewWrapper)convertView.getTag();
  45. wrap.setGuts(delegate.getView(position, wrap.getGuts(), parent));
  46. wrap.getRatingBar().setTag(new Integer(position));
  47. wrap.getRatingBar().setRating(rates[position]);
  48. }
  49. return(row);
  50. }
  51. }
  • Create and configure the RatingBar when it is lazy-instantiated in getView()
  • Hook in a RatingBar.OnRatingBarChangeListener to update a float[] of ratings, rather than a boolean[] of checkbox states
  • Update the rating in the RatingBar when it is recycled
  • Rename all the classes to something more logical (e.g., CheckableWrapper becomes RateableWrapper)

Unfortunately, the stock style for RatingBar is a bit big for lists:

A sample ListView using a RatingBar in each row

You can use a ratingBarStyleSmall style to shrink the size, but then the RatingBar becomes read-only, meaning you would need some other means to let people specify the rating itself.

0 comments:

Post a Comment