从列表适配器更改图层列表形状纯色

时间:2014-09-04 14:27:10

标签: android listadapter layer-list

我有这个cardview我用图层列表背景。此cardview填充列表视图。 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在那里不起作用。

请帮帮我!

更新: 后台drawable在我的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" >

....

1 个答案:

答案 0 :(得分:0)

我找到了自己的方式,我创建了另一个xml资源名称layer_car_background_green.xml

我引用了我的relativelayout

public RelativeLayout mainLayout;

....

viewContainer.mainLayout = (RelativeLayout)rowView.findViewById(R.id.item_layout_main);

然后在我的onClick

中更改它的后台资源
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.mainLayout.setBackgroundResource(R.drawable.layer_car_background_green);

                            }

                        } else {
                            viewContainer.expandButton
                                    .setImageResource(R.drawable.ic_action_collapse);
                            viewContainer.expandLayout
                                    .setVisibility(View.VISIBLE);

                        }
                    }
                });