带复选框和更改背景颜色的Android自定义ListView
我有一个带有复选框和3个文本视图的自定义列表视图。以下是我的列表项布局:带复选框和更改背景颜色的Android自定义ListView,android,listview,android-listview,Android,Listview,Android Listview,我有一个带有复选框和3个文本视图的自定义列表视图。以下是我的列表项布局: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/listviewitem_layout" android:layout_width="match_parent" android:layout_h
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/listviewitem_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<CheckBox
android:id="@+id/faxCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false" />
<LinearLayout android:orientation="horizontal"
android:id="@+id/faxbox_item_first_row"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/faxCheckBox"
android:paddingTop="8dp"
android:paddingLeft="8dp"
android:paddingRight="8dp">
<TextView android:id="@+id/fax_number_textView"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:lines="1"
android:ellipsize="start"
android:textSize="12sp"
android:paddingRight="5dp"/>
<TextView android:id="@+id/fax_send_date_textView"
android:layout_width="wrap_content"
android:layout_weight="0"
android:layout_height="wrap_content"
android:textSize="9sp"/>
</LinearLayout>
<TextView android:id="@+id/more_fax_info_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="9sp"
android:textColor="@color/gray"
android:layout_toRightOf="@id/faxCheckBox"
android:layout_below="@id/faxbox_item_first_row"
android:paddingLeft="8dp"/>
</RelativeLayout>
我还有一个用于listview的自定义arrayadapter:
private class MyAdapter extends ArrayAdapter<Order> {
private ArrayList<Order> mOrders;
private Context mContext;
private int mResourceId;
public MyAdapter(Context context, int resourceId, ArrayList<Order> orders) {
super(context, resourceId, orders);
this.mContext = context;
this.mOrders = orders;
this.mResourceId = resourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(mResourceId, parent, false);
}
final Order order = mOrders.get(position);
if (order != null) {
TextView sender = (TextView)view.findViewById(R.id.number_textView);
TextView date = (TextView)view.findViewById(R.id.send_date_textView);
TextView moreInfo = (TextView)view.findViewById(R.id.more_info_textView);
CheckBox checkBox = (CheckBox)view.findViewById(R.id.checkBox);
final int itemPosition = position;
final RelativeLayout rowLayout = (RelativeLayout)view.findViewById(R.id.listviewitem_layout);
view.setTag(order.getId());
if (sender != null) {
sender.setText(order.getSender());
}
rowLayout.setBackgroundResource(R.drawable.listview_item_selector);
if (mSelectedIds.contains(order.getId())) {
rowLayout.setBackgroundColor(Color.parseColor("#9CD7EF"));
}
if (date != null) {
date.setText(Utils.getStringDateForBox(order.getSendDate(), mContext));
}
if (moreInfo != null) {
moreInfo.setText(getResources().getQuantityString(R.plurals.number_of_pages, order.getPages(), order.getPages()));
}
if (checkBox != null) {
checkBox.setTag(order.getId());
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (mSelectedIds.contains(buttonView.getTag()) == true) {
if (isChecked == false) {
mSelectedIds.remove(buttonView.getTag());
}
} else {
if (isChecked) {
mSelectedIds.add((Long) buttonView.getTag());
}
}
showHideActionMode();
setActionModeTitle();
mListView.setItemChecked(position, true);
}
});
if (mSelectedIds.contains(order.getId())) {
checkBox.setChecked(true);
} else {
checkBox.setChecked(false);
}
}
}
return view;
}
}
私有类MyAdapter扩展了ArrayAdapter{
私人ArrayList mOrders;
私有上下文;
私有资源;
公共MyAdapter(上下文上下文、int resourceId、ArrayList顺序){
超级(上下文、资源ID、订单);
this.mContext=上下文;
这个.mOrders=订单;
this.mResourceId=资源ID;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图=转换视图;
如果(视图==null){
LayoutInflater vi=(LayoutInflater)mContext.getSystemService(Context.LAYOUT\u INFLATER\u SERVICE);
视图=vi.充气(mResourceId,父项,false);
}
最终订单=mOrders.get(位置);
如果(订单!=null){
TextView发送者=(TextView)view.findViewById(R.id.number\u TextView);
TextView日期=(TextView)view.findViewById(R.id.send\u date\u TextView);
TextView moreInfo=(TextView)view.findViewById(R.id.more\u info\u TextView);
CheckBox=(CheckBox)view.findViewById(R.id.CheckBox);
最终int itemPosition=位置;
最终RelativeLayout行布局=(RelativeLayout)视图.findViewById(R.id.listviewitem_布局);
view.setTag(order.getId());
if(发送方!=null){
sender.setText(order.getSender());
}
rowLayout.setBackgroundResource(R.drawable.listview\u项目\u选择器);
if(mselectedds.contains(order.getId())){
rowLayout.setBackgroundColor(Color.parseColor(“#9CD7EF”);
}
如果(日期!=null){
date.setText(Utils.getStringDateForBox(order.getSendDate(),mContext));
}
如果(moreInfo!=null){
moreInfo.setText(getResources().getQuantityString(R.plurals.number of_pages,order.getPages(),order.getPages());
}
如果(复选框!=null){
setTag(order.getId());
setOnCheckedChangeListener(新建CompoundButton.OnCheckedChangeListener(){
@凌驾
检查更改后的公共无效(复合按钮视图,布尔值已检查){
if(mSelectedIds.contains(buttonView.getTag())==true){
如果(isChecked==false){
mselecteds.remove(buttonView.getTag());
}
}否则{
如果(已检查){
mselecteds.add((长)按钮view.getTag());
}
}
showHideActionMode();
setActionModeTitle();
mListView.setItemChecked(位置,true);
}
});
if(mselectedds.contains(order.getId())){
checkBox.setChecked(true);
}否则{
checkBox.setChecked(false);
}
}
}
返回视图;
}
}
listview_item_selector.xml
<?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/blue" />
<item android:state_pressed="true"
android:drawable="@color/blue" />
<item android:state_selected="true"
android:state_activated="true"
android:drawable="@color/light_blue" />
<item android:state_activated="true"
android:drawable="@color/light_blue" />
<item android:state_selected="true"
android:drawable="@color/light_blue" />
<item android:state_focused="false" android:state_selected="false" android:state_pressed="false"
android:drawable="@android:color/white" />
<item android:drawable="@android:color/transparent" />
</selector>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true"
android:drawable="@drawable/btn_check_off_pressed_holo_light" />
<item android:state_checked="true"
android:state_pressed="true"
android:drawable="@drawable/btn_check_on_pressed_holo_light" />
<item android:state_pressed="true"
android:drawable="@drawable/btn_check_off_pressed_holo_light" />
<item android:state_checked="true"
android:state_activated="true"
android:drawable="@drawable/btn_check_on_holo_light" />
<item android:state_activated="true"
android:drawable="@drawable/btn_check_on_holo_light" />
<item android:state_checked="true"
android:drawable="@drawable/btn_check_on_holo_light" />
<item android:state_focused="false" android:state_selected="false" android:state_pressed="false"
android:drawable="@drawable/btn_check_off_holo_light" />
<item android:drawable="@drawable/btn_check_off_holo_light" />
问题是,如果我按一行,它会将背景颜色更改为蓝色,但如果没有复选框,checbox始终具有橙色背景颜色。我怎样才能改变它?
也许我应该改变一切?我最喜欢的是ICS上的gmail应用程序。我只知道如何设置自定义复选框,但这不是最好的解决方案 checbox_selector.xml
<?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/blue" />
<item android:state_pressed="true"
android:drawable="@color/blue" />
<item android:state_selected="true"
android:state_activated="true"
android:drawable="@color/light_blue" />
<item android:state_activated="true"
android:drawable="@color/light_blue" />
<item android:state_selected="true"
android:drawable="@color/light_blue" />
<item android:state_focused="false" android:state_selected="false" android:state_pressed="false"
android:drawable="@android:color/white" />
<item android:drawable="@android:color/transparent" />
</selector>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true"
android:drawable="@drawable/btn_check_off_pressed_holo_light" />
<item android:state_checked="true"
android:state_pressed="true"
android:drawable="@drawable/btn_check_on_pressed_holo_light" />
<item android:state_pressed="true"
android:drawable="@drawable/btn_check_off_pressed_holo_light" />
<item android:state_checked="true"
android:state_activated="true"
android:drawable="@drawable/btn_check_on_holo_light" />
<item android:state_activated="true"
android:drawable="@drawable/btn_check_on_holo_light" />
<item android:state_checked="true"
android:drawable="@drawable/btn_check_on_holo_light" />
<item android:state_focused="false" android:state_selected="false" android:state_pressed="false"
android:drawable="@drawable/btn_check_off_holo_light" />
<item android:drawable="@drawable/btn_check_off_holo_light" />