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

1

5

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?

Clytemnestra answered 20/4, 2013 at 19:34 Comment(0)
D
8

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"...
Dispensary answered 20/4, 2013 at 19:44 Comment(4)
It gives the error: Type mismatch: cannot convert from ListView to PullToRefreshListViewClytemnestra
check the code above: you should invoke getRefreshableView() on your PullToRefreshListView. Casting to ListView is useless in this case. I'll get rid of that in my answer.Dispensary
The code didn't work. I ended up styling the divider with an XML drawable. Thanks for the help! :)Clytemnestra
XML-based solution is working out-of-the-box. But I'm sure of the code-based solution as well. I suppose your list reference is a PullToRefreshListView, while in the modified snippet I assumed that as a ListView one. I'll slightly modify my answer in order to recover this issue.Dispensary

© 2022 - 2024 — McMap. All rights reserved.