简体   繁体   中英

Android ListView scrolling with selected item

I have the problem mentioned in this SO post: Selected list item color moves on scrolling the listView in Android , but I don't understand how to fix this.

This is what it looks like:

在此处输入图片说明

The highlight of an item moves over another item that isn't selected (when scrolling). Sometimes the highlight is between 2 items (half and half)..

This bug also happens with default adapter (no adapter set).

This is my Adapter:

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Color;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class SimpleCheckAdapter extends ArrayAdapter<String> {

    private SparseBooleanArray mSparseBooleanArray;
    private LayoutInflater mInflater;
    private ArrayList<String> itemsArrayList;

    public SimpleCheckAdapter(Context context, ArrayList<String> a) {
        super(context,R.layout.srow,a);
        this.itemsArrayList = a;
        this.mInflater = LayoutInflater.from(context);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

      if(convertView == null){
          convertView = this.mInflater.inflate( R.layout.srow, parent, false);
      }

      // 3. Get the two text view from the rowView
        TextView txt = (TextView) convertView.findViewById(R.id.simpleText);

        // 4. Set the text for textView 
        txt.setText(itemsArrayList.get(position));


       // 5. return rowView
        return convertView;
    }
}

Who can help and know how to fix this ugly behavior?

创建 onscrolllistener 并删除滚动列表的选择。

I use that approach on most of my projects and i never found any issue with it. I even made an app involving contacts, wich manage with a considerable amount of data (given de way contact API works) and it works smoothly so my answer would be yes.

Here is a simple solution that works for me:

  1. Extend the code of your SimpleCheckAdapter:

     private int selectedPosition = -1; void setSelectedPosition( int pos ) { selectedPosition = pos; notifyDataSetChanged(); }
  2. Extend the code in GetView():

     convertView.setBackgroundColor(Color.WHITE); if ( position == selectedPosition) { convertView.setBackgroundColor(Color.LTGRAY); }
  3. In yout Activity register a ClickCallback() for the ListView

     listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View viewClicked, int position, long id) { adapter.setSelectedPosition(position); } });

To clear the selection call:

    adapter.setSelectedPosition(-1);

That's all, programatically setChoiceMode() and setSelector() or in xml android:choiceMode and android:listSelector are not needed!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM