简体   繁体   English

具有可单击复选框的ListView项和容器

[英]ListView item with clickable checkbox, and container

Problem 问题

I would like to create a ListView that is similiar to the stock gmail app. 我想创建一个类似于股票Gmail应用程序的ListView。 I have a CheckBox on the left, and two TextView to the right. 我在左侧有一个CheckBox,在右侧有两个TextView。 I am able to select the CheckBox, but not the rest of the layout. 我可以选择CheckBox,但不能选择其余的布局。 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 : 最终,我的适配器中的getView()方法和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 . 您可以设置onClick事件的ButtononCheckedChangeCheckBox中的getView And remember to disable focus on Clickable item such as Button and CheckBox . 并记住禁用专注于可点击项目如ButtonCheckBox Here are two very detail blog posts from Cyril Mottier . 这是Cyril Mottier撰写的两篇非常详细的博客文章。 They explain detail step and how to fix issue. 他们解释了详细步骤以及如何解决问题。 Your problem fixed with custom Button and CheckBox in Don't press everything! 使用“自定义按钮”和“复选框”解决的问题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提示和技巧#4:添加多个可单击区域

ListView Tips & Tricks #5: Enlarged touchable areas ListView提示和技巧#5:扩大了可触摸区域

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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