TutenStain
TutenStain

Reputation: 247

Android listview separator

I want to display a very simple separator in my listview. I have made a custom adapter that extends 'SimpleAdapter' and my implementation works somewhat. It displays the dividers and my list correct but once I start to scroll and then scroll back up the dividers gets messed up, placed instead of listitems etc. Sometime when I scroll some more it will look correct again. This is my code for 'getView'

@Override
    public View getView(int position, View convertView, ViewGroup parent){
            if(((String)items.get(position).get("name")).startsWith("-")){
                View divider = inflater.inflate(R.layout.list_separator, null);
                TextView separator = (TextView)divider.findViewById(R.id.separator);
                separator.setText(((String)items.get(position).get("name")));

                return divider;
            } else {
                return super.getView(position, convertView, parent);
            }
    }

What might be my problem?

Upvotes: 0

Views: 2939

Answers (1)

Julio_oa
Julio_oa

Reputation: 580

If your separator is very simple as you say, you have two better ways to put it in the listview:

1.Put it in the xml using android:divider attribute:

<item name="android:divider">@layout/list_separator</item>

2.Use the method setDivider() from ListView, give your layout as Drawable.:

ListView lv = ... ;
lv.setDivider(getResources().getDrawable(R.layout.list_separator));

Upvotes: 3

Related Questions