Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I setup a custom ListView with divider using Android?

I wanted to implement Pull to Refresh feature in my android application, so I implemented this library: Android-PullToRefresh. However, I can't seem to set custom style to divide programmatically.

The code is simple:

list = (PullToRefreshListView) findViewById(R.id.list);
int[] colors = {0, 0xFF97CF4D, 0}; 
list.setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colors));
list.setDividerHeight(1);

However, it is throwing this error: The method setDivider(GradientDrawable) is undefined for the type PullToRefreshListView and The method setDividerHeight(int) is undefined for the type PullToRefreshListView.

What am I doing wrong here?

like image 944
input Avatar asked Apr 20 '13 19:04

input


1 Answers

PullToRefreshListView is not a ListView, hence that error. You should access the ListView inside PullToRefreshListView and invoke setDivider* methods on that.

list = (PullToRefreshListView) findViewById(R.id.list);
int[] colors = {0, 0xFF97CF4D, 0};
ListView inner = list.getRefreshableView();
inner.setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colors));
inner.setDividerHeight(1);

As an alternative you could define your gradient as an XML drawable and set the attributes right in your layout like shown in the sample here

eg:

<com.handmark.pulltorefresh.library.PullToRefreshListView
  android:divider="@drawable/fancy_gradient"
  android:dividerHeight="@dimen/divider_height"...
like image 58
a.bertucci Avatar answered Nov 15 '22 11:11

a.bertucci