简体   繁体   中英

Change Background color item of ListView while maintaining effect of custom Drawable

I try to change the background color at specific item within my ListView, while maintaining the effect of the custom Drawable.

My custom 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" />

My custom 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;
    }
}

My Layout for my Activity :

<?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>

My custom drawable for change the color when item is pressed :

<?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>

public class MissionsListActivity extends Activity implements 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);
}
[..]

For the above code, the background color of fourth item its changed, but is no longer affected by the custom Drawable :

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

So, how can I change the background color of one item while maintaining the effects of Drawable on my item ?

Why not create another drawable with another color and then set that drawable if the position is 4:

@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;
}

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