2016-12-02 1 views
0

단일 선택 목록보기로 사용자 지정 레이아웃을 디자인하려고 시도했지만 선택 모드가 작동하지 않습니다. 다음은 관련 코드입니다.사용자 지정 레이아웃이 목록보기 (단일 선택)로 작동하지 않습니다.

public class CheckableFrameLayout extends FrameLayout implements Checkable { 
    private boolean isChecked; 
    public CheckableFrameLayout(Context context) { 
     super(context); 
    } 

    public CheckableFrameLayout(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public CheckableFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
    } 

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) 
    public CheckableFrameLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
     super(context, attrs, defStyleAttr, defStyleRes); 
    } 

    @Override 
    public void setChecked(boolean b) { 
     setEnabled(b); 
     isChecked = b; 
     traverseAndCheck(this, b); 
    } 
    private void traverseAndCheck(View v, boolean b){ 
     if(v instanceof ViewGroup){ 
      int count = ((ViewGroup)v).getChildCount(); 
      for(int i = 0; i< count; i++){ 
       View c = ((ViewGroup)v).getChildAt(i); 
       if(c instanceof Checkable){ 
        ((Checkable)c).setChecked(b); 
       } 
       else if (c instanceof ViewGroup) { 
        traverseAndCheck(c, b); 
       } 
      } 
     } 
     refreshDrawableState(); 
    } 
    @Override 
    public boolean isChecked() { 
     return isChecked; 
    } 

    @Override 
    public void toggle() { 
     isChecked = !isChecked; 
     setEnabled(isChecked); 
     traverseAndToggle(this); 
    } 
    private void traverseAndToggle(View v) { 
     if (v instanceof ViewGroup) { 
      int count = ((ViewGroup)v).getChildCount(); 
      for (int i = 0; i < count; i++) { 
       View c = ((ViewGroup)v).getChildAt(i); 
       if (c instanceof Checkable) { 
        ((Checkable)c).toggle(); 
       } 
       else if(c instanceof ViewGroup) { 
        traverseAndToggle(c); 
       } 
      } 
     } 
     refreshDrawableState(); 
    } 

} 

여기에 해당하는 항목이 있습니다.

<?xml version="1.0" encoding="utf-8"?> 
<layout xmlns:app="http://schemas.android.com/apk/res-auto"> 
    <data> 
     <variable 
      name="userCard" 
      type="com.innofied.saferyde.model.UserCard"/> 

    </data> 
    <com.innofied.saferyde.widget.CheckableFrameLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_height="match_parent" 
     android:layout_width="match_parent" 
     android:padding="10dp"> 
     <android.support.v7.widget.CardView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      app:cardBackgroundColor="@color/cardview_dark_background" 
      > 
     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:padding="10dp"> 

      <CheckBox 
       android:id="@+id/check" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:drawableLeft="@drawable/payment_check_drawable" 
       android:button="@null" 
       android:layout_alignParentLeft="true" 
       android:layout_centerVertical="true" 
       android:layout_marginRight="10dp" 
       /> 
      <ImageView 
       android:id="@+id/card_type" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_toRightOf="@+id/check" 
       android:layout_centerVertical="true" 
       android:layout_marginRight="10dp" 
       android:layout_marginLeft="10dp"/> 
      <LinearLayout 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:orientation="vertical" 
       android:layout_toRightOf="@+id/card_type" 
       android:layout_toLeftOf="@+id/exp_date_txt"> 
       <TextView 
        android:id="@+id/name_txt" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:textColor="@color/colorWhite" 
        android:textAllCaps="true" 
        android:text="@{userCard.name}" 
        android:layout_marginBottom="5dp"/> 
       <TextView 
        android:id="@+id/card_number" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:textColor="@color/colorWhite" 
        android:text='@{"**** **** **** " + userCard.last4}' 
        /> 
      </LinearLayout> 
      <TextView 
       android:id="@+id/exp_date_txt" 
       android:layout_alignParentRight="true" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textColor="@color/colorWhite" 
       android:text='@{"Exp.\n" + userCard.exp_month + "/" + userCard.exp_year }'/> 
     </RelativeLayout> 
     </android.support.v7.widget.CardView> 
    </com.innofied.saferyde.widget.CheckableFrameLayout> 
</layout> 

원하는 경우 어댑터 코드를 붙여 넣을 수 있습니다.

목록보기에서 singleChoiceMode를 설정 했으므로 다른 작업을 수행해야합니까? 목록 항목을 클릭하면 CheckableFrameLayout의 setChecked() 메서드가 호출되지 않습니다.

답변

0

해킹은 Checkbox 대신 CheckedTextView를 사용하는 것입니다. 나는 아직도 이유를 좁히지 않았다. 그래서 난 그냥

<CheckedTextView 
        android:id="@+id/check" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_alignParentLeft="true" 
        android:checkMark="@drawable/payment_check_drawable" 
        android:gravity="left" 
        android:layout_centerVertical="true" 
        android:layout_marginRight="10dp" 
        /> 

체크 아웃 테스트 코드에 부안

<CheckBox 
       android:id="@+id/check" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:drawableLeft="@drawable/payment_check_drawable" 
       android:button="@null" 
       android:layout_alignParentLeft="true" 
       android:layout_centerVertical="true" 
       android:layout_marginRight="10dp" 
       /> 

을 변경 here

관련 문제