自定义布局不能使用列表视图(单选)

问题描述:

我尝试过使用单选列表视图设计自定义布局,但选择模式不起作用。这里是相关的代码。自定义布局不能使用列表视图(单选)

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()方法不会被调用。

黑客是使用CheckedTextView代替复选框。我还没有缩小原因。所以,我只是改变了耳点

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

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

退房的测试代码here