ListView行中的CheckBox [英] CheckBox in a row of ListView

查看:84
本文介绍了ListView行中的CheckBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何覆盖CheckBox的默认 onClick()行为?

How do I override a CheckBox's default onClick() behaviour?

用例:我希望复选框反映在行视图中呈现的对象的某些布尔状态.当用户单击行(包括复选框)时,将运行某些进程,该进程可能会更改对象状态(布尔变量).

Use case I want the checkBox to reflect some boolean status of the object being rendered in the row view. When the user clicks on the row (including the checkBox), some process will run which may alter the objects state (the boolean variable).

现在如何防止CheckBox的默认onClick行为发生?换句话说,我希望 CheckBox.onClick()调用 theCustomAdapter.onItemClick().我该如何实现?

Now how do I prevent the CheckBox's default onClick behaviour from kicking in? In other words I want CheckBox.onClick() to call theCustomAdapter.onItemClick(). How do I achieve this?

再三考虑,是否有任何这样的窗口小部件对象仅显示开/关状态&没有实现Clickable?

On a second thought, is there any such widget object which just displays an on/off status & does not implement Clickable?

自定义适配器

public class UserListViewAdapter extends ArrayAdapter<User> implements AdapterView.OnItemClickListener
{
    private final Context context;
    private final List<User> users;

    public UserListViewAdapter(Context context, int resource, List<User> users)
    {
        super(context, resource, users);
        this.context = context;
        this.users = users;
    }

    public View getView(int position, View convertView, ViewGroup parent)
    {
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = layoutInflater.inflate(R.layout.user, parent, false);

//        set the loggedIn? isCurrent? radio buttons
        CheckBox checkBox = (CheckBox) row.findViewById(R.id.isLoggedIn);
        checkBox.setChecked(users.get(position).isLoggedIn());
        checkBox = (CheckBox) row.findViewById(R.id.isCurrent);
        checkBox.setChecked(users.get(position).isCurrent());

//        fill the user name
        TextView textView = (TextView) row.findViewById(R.id.userName);
        textView.setText(users.get(position).getUserName());

//        fill the user id
        textView = (TextView) row.findViewById(R.id.userId);
        textView.setText(users.get(position).getUserId());

//        return the row
        return row;
    }

//    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
    {
        Log.i("Clicked", adapterView.toString());
        Toast.makeText(this.context, "Clicked! "+l, Toast.LENGTH_LONG).show();
    }
}

自定义行视图

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
            android:gravity="center"
        android:clickable="true">
    <CheckBox android:layout_height="wrap_content"
                 android:layout_width="wrap_content"
                 android:id="@+id/isLoggedIn"
                 android:padding="4dp"
                android:gravity="center"
            android:focusable="false"
            android:clickable="false"
            android:focusableInTouchMode="false"/>
    <CheckBox android:layout_height="wrap_content"
                 android:layout_width="wrap_content"
                 android:id="@+id/isCurrent"
                 android:padding="4dp"
                 android:gravity="center"
                android:focusable="false"
                android:clickable="false"
                android:focusableInTouchMode="false"/>
    <TextView android:layout_height="wrap_content"
              android:layout_width="wrap_content"
              android:id="@+id/userName"
              android:padding="4dp"
            android:gravity="left"
            android:focusable="false"
            android:clickable="false"
            android:focusableInTouchMode="false"/>
    <TextView android:layout_height="wrap_content"
              android:layout_width="wrap_content"
              android:id="@+id/userId"
              android:padding="4dp"
            android:gravity="left"
            android:focusable="false"
            android:clickable="false"
            android:focusableInTouchMode="false"/>
</LinearLayout>

推荐答案

复选框的替代方法是切换按钮:

An alternative to CheckBoxes are Toggle Buttons:

http://developer.android.com/guide/topics/ui/controls/togglebutton.html

在示例中,您可以看到如何根据自己想设置的条件,使ToggleButton自己决定是否切换.

In the example you can see how you can make the ToggleButton decide on its own if it toggles or not, depending on whatever condition you may want to set.

这篇关于ListView行中的CheckBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆