简体   繁体   中英

Android Custom Listview Programmatically Click Item

I am using a custom single choice listview. I want to call ItemOnClickListener programmatically. I am using android:listSelector="#47D149" property in my xml. I saw this post . performItemClick function is working but doesn't change the background of list item.

What should I do? Any suggestions. Thanks!

SAMPLE

ListView

<ListView
            android:id="@+id/navigation_menu_container"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_below="@id/rlBanner"
            android:layout_gravity="start"
            android:background="@color/nyc_black"
            android:choiceMode="singleChoice"
            android:divider="@color/border_black"
            android:dividerHeight="@dimen/divider_height"
            android:listSelector="@drawable/item_selector" >

drawables/item_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_activated="false" android:drawable="@drawable/item_normal"></item>
    <item android:state_pressed="true" android:drawable="@drawable/item_pressed"></item>
    <item android:state_activated="true" android:drawable="@drawable/item_pressed"></item>
</selector>

drawables/item_normal.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
     >
    <solid 
        android:color="@color/black"
        />
</shape>

drawables/item_pressed.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle"
     >
     <solid 
         android:color="@color/red"
         />
</shape>

Try the above sample. :) tell me if it works

Maybe you could just take care of it in your adapter itself?

@Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        if (null == convertView) {
            LayoutInflater inflater = (LayoutInflater) _context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.list_item, parent, false);
        }

        if(this.getItem(position).isChecked()){
            convertView.setBackgroundResource(android.R.color.holo_blue_bright);
        }else{
            convertView.setBackgroundResource(android.R.color.white);
        }

        final View tempFinalView = convertView;
        tempFinalView.setOnClickListener(new View.OnClickListener() {
                                                              @Override
                                                              public void onClick(View v) {
                                                                  if (getItem(position).isChecked()) {
                                                                      getItem(position).setChecked(false);
                                                                      tempFinalView.setBackgroundResource(R.color.white);
                                                                  } else {
                                                                      getItem(position).setChecked(true);
                                                                      tempFinalView.setBackgroundResource(R.color.pressed);
                                                                  }
                                                              }
                                                          }
        );

   return convertView;
}

EDIT : If you dont want to add another variable to the item.You could use the tag feature instead.

Just set and get the tag.

private static int VIEW_TAG=99;

@Override public View getView(final int position, View convertView, final ViewGroup parent) { if (null == convertView) { LayoutInflater inflater = (LayoutInflater) _context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate(R.layout.list_item, parent, false); }

    if((Boolean)convertView.getTag(VIEW_TAG)){
        convertView.setBackgroundResource(android.R.color.holo_blue_bright);
    }else{
        convertView.setBackgroundResource(android.R.color.white);
    }

    final View tempFinalView = convertView;
    tempFinalView.setOnClickListener(new View.OnClickListener() {
                                                          @Override
                                                          public void onClick(View v) {
                                                              if (getItem(position).isChecked()) {
                                                                  convertView.setTag(VIEW_TAG,new Boolean(false));
                                                                  tempFinalView.setBackgroundResource(R.color.white);
                                                              } else {
                                                                  convertView.setTag(VIEW_TAG,new Boolean(true));
                                                                  tempFinalView.setBackgroundResource(R.color.pressed);
                                                              }
                                                          }
                                                      }
    );

return convertView; }

Ok. I have found the solution. :) @Shifar Shifz answer, dont use

android:listSelector="@drawable/item_selector"

Instead of it, add this drawable xml to your row.xml layout as background

android:background="@drawable/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