1
我有這個cardview我用層列表背景。這個cardview填充一個listview。 Cardview保存EditTexts,ImageViews,TextViews dans CheckBoxes。還有,展開和摺疊按鈕可以查看更多卡片。我想要做的是,當用戶點擊該按鈕摺疊視圖,如果某複選框被選中我想卡的背景色設置爲另一種顏色從列表適配器更改層列表形狀的純色
這裏是我的layer.car_background.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="@android:color/darker_gray"/>
<corners android:radius="8dp" />
</shape>
</item>
<item
android:id="@+id/item_list_background"
android:left="0dp"
android:right="0dp"
android:top="0dp"
android:bottom="2dp">
<shape android:shape="rectangle">
<solid android:color="@android:color/white"/>
<corners android:radius="8dp" />
</shape>
</item>
</layer-list>
這裏是我的名單適配器
public class InspectionListAdapter extends ArrayAdapter<String>{
private final Activity context;
private final String[] inspectionTitles;
private final String[] description;
public InspectionListAdapter(Activity context, String[] inspectionTitles,
String[] description) {
super(context, R.layout.inspection_item_layout, inspectionTitles);
this.context = context;
this.inspectionTitles = inspectionTitles;
this.description = description;
}
static class ViewContainer {
public ImageView imageView, expandButton;
public TextView txtTitle;
public TextView txtDescription;
public RelativeLayout expandLayout;
public EditText ownDesc, estCost;
public LayerDrawable listItem;
public GradientDrawable color;
public CheckBox checkRepair, checkDone;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
final ViewContainer viewContainer;
View rowView = view;
if (rowView == null) {
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.inspection_item_layout, null,
true);
viewContainer = new ViewContainer();
viewContainer.txtTitle = (TextView) rowView
.findViewById(R.id.textViewItemTitle);
viewContainer.txtDescription = (TextView) rowView
.findViewById(R.id.textViewTip);
viewContainer.listItem = (LayerDrawable)rowView.getResources().getDrawable(R.drawable.layer_car_background);
viewContainer.color = (GradientDrawable)(viewContainer.listItem.findDrawableByLayerId(R.id.item_list_background));
viewContainer.imageView = (ImageView) rowView
.findViewById(R.id.imageViewItem);
viewContainer.expandButton = (ImageView) rowView
.findViewById(R.id.imageViewTitle);
viewContainer.ownDesc = (EditText)rowView.findViewById(R.id.editTextDescription);
viewContainer.estCost = (EditText)rowView.findViewById(R.id.editTextEstCost);
viewContainer.checkRepair = (CheckBox)rowView.findViewById(R.id.checkBox2);
viewContainer.checkDone = (CheckBox)rowView.findViewById(R.id.checkBox1);
viewContainer.checkRepair.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton view, boolean arg1) {
// TODO Auto-generated method stub
if(view.isChecked()){
viewContainer.checkDone.setChecked(false);
}
}
});
viewContainer.checkDone.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton view, boolean arg1) {
// TODO Auto-generated method stub
if(view.isChecked()){
viewContainer.checkRepair.setChecked(false);
}
}
});
viewContainer.expandLayout = (RelativeLayout) rowView
.findViewById(R.id.relativeLayoutExpandable);
viewContainer.expandLayout.setVisibility(View.GONE);
rowView.setTag(viewContainer);
viewContainer.expandButton
.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if (viewContainer.expandLayout.getVisibility() == View.VISIBLE) {
viewContainer.expandButton
.setImageResource(R.drawable.ic_action_expand);
viewContainer.expandLayout
.setVisibility(View.GONE);
if(viewContainer.checkDone.isChecked()){
viewContainer.color.setColor(Color.GREEN);
}
} else {
viewContainer.expandButton
.setImageResource(R.drawable.ic_action_collapse);
viewContainer.expandLayout
.setVisibility(View.VISIBLE);
}
}
});
} else {
// ---view was previously created; can recycle---
Log.d("InspectionListAdapter", "Recycling");
// ---retrieve the previously assigned tag to get
// a reference to all the views; bypass the findViewByID() process,
// which is computationally expensive---
viewContainer = (ViewContainer) rowView.getTag();
}
// ---customize the content of each row based on position---
viewContainer.txtTitle.setText(inspectionTitles[position]);
viewContainer.txtDescription.setText("Tip: \n" + description[position]);
viewContainer.imageView.setImageResource(R.drawable.ic_launcher);
viewContainer.expandButton
.setImageResource(R.drawable.ic_action_expand);
return rowView;
}
}
如果你看看我的onClick方法,調用viewContainer.color.setColor不起作用ŧ這裏。
請幫我一把!
UPDATE: 背景繪製在我item_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:background="@drawable/layer_car_background" >
....
你在用LayerDrawable listItem做什麼;?答案是:沒什麼,你只是從資源中得到它......多數民衆贊成它 – pskink 2014-09-04 14:40:46
我需要它來獲得顏色GradientDrawable ID不是嗎? 無論如何,這不是重點! – AbsoluteHeero 2014-09-04 15:01:19
yesy您必須獲取基於id的圖層,但您不對LayerDrawable執行任何操作,請勿將其設置爲任何形式 – pskink 2014-09-04 15:13:58