问题
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?
回答1:
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));
来源:https://stackoverflow.com/questions/17665984/android-listview-separator