CheckBox in a row of ListView

不问归期 提交于 2021-01-28 21:54:51

问题


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).

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?

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

Custom Adapter

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();
    }
}

Custom Row View

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

回答1:


An alternative to CheckBoxes are Toggle Buttons:

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

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.



来源:https://stackoverflow.com/questions/18229239/checkbox-in-a-row-of-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!