Puja Surya
Puja Surya

Reputation: 1287

Listview Highlight move when scrolling

this is the activity

public void onListItemClick(ListView parent, View v,int position, long id) {
        String str;

        if (nRowSelected>=0) {
            View row=parent.getChildAt(nRowSelected);
            if (row!=null) {
                row.setBackgroundColor(0xFFFFFFFF);
            }
        }
        nRowSelected=position;

        v.setBackgroundColor(Color.GRAY);
    }//onListItemClick

this is my listview

        <ListView
            android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="425dp" 
            >
        </ListView>

i need highlight single choice. i choose/focus row number 1. but when i scroll, the focus is more than one. the row focus in row 8 too

this is the capture

Image 1

and

Image2

how to fix that?

Upvotes: 1

Views: 766

Answers (4)

Bhavdip Sagar
Bhavdip Sagar

Reputation: 1971

Hi Please see the below code might be help it is single choice selection example follow this it work good .

public class List17 extends ListActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Use the built-in layout for showing a list item with a single
    // line of text whose background is changes when activated.
    setListAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_activated_1, mStrings));
    getListView().setTextFilterEnabled(true);

    // Tell the list view to show one checked/activated item at a time.
    getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);

    // Start with first item activated.
    // Make the newly clicked item the currently selected one.
    getListView().setItemChecked(0, true);
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // Make the newly clicked item the currently selected one.
    getListView().setItemChecked(position, true);
}

private String[] mStrings = Cheeses.sCheeseStrings;

}

This is list activity it does't matter that you have list view or list activity.

Upvotes: 0

AndroidLearner
AndroidLearner

Reputation: 4636

Replace your xml code with following::

<ListView
   android:id="@android:id/list"
   android:layout_width="match_parent"
   android:layout_height="425dp" 
   android:cacheColorHint="#00000000">
</ListView>

Upvotes: 0

user1755441
user1755441

Reputation:

Add this to the xml:

android:cacheColorHint="#00000000"

Upvotes: 2

Sam
Sam

Reputation: 86948

You are fighting the way Adapter's recycle the row layouts... You need to extend your current Adapter and override getView() to highlight the correct row (and only the correct row).

At the most basic level it would look like:

public View getView(...) {
    View view = super.getView(...);

    if(position == mRowSelected) {
        view.setBackgroundColor(Color.GRAY);
    }
    else { // You must use a default case to "un-highlight" the reused layout
        view.setBackgroundColor(0xFFFFFFFF);
    }
    return view;
}

Upvotes: 2

Related Questions