简体   繁体   English

Android Listview项目颜色更改

[英]Android listview item color change

I have the following list adapter that uses an inflater, I've tried adding the color change here but it changes every item not just the one clicked. 我有以下使用充气机的列表适配器,我尝试在此处添加颜色更改,但它更改了每个项目,而不仅仅是单击的项目。

private class ListAdapter extends ArrayAdapter<jsonData>{

    private List<jsonData> jList;

    public ListAdapter(Context context, int resource,List<jsonData> jList) {
        super(context, resource, jList);
        this.jList = jList;
    }
         public View getView(int position, View convertView, ViewGroup parent) {
                View v = convertView;

                if (v == null) {
                    LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    v = vi.inflate(R.layout.listviewact_layout, null);
                            v.setBackgroundResource(R.layout.list_selector);
                }

                jsonData jd = jList.get(position);

                if (jd != null) {             
                    TextView name = (TextView) v.findViewById(R.id.memberName);
                    TextView dateJoined = (TextView) v.findViewById(R.id.dateJoined);
                    if (name != null) {
                        name.setText("Member Name: " + jd.name);
                    }
                    if (dateJoined != null) {
                        dateJoined.setText("Joined: " + getNewDate(jd.joined));
                    }
                }

                return v;
            }

I am able to get the item position also, most of it works fine except the colors. 我也可以得到项目的位置,除了颜色以外,大多数都可以正常工作。 I also tried adding a resource file for the selector, but I get the same result. 我也尝试为选择器添加资源文件,但得到的结果相同。

UPDATE: This seems to work. 更新:这似乎有效。 I have a glitch though when I scroll the item colors go crazy. 但是,当我滚动项目时,我会出现一个小故障。

            public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {          

            if(selectedItemPosition != position){
                //Resets old item to original color
                parent.getChildAt(selectedItemPosition).setBackgroundColor(Color.BLACK);
                view.setBackgroundColor(Color.BLUE);                    
                selectedItemPosition = position;
            }               
        }

use android:listSelector not android:background for adding selector xml file to ur listview 使用android:listSelector而不是android:background将选择器xml文件添加到您的listview

         <ListView
            android:id="@+id/list"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:listSelector="@drawable/list_selector">
        </ListView>

Try this selector xml 试试这个选择器XML

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_enabled="true" 
 android:state_pressed="true" android:drawable="@color/android:blue" />
<item android:state_enabled="true"
 android:state_focused="true" android:drawable="@color/android:transparent" />
<item
 android:drawable="@color/android:black" />

view.setbackground();

Changes the background of entire list .since your view points to that list not a field in that list.
try this

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM