简体   繁体   中英

How to change the color of a selected item in a listview?

I need to change the color of a selected item in a ListView when its clicked, so the user know what has clicked.

So far I have done this code for that:

listview.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
    for (int a = 0; a< parent.getChildCount();a++){
    parent.getChildAt(a).setBackgroundColor(Color.TRANSPARENT);
    view.refreshDrawableState();
     if(parent.getChildAt(a) == view){
         view.setBackgroundColor(getResources().getColor(R.color.soft_opaque));
         view.refreshDrawableState();
    }
}

What it does is to change the background color of the item selected and keep it like that until I click another item so it change only the current item selected background color

I also was wondering if there is an XML way to do it. What I've found so far is:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@color/orange" /> <!-- pressed -->
    <item android:state_focused="true" android:drawable="@color/black" /> <!-- focused -->
    <item android:state_selected="true" android:state_pressed="true" android:drawable="@color/black" /> <!-- focused and pressed-->
    <item android:drawable="@color/black" /> <!-- default -->
</selector> 

but haven't got what I the previous code does. what it does, though, is to change the background color and change it when its clicked, but doesn't keep the selected item with an specific color

EDIT: My adapter

    public class ExtraAdapter extends ArrayAdapter<String> {
    Context context;
    String[] tittleArray;

    public ExtraAdapter(Context c, String[] titles) {
        super(c, R.layout.custom_listview_extra, titles);
        this.context = c;
        this.tittleArray = titles;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        if (row == null) {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.custom_listview_extra, parent,
                    false);
        }
        TextView myTitle = (TextView) row.findViewById(R.id.plato_extra);
        myTitle.setText(tittleArray[position]);

        return row;
    }

}

My Activity

    public class ExtraMenu extends ActionBarActivity {
    ListView lcategory;
    ListView lextras;
    ExtraAdapter adapter;
    String[] categoria;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_extra_test);
        lcategory = (ListView) findViewById(R.id.categorias);
        String[] listaExtras = getResources().getStringArray(R.array.listaExtras);
        adapter = new ExtraAdapter(getApplicationContext(), listaExtras);
        lcategory.setAdapter(adapter);

        lcategory.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                parent.setSelected(true);
                /*
                for (int a = 0; a< parent.getChildCount();a++){
                    parent.getChildAt(a).setBackgroundColor(Color.TRANSPARENT);
                    view.refreshDrawableState();
                    if(parent.getChildAt(a) == view){
                        view.setBackgroundColor(getResources().getColor(R.color.soft_opaque));
                        view.refreshDrawableState();
                    }
                }*/
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.extra_test, menu);
        return true;
    }
    }

My Activity XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:weightSum="2" >

    <ImageView
        android:id="@+id/logo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_weight="1.5"
        android:src="@drawable/logo" />

    <ListView
        android:id="@+id/categorias"
        android:layout_weight="0.5"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        //android:background="@drawable/list_color"
        android:layout_marginTop="20dp">
    </ListView>

</LinearLayout>

My adapter XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/Widget.AppCompat.ListView.Menu"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:paddingTop="10dip"
    android:paddingBottom="10dip">

    <TextView
        android:id="@+id/plato_extra"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="20dp"
        //android:background="@drawable/list_color"
        android:textColor="#000000"
        android:textSize="25sp" />

</RelativeLayout>

the way you wrote the selector you get black, when you keep the row pressed. That's because the pressed view is also focused, and the first match is pressed and focused. Change the order abd get rid of the implementation of onItemClick

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@color/green" /> <!-- pressed -->
    <item android:state_focused="true" android:drawable="@color/black" /> <!-- focused -->
    <item android:state_focused="true" android:state_pressed="true" android:drawable="@color/black" /> <!-- focused and pressed-->
    <item android:drawable="@color/black" /> <!-- default -->
</selector> 

Use

listView.setOnItemSelectedListener(new OnItemSelectedListener() {

                @Override
                public void onItemSelected(AdapterView<?> arg0, View arg1,
                        int arg2, long arg3) {
                    // TODO Auto-generated method stub
                    arg0.setSelected(true);
                }

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

                }
            });

And in your selector Drawable

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@color/green" /> <!-- pressed -->
    <item android:state_focused="true" android:drawable="@color/black" /> <!-- focused -->
    <item android:state_selected="true"  android:drawable="@color/black" /> <!-- selected-->
    <item android:drawable="@color/black" /> <!-- default -->
</selector> 

I haven't tested it but surely this will work.

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