多选ListView:只有在单击复选框而不是整行时才可以选择行吗?

时间:2014-09-09 14:11:56

标签: android android-listview android-checkbox multiple-choice

问题

就像主题所说的那样,我想仅在单击整行的相对复选框时将列表视图行标记为选中

enter image description here

有可能吗?

代码

主布局中的ListView视图:

<ListView 
        android:id="@+id/lv_anprreading_reading_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/cb_anprreading_select_all"
        android:listSelector="@drawable/list_selector_blue"
        android:choiceMode="multipleChoice"
        android:background="@drawable/grey_border_trasparent_bck" />

适配器布局:

<?xml version="1.0" encoding="utf-8"?>
<it.helian.infrastructure.FE.ui.widget.CheckableLinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll_plate_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:padding="3dp" >

    <it.helian.infrastructure.FE.ui.widget.InertCheckBox
        android:id="@+id/itemCheckBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:button="@drawable/checkbox_white"
        android:focusable="false" />  
    <TextView
        android:id="@+id/tv_plate_plate" 
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".30"
        android:text="@string/dummy_vehicle_plate"
        android:textSize="@dimen/anprreading_reading_text_size"
        android:textStyle="bold"
        android:focusable="false" />

    <TextView
        android:id="@+id/tv_plate_country" 
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".15"
        android:text="@string/dummy_string"
        android:textSize="@dimen/anprreading_reading_text_size"
        android:textStyle="bold"
        android:focusable="false" />

    <TextView
        android:id="@+id/tv_plate_data" 
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".45"
        android:text="@string/dummy_date"
        android:textSize="@dimen/anprreading_reading_text_size"
        android:focusable="false" />

    <ImageView 
        android:id="@+id/iv_plate_blacklist"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="fitCenter" 
        android:src="@drawable/blacklist_mark"
        android:padding="5dp"
        android:contentDescription="@string/dummy_string" 
        android:focusable="false"
        android:visibility="gone" />

    <ImageView 
        android:id="@+id/iv_plate_whitelist"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="fitCenter" 
        android:src="@drawable/whitelist_mark"
        android:padding="5dp"
        android:contentDescription="@string/dummy_string" 
        android:focusable="false"
        android:visibility="gone" />

    <ImageButton 
        android:id="@+id/ib_plate_detail"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight=".10"
        android:scaleType="fitCenter" 
        android:src="@drawable/btn_right_rect"
        android:contentDescription="@string/dummy_string" 
        android:background="@color/transparent"
        android:focusable="false" />
</it.helian.infrastructure.FE.ui.widget.CheckableLinearLayout>

InertCheckBox:

import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.widget.CheckBox;

/**
 * CheckBox that does not react to any user event in order to let the container handle them.
 */
public class InertCheckBox extends CheckBox {

    // Provide the same constructors as the superclass
    public InertCheckBox(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    // Provide the same constructors as the superclass
    public InertCheckBox(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    // Provide the same constructors as the superclass
    public InertCheckBox(Context context) {
        super(context);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // Make the checkbox not respond to any user event
        return false;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // Make the checkbox not respond to any user event
        return false;
    }

    @Override
    public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event) {
        // Make the checkbox not respond to any user event
        return false;
    }

    @Override
    public boolean onKeyPreIme(int keyCode, KeyEvent event) {
        // Make the checkbox not respond to any user event
        return false;
    }

    @Override
    public boolean onKeyShortcut(int keyCode, KeyEvent event) {
        // Make the checkbox not respond to any user event
        return false;
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        // Make the checkbox not respond to any user event
        return false;
    }

    @Override
    public boolean onTrackballEvent(MotionEvent event) {
        // Make the checkbox not respond to any user event
        return false;
    }
}

CheckableLinearLayout:

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Checkable;
import android.widget.LinearLayout;

/**
 * Extension of a linear layout to provide a checkable behaviour
 * 
 * @author marvinlabs
 */
public class CheckableLinearLayout extends LinearLayout implements Checkable {

    /**
     * Interface definition for a callback to be invoked when the checked state of a CheckableRelativeLayout changed.
     */
    public static interface OnCheckedChangeListener {
        public void onCheckedChanged(CheckableLinearLayout layout, boolean isChecked);
    }

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

    public CheckableLinearLayout(Context context, int checkableId) {
        super(context);
        initialise(null);
    }

    /*
     * @see android.widget.Checkable#isChecked()
     */
    public boolean isChecked() {
        return isChecked;
    }

    /*
     * @see android.widget.Checkable#setChecked(boolean)
     */
    public void setChecked(boolean isChecked) {
        this.isChecked = isChecked;
        for (Checkable c : checkableViews) {
            c.setChecked(isChecked);
        }

        if (onCheckedChangeListener != null) {
            onCheckedChangeListener.onCheckedChanged(this, isChecked);
        }
    }

    /*
     * @see android.widget.Checkable#toggle()
     */
    public void toggle() {
        this.isChecked = !this.isChecked;
        for (Checkable c : checkableViews) {
            c.toggle();
        }
    }

    public void setOnCheckedChangeListener(OnCheckedChangeListener onCheckedChangeListener) {
        this.onCheckedChangeListener = onCheckedChangeListener;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        final int childCount = this.getChildCount();
        for (int i = 0; i < childCount; ++i) {
            findCheckableChildren(this.getChildAt(i));
        }
    }

    /**
     * Read the custom XML attributes
     */
    private void initialise(AttributeSet attrs) {
        this.isChecked = false;
        this.checkableViews = new ArrayList<Checkable>(5);
    }

    /**
     * Add to our checkable list all the children of the view that implement the interface Checkable
     */
    private void findCheckableChildren(View v) {
        if (v instanceof Checkable) {
            this.checkableViews.add((Checkable) v);
        }

        if (v instanceof ViewGroup) {
            final ViewGroup vg = (ViewGroup) v;
            final int childCount = vg.getChildCount();
            for (int i = 0; i < childCount; ++i) {
                findCheckableChildren(vg.getChildAt(i));
            }
        }
    }

    private boolean isChecked;
    private List<Checkable> checkableViews;
    private OnCheckedChangeListener onCheckedChangeListener;
}

链接

InertCheckBox

CheckableLinearLayout

0 个答案:

没有答案