Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android listview项目颜色更改_Android - Fatal编程技术网

Android listview项目颜色更改

Android listview项目颜色更改,android,Android,我有以下使用充气器的列表适配器,我尝试在这里添加颜色更改,但它会更改每个项目,而不仅仅是单击的项目 private class ListAdapter extends ArrayAdapter<jsonData>{ private List<jsonData> jList; public ListAdapter(Context context, int resource,List<jsonData> jList) { sup

我有以下使用充气器的列表适配器,我尝试在这里添加颜色更改,但它会更改每个项目,而不仅仅是单击的项目

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;
            }
私有类ListAdapter扩展了ArrayAdapter{
私人名单;
公共ListAdapter(上下文上下文、int资源、列表jList){
超级(上下文、资源、jList);
this.jList=jList;
}
公共视图getView(int位置、视图转换视图、视图组父视图){
视图v=转换视图;
如果(v==null){
LayoutInflater vi=(LayoutInflater)getSystemService(Context.LAYOUT\u INFLATER\u SERVICE);
v=vi.充气(R.layout.listviewact\u布局,空);
v、 setBackgroundResource(R.layout.list\u选择器);
}
jsonData jd=jList.get(位置);
如果(jd!=null){
TextView名称=(TextView)v.findViewById(R.id.memberName);
TextView dateJoined=(TextView)v.findviewbyd(R.id.dateJoined);
if(name!=null){
name.setText(“成员名称:+jd.name”);
}
if(dateJoined!=null){
dateJoined.setText(“Joined:+getNewDate(jd.Joined));
}
}
返回v;
}
我也能得到项目的位置,除了颜色外,大部分都很好用。我也尝试过为选择器添加一个资源文件,但我得到了相同的结果

更新:这似乎有效。但当我滚动物品颜色时,我有一个小故障

            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;
            }               
        }
public void onItemClick(AdapterView父视图、视图、int位置、,
长id){
如果(选择编辑位置!=位置){
//将旧项目重置为原始颜色
parent.getChildAt(selectedItemPosition).setBackgroundColor(Color.BLACK);
视图。setBackgroundColor(颜色。蓝色);
选择编辑位置=位置;
}               
}

使用
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>

试试这个选择器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" />

查看.设置背景();
更改整个列表的背景。因为您的视图指向该列表,而不是该列表中的字段。
试试这个

在OnClickListener中编写颜色更改代码为什么不在版面资源本身中设置背景可绘制?另外,您正在使用另一个版面资源作为背景可绘制?这只是我使用的一个不起作用的示例。我将颜色更改添加到OnItemClickListener中,它可以工作,但颜色不会重置。我将我还需要进一步研究,但我的思路是正确的。我也会尝试这种方法。我在列表选择器中有这种方法。按如下方式使状态为真:这两种方法似乎都不起作用,我尝试了几种方法,它似乎会突出显示每一项,而不仅仅是一项。我希望所选项为蓝色。我必须添加v.setBackgroundResource(R.drawable.list_selector);返回并运行后,它还修复了滚动问题。现在我只需让它保持选中状态。我假设将其更改为蓝色透明。结果相同,并且不会保持该项目处于选中状态。我尝试将其设置为onItemClick视图中的BackgroundResource。同样,它的工作方式“不同”但还不正确。即使使用onItemClick视图,也会使整个列表变为蓝色。
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>