简体   繁体   中英

ListView item with clickable checkbox, and container

Problem

I would like to create a ListView that is similiar to the stock gmail app. I have a CheckBox on the left, and two TextView to the right. I am able to select the CheckBox, but not the rest of the layout. I want to select the Checkbox to activate the Contextual Action Bar, but the rest of the raw should start another activity (two different action for the listeners). My list item looks like this: 在此处输入图片说明

Question

How can I create a list item, where different areas of the list item triggers different actions?

Code

List item layout:

<merge xmlns:android="http://schemas.android.com/apk/res/android" >
<CheckBox
    android:id="@+id/checkbox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:focusable="false" />

<TextView
    android:id="@+id/amount"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:gravity="right" />

<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="8dp"
    android:layout_toRightOf="@+id/checkbox" />
</merge>

The list item itself:

<?xml version="1.0" encoding="utf-8"?>
<hu.gulyasm.fintrac.ui.TransactionItem xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/list_item_root"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:clickable="true"
   android:padding="24dp" />

Finnaly my getView() method in the adapter, and the onClickListener :

@Override
public void onClick(View v) {
    if (v.getId() == R.id.checkbox) {
        Toast.makeText(getActivity(), "Checkbox pressed", Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(getActivity(), "Item pressed", Toast.LENGTH_SHORT).show();
    }
}

@Override
public View newView(Context arg0, Cursor arg1, ViewGroup arg2) {
    LayoutInflater layoutInflater = getActivity().getLayoutInflater();
    TransactionItem view = (TransactionItem) layoutInflater.inflate(R.layout.list_item, arg2, false);
    view.setOnClickListener(TransactionListFragment.this);
    // View checkBox = view.findViewById(R.id.checkbox);
    // checkBox.setOnClickListener(TransactionListFragment.this);
    bindToExistingView(view, arg1);
    return view;
}

Try this combo on the checkbox:

    android:focusable="false"
    android:focusableInTouchMode="false"

You can set onClick event for your Button , onCheckedChange for CheckBox in getView . And remember to disable focus on Clickable item such as Button and CheckBox . Here are two very detail blog posts from Cyril Mottier . They explain detail step and how to fix issue. Your problem fixed with custom Button and CheckBox in Don't press everything! part.

    package com.cyrilmottier.android.listviewtipsandtricks.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;

public class DontPressWithParentButton extends Button {

    public DontPressWithParentButton(Context context) {
        super(context);
    }

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

    public DontPressWithParentButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void setPressed(boolean pressed) {
        if (pressed && getParent() instanceof View && ((View) getParent()).isPressed()) {
            return;
        }
        super.setPressed(pressed);
    }

}

ListView Tips & Tricks #4: Add several clickable areas

ListView Tips & Tricks #5: Enlarged touchable areas

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM