2014-09-04 6 views
1

레이어 목록 배경으로 만든이 카드보기가 있습니다. 이 카드보기는 목록보기를 채 웁니다. cardview에는 EditTexts, ImageViews, TextViews 및 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; 
} 

}

이다, 통화 viewContainer.color.setColor은 t 작동하지 않습니다 이리.

제발 도와주세요!

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

.... 
+0

LayerDrawable listItem으로 무엇을하고 있습니까? 대답은 : 아무 것도, 당신은 그냥 리소스에서 그것을 얻을 .... 그게 다야 – pskink

+0

내가 GradientDrawable ID를 얻을 색상이 필요합니까? 어쨌든 중요한 것은 아닙니다! – AbsoluteHeero

+0

예, ID를 기반으로 레이어를 가져와야하지만 해당 레이어 드로우 블을 사용하지 않으면 아무 것도 설정하지 마십시오. – pskink

답변

0

에 설정되어 , 나는 다른 XML 능숙 내가 내 RelativeLayout의를 참조했습니다

layer_car_background_green.xml 이름을 만들었습니다

public RelativeLayout mainLayout; 

.... 

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

후 변경 내 온 클릭

에 배경 자원의3210
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); 

         } 
        } 
       }); 
관련 문제