简体   繁体   中英

java android listView set diffrent background color and selected item

I want to set diffrent background color in my lit view in adapter I did this

if (position % 2 == 0) {
                convertView.setBackgroundColor(context.getResources().getColor(R.color.darkGrey));
            } else {
                convertView.setBackgroundColor(context.getResources().getColor(R.color.grey));
            }

and it works but when I added this :

android:listSelector="@drawable/list_selector"

here :

<ListView
        android:id="@+id/list"
        android:layout_marginTop="5dp"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:drawSelectorOnTop="false"
        android:scrollbarThumbVertical="@drawable/scrollbar_vertical_thumb"
        android:visibility="visible"
        android:listSelector="@drawable/list_selector"
        android:layout_above="@+id/bottomPanel" />

I didn't see a selected item but when I remove a setting color in my adapter I see a selected item

you can set background under this method for selected item

  myListView.setOnItemSelectedListener(new OnItemSelectedListener(){

    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1,
            int position, long arg3) 
    {

    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub

    }
});

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