listview item background color change

后端 未结 5 689
甜味超标
甜味超标 2020-12-16 08:43

I am working on an android application. I have created a listview by using

setListAdapter(new ArrayAdapter(this,android.R.layout.simple_list_i         


        
相关标签:
5条回答
  • 2020-12-16 09:09

    After going through lots of posts and blogs i found this solution it works for me...

    declare row variable global

    public View row;
    
    your_list.setOnItemClickListener(new OnItemClickListener() {
    
        public void onItemClick(AdapterView<?> a, View v,
                            int position, long id) {
    
        if (row != null) {
            row.setBackgroundResource(R.color.orange);
        }
        row = v;
        v.setBackgroundResource(R.color.transparent_green);
    )};
    
    0 讨论(0)
  • 2020-12-16 09:14

    By default the background is black. If you have customized your listview then when you scroll it would turn black. So when you define your list view set background cache color to the color you need as below:

        yourlistView.setCacheColorHint(Color.WHITE);
    
    0 讨论(0)
  • 2020-12-16 09:19

    What you can do is instead of using .setBackgroundColor() with a Color value, create a Color State List and assign it with .setBackgroundResource(). That way, you can define the various states that your list item can become, depending on the item's current state.

    0 讨论(0)
  • 2020-12-16 09:22

    I this This happening because you have put text color as black and your setting the background color also black that's why you can't see the difference. for setting the background color you can use the following line.

    view.setBackgroundColor(Color.YELLOW);

    use different color then text color.

    0 讨论(0)
  • 2020-12-16 09:32

    Well using this in the properties of your list view mught help:

    android:listSelector="@drawable/list_selector"
    

    It will automatically set the selected drawable as background of the selected item. Hope this works for you and drawable may be your own choice's color.

    Explanation: Add an image strip to your drawables folder/s and set that image in listSelector attribute of the your list view. Now you will navigate through your list view, the list view's background will be of the color of the image strip you set instead of android's native color. Hope you get it now...:-)

    0 讨论(0)
提交回复
热议问题