1

Question

Like the subject says, I would like to mark the list view row as checked only when clicking the relative checkbox instaed of the entire row.

enter image description here

Is is possible?

Code

ListView view in the main layout:

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

Adapter layout:

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

Links

InertCheckBox

CheckableLinearLayout

Link 88
  • 553
  • 8
  • 27
  • So the whole row is a checkbox? – greenapps Sep 09 '14 at 15:20
  • Hmmmm... now i see `< stuff />`. Then what is your checkbox exactly? You didn't give the checkbox its own on click listener? – greenapps Sep 09 '14 at 15:28
  • I have updated the adater layout with all the real items. Well when I set android:choiceMode="multipleChoice" autonatically the row is checked when I click any point of the row itself. I just would like to prevent this – Link 88 Sep 10 '14 at 07:40
  • Why are you setting it to multiple choice? – greenapps Sep 10 '14 at 08:12
  • Because my application needs to handle multiple selection. But making the row be selected only when clicking on the row checkbox is more elegant. – Link 88 Sep 10 '14 at 08:19
  • That is no reason att all. Why wont your own checkbox be enough? They can be checked individually. – greenapps Sep 10 '14 at 12:50
  • Can you please answer me and show me your solution? Sorry but I don't get it without an example – Link 88 Sep 11 '14 at 07:10
  • Sorry, but i don't understand the problem. I suggested to remove the `android:choiceMode="multipleChoice"` from your .xml. And then tell me what does not work. In my opinion you can do without as you have your individual checkboxes already. Please try and report. – greenapps Sep 11 '14 at 07:14
  • So you are saying to: 1 remove multipleChoice 2 loop over the entire row and look for the checked state of the relative row checkbox. This way I get the selected listview row anyway. Am I right ? – Link 88 Sep 11 '14 at 07:18
  • In that way you get ALL selected rows. `loop over the entire row`? You mean all rows? – greenapps Sep 11 '14 at 07:41
  • yes... I want to retrieve all the selected rows. About your answer yes i mean all rows. I am testing your solution at the moment. I think this is easier than I though... – Link 88 Sep 11 '14 at 07:56

0 Answers0