3

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:

enter image description here

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?

Community
  • 1
  • 1
lite
  • 31
  • 5

3 Answers3

0

create onscrollistener and remove the selection of the list on scroll.

Amrut
  • 543
  • 4
  • 8
  • But i want to keep the selection, if i scroll up again, is this possible? Thank you! – lite Aug 07 '14 at 20:47
  • Yes in that case you will need to remember the selection and there are different methods which provides the first and last displayed items index. Using that you will know if user has scrolled back up again which you can add selection back. – Amrut Aug 07 '14 at 20:48
  • How can i check an item correct? singleCheckView.setItemChecked(1, true); seems not to work for me (in MainActivity in OnScrollListener and singleCheckView is a ListView) – lite Aug 07 '14 at 21:01
0

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.

user3497504
  • 106
  • 1
  • 10
0

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!

Cor
  • 389
  • 1
  • 2
  • 14