ListView Scrolling Issue: on Item Selection

后端 未结 2 1900
悲哀的现实
悲哀的现实 2021-01-25 21:26

I have Listview with which I\'m trying to display my custom Adapter.Everything works fine except when I select list item and scroll it,the items which were not selected are alre

2条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-25 21:45

    Another solution, put a simple int in your Custom list View

    public class CustomListView extends BaseAdapter {
         private int selected = 0;
    

    check the position in your getView

    public View getView(int position, View convertView, ViewGroup parent) {
       if (position == selected)
            selector.setImageResource(R.drawable.selected);
       else
            selector.setImageResource(R.drawable.not_selected);
    

    and finally in the clickListener of your activity or fragment, set the position with setter/getter method and notify the adapter.

    @Override
    public void onItemClick(AdapterView parent, View view, int position, long id) {
        customListViewAdapter.setSelectedPosition(position);
        customListViewAdapter.notifyDataSetChanged();
    }
    

    It works like a charm for me ;)

提交回复
热议问题