简体   繁体   中英

change the background color of a listview item when clicked

my app contain a ListView and when a user click on one of the items, the selected item color change to a different color. i acomplished this by adding this android:listSelector="@android:color/darker_gray" in my ListView located in my activity_main.xml file. This is precisely what i am looking for, but im having issues with it because the background color also changes when the user hold the item but dont click it. so i was wondering if the was a very extremely simple way i could put this code: android:listSelector="@android:color/darker_gray" inside my OnItemClick method. that way when an item from my listview is clicked, i want the selected item background to change. also i do NOT want to rearrange my listview and put the selected items on top, i just want all the items that the user have clicked to have a different background color which is why i want to put the coding inside onItemClick. Currently i am using the default sample_list_item_1 for my ListView.

Additional Info about App: This is kinda like a tutorial app where the user is presented with a list of tutorial names. when the user clicks on a names it loads a stream-able link so the user can just stream it inside the app (this is already done). i have also added a toast message that shows when the user clicks on the stream-able links (this is kinda like what i want eccept instead of a toast message, it changes the background color of the selected item to darker_grey). i was able to do all this inside if-statements. for example:

Inside OnCreate()

String[] Tutorial = getResources().getStringArray(R.array.Tutorial);
spinner1 = (ListView) findViewById(R.id.spinner1);
spinner1.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Tutorial));
spinner1.setOnItemClickListener(new OnItemClickListener() 
{
    public void onItemClick(AdapterView<?> parent, View v, int pos, long id) 
    {
        if(pos==0)
        {
            // Do nothing       
        }
        else if(pos==1)
        {
            Intent i = new Intent();
            i.setAction(Intent.ACTION_VIEW);
            Uri uri = Uri.parse("https://download846.mediafire.com/8cfxi4026iog/x4ywqe8dc8u2r4f/Hetalia+Season+1+Episode+1.mp4"); /* this is a random link to show you that my programing so far works(Enter link to the selected tutorial here)*/
            i.setDataAndType(uri, "video/*");
            try {
                startActivity(i);
            } catch (ActivityNotFoundException e) {
                 // Handle exception
            } catch (Exception e) {
                // Handle exception 
            }
        Toast.makeText(parent.getContext(), parent.getItemAtPosition(pos).toString(), Toast.LENGTH_SHORT).show();       
        // android:listSelector="@android:color/darker_gray"
        }           
    }           
}

if you still dont understand what i meant, there is an app in the googlePlay store called Anime Plus TV that does something similar to what i meant. in the app, when you click on the episode of a serie (example: naruto) it loads the episode's stream-able link, and also change the text color of that selected episode so that when the user finishes watching the episode, he wont be confused about which episode he was watching since some stream-able links doesnt realy give you the real title of the video you where watching. but unlike them i just want the background of the selected Tutorial to change

You have two options doing that .

1.progamatically: In this case when you set the OnItemClickListener for your ListView , get the child and setBackground for it .

        String[] inputs = {"test1","test2","test3"};
    final ListView l = (ListView)findViewById(R.id.listview);
    l.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 ,inputs));
    l.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View view, int position,
                long id) {
            l.getChildAt(position).setBackgroundColor(android.R.color.black);
        }
    });

You can define colors in color.xml file under folder res/values ( create it if you dont have it ) and use them like R.color.gray (example) instead of android.R.color.black .

2.Using Custom listSelector

under folder drawable create the listselector.xml . In listselector you can handle different states just like this :

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Selector style for listrow -->
    <item 
        android:state_selected="false"
        android:state_pressed="false" 
        android:drawable="@drawable/test1" />
    <item android:state_pressed="true" 
        android:drawable="@drawable/test2" />
    <item android:state_selected="true"
        android:state_pressed="false" 
        android:drawable="@drawable/test3" />
    </selector>

for your case i think using first way is the best . ask if you had any other questions :)

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