Android listview separator

你说的曾经没有我的故事 提交于 2019-12-08 14:48:54

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!