2012-08-15 2 views
0

나는 Checkboxes 및 3 TextViews가있는 사용자 지정 ListView가 있습니다.안드로이드 사용자 정의 ListView 확인란 및 배경색 변경

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

나는 또한 목록보기에 대한 사용자 정의 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; 
    } 
} 

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> 

나는 키를 누르면 문제이며, 행을 선택하면 배경색이 파란색으로 바뀌지 만 확인란이없는 경우 checbox는 항상 오렌지색 배경색을가집니다. 어떻게 변경할 수 있습니까? 다르게 다르게해야합니까? 나는 ICS의 Gmail 응용 프로그램과 같은 최고의 것을 원합니다.

답변

0

맞춤 체크 박스 만드는 법만 있지만 가장 좋은 해결책은 아닙니다.

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="@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" /> 
나의 체크 박스 :

android:button="@drawable/checkbox_selector" 
관련 문제