简体   繁体   中英

Listview Adapter selected row background android

I am changing listviewAdapter row selected item dynamically. In the adapter by default selectedItem is -1.

public static int selectedItem = -1; // no item selected by default

and method highlightItem is called in adapter getview method.

public View getView(final int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        if (convertView == null) {
            vi = inflater.inflate(R.layout.catalogue_row, null);
            holder = new ViewHolder();

highlightItem(selectedItem,position,vi);

and here is my highlightItem method.

private static void highlightItem(int selectedItem,int position, View result) {

    System.out.println("selected item "+selectedItem);
    if(position == selectedItem) {

        // you can define your own color of selected item here
        ViewHolder.lyCatalogueRow.setBackgroundColor(Color.parseColor(SharePreferenceController.getListViwHightlightColor()));

    } else {

        // you can define your own default selector here
        ViewHolder.lyCatalogueRow.setBackgroundColor(Color.parseColor(SharePreferenceController.getListViwBackgroundColor()));

    }
}

When i click on listview item

private OnItemClickListener itemlistener = new OnItemClickListener(){

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
    // TODO Auto-generated method stub

    int item_position = position -1;

    System.out.println("selectedItemposition "+item_position);

    CatalogueEfficientAdapter.selectedItem=item_position;

    catalogueEfficientAdapter.notifyDataSetChanged();


}

};

My arraylist contains three items. When I click on third item it is being highlighted. Now if i click on the second element only the second element should be highlighted but third row is being hightlighted. How to select and hightlight selected item only

Update the problem is highlightItem which is not updated.

public View getView(final int position, View convertView, ViewGroup parent) {
        View vi = convertView;
        if (convertView == null) {
            vi = inflater.inflate(R.layout.catalogue_row, null);

    if(position == selectedItem) {

        // you can define your own color of selected item here
        vi.setBackgroundColor(Color.parseColor(SharePreferenceController.getListViwHightlightColor()));

    } else {

        // you can define your own default selector here
        vi.setBackgroundColor(Color.parseColor(SharePreferenceController.getListViwBackgroundColor()));

    }

if you want to select items manually and programmatically you can try discard from ViewHolder.
determine each element in getView method!

public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
    // Dont decrement the position
    int item_position = position;

    System.out.println("selectedItemposition "+item_position);

    CatalogueEfficientAdapter.selectedItem=item_position;

    catalogueEfficientAdapter.notifyDataSetChanged();   
}
// try this way hope this will help you...

1.define your "ListView" item selector color in colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="list_item_selected_bg">#FF0000</color>
</resources>

2.define drawable selector for "ListView" item
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- Selected -->
    <item
        android:state_focused="true"
        android:state_selected="false"
        android:drawable="@color/list_item_selected_bg"/>

    <!-- Pressed -->
    <item
        android:state_selected="true"
        android:state_focused="false"
        android:drawable="@color/list_item_selected_bg" />

</selector>

3.use this selector for "ListView" item selection
yourListView.setSelector(R.drawable.list_item_selector);

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