更改ListView的背景颜色项,同时保持自定义Drawable的效果

时间:2013-05-14 13:32:07

标签: java android android-drawable

我尝试更改ListView中特定项目的背景颜色,同时保持自定义Drawable的效果。

我的自定义list_items:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/label"
    style="@android:style/TextAppearance.Holo"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background_list"
    android:textColor="@color/white" />

我的自定义ArrayAdapter:

public class MissionDataListAdapter extends ArrayAdapter {
    private final ArrayList<MissionData> list_missions; 
    public MissionDataListAdapter(Context context, int textViewResourceId, ArrayList<MissionData> objects) {
        super(context, textViewResourceId, objects);
        this.list_missions = objects;       
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent){      
        View v = super.getView(position, convertView, parent);
        MissionData mission = list_missions.get(position);

        if(position==4){        
            v.setBackgroundResource(R.color.mauve);     
        }
        return v;
    }
}

我的活动布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/border_list"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/missionsListView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:divider="@color/blue2"
        android:dividerHeight="1dp" />

</LinearLayout>

我的自定义绘图用于在按下项目时更改颜色:

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

公共类MissionsListActivity扩展了Activity实现OnClickListener {

[..]
MissionDataListAdapter arrayAdapter;

    ArrayList<String> donnees_parses;
    ArrayList<MissionData> missions;
    ListView missionsListView;
    MissionData selectedmission;
    MissionsDAO missionsBdd;

public void onCreate(Bundle savedInstanceState) {       
        super.onCreate(savedInstanceState);
        setContentView(R.layout.missions);
        [..]
        missionsListView = (ListView)this.findViewById(R.id.missionsListView);  
        int layoutID = R.layout.list_items; 
        arrayAdapter = new MissionDataListAdapter(this, layoutID , missions);           
        missionsListView.setAdapter(arrayAdapter);
}
[..]

对于上面的代码,第四项的背景颜色已更改,但不再受自定义Drawable的影响

[..]
<item android:drawable="@color/orange_off" android:state_pressed="true"/>  
[..]

那么,如何在保持Drawable对我的项目的影响的同时更改一个项目的背景颜色?

1 个答案:

答案 0 :(得分:1)

为什么不用另一种颜色创建另一个drawable,然后在位置为4时设置drawable:

@Override
public View getView(int position, View convertView, ViewGroup parent){      
    View v = super.getView(position, convertView, parent);
    MissionData mission = list_missions.get(position);

    if(position==4){        
        v.setBackgroundResource(R.drawable.background_list_mauve);     
    }
    return v;
}