Android:无法让Checkbox OnClickListener在ListFragment中工作

时间:2015-07-28 08:30:20

标签: java android android-listview android-listfragment android-checkbox

我查看了一些帖子,但无法获取复选框以确认点击次数。我认为我90%在那里,但落在最后一道障碍上。

我有一个ListFragment,其中每个项目都有一个CheckBox和一个TextView。当用户单击TextView时,他们将被带到一个新片段。但是当他们点击CheckBox时,我希望在列表中直接勾选/取消该值。

这是我的代码,保留它以便只显示与ListView相关的类,如果您需要查看其他类,请告诉我。

我确实按照本指南操作,但无法理解为什么它不能处理我的代码:http://www.mysamplecode.com/2012/07/android-listview-checkbox-example.html

TaskListFragment.java

package com.laytonlabs.android.todotoday;

import java.util.ArrayList;

import android.annotation.TargetApi;
import android.app.Activity;
import android.graphics.drawable.LayerDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.ActionMode;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.AbsListView.MultiChoiceModeListener;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.TextView;

public class TaskListFragment extends ListFragment {
    ArrayList<Task> mTasks;
    private boolean mSubtitleVisible;
    private Callbacks mCallbacks;
    private static final String TAG = "TaskListFragment";
    private int touchPositionX;

    public interface Callbacks {
        void onTaskSelected(Task task);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mCallbacks = (Callbacks)activity;
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mCallbacks = null;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
        getActivity().setTitle(R.string.crimes_title);
        mTasks = TaskLab.get(getActivity()).getTasks();

        TaskAdapter adapter = new TaskAdapter(mTasks);
        setListAdapter(adapter);
        setRetainInstance(true);
        mSubtitleVisible = false;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.fragment_crime_list, menu);
        MenuItem showSubtitle = menu.findItem(R.id.menu_item_show_subtitle);
        if (mSubtitleVisible && showSubtitle != null) {
            showSubtitle.setTitle(R.string.hide_subtitle);
        }
    }

    @TargetApi(11)
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_item_new_crime:
                Task task = new Task();
                TaskLab.get(getActivity()).addTaskToFirst(task);
                ((TaskAdapter)getListAdapter()).notifyDataSetChanged();
                mCallbacks.onTaskSelected(task);
                return true;
            case R.id.menu_item_show_subtitle:
                if (getActivity().getActionBar().getSubtitle() == null) {
                    getActivity().getActionBar().setSubtitle(R.string.subtitle);
                    mSubtitleVisible = true;
                    item.setTitle(R.string.hide_subtitle);
                } else {
                    getActivity().getActionBar().setSubtitle(null);
                    mSubtitleVisible = false;
                    item.setTitle(R.string.show_subtitle);
                }
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        getActivity().getMenuInflater().inflate(R.menu.crime_list_item_context, menu);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo();
        int position = info.position;
        TaskAdapter adapter = (TaskAdapter)getListAdapter();
        Task task = adapter.getItem(position);

        switch (item.getItemId()) {
        case R.id.menu_item_delete_crime:
            TaskLab.get(getActivity()).deleteTask(task);
            adapter.notifyDataSetChanged();
            return true;
        }
        return super.onContextItemSelected(item);
    }

    @TargetApi(11)
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
        View v = super.onCreateView(inflater, parent, savedInstanceState);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            if (mSubtitleVisible) {
                getActivity().getActionBar().setSubtitle(R.string.subtitle);
            }
        }       

        ListView listView = (ListView)v.findViewById(android.R.id.list);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
            //Use floating context menus on Froyo and Gingerbread
            registerForContextMenu(listView);
        } else {
            //Use contextual action bar on Honeycomb and higher
            listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
            listView.setMultiChoiceModeListener(new MultiChoiceModeListener() {

                @Override
                public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
                    // TODO Auto-generated method stub
                    return false;
                }

                @Override
                public void onDestroyActionMode(ActionMode mode) {
                    // TODO Auto-generated method stub

                }

                @Override
                public boolean onCreateActionMode(ActionMode mode, Menu menu) {
                    MenuInflater inflater = mode.getMenuInflater();
                    inflater.inflate(R.menu.crime_list_item_context, menu);
                    return true;
                }

                @Override
                public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
                    switch (item.getItemId()) {
                    case R.id.menu_item_delete_crime:
                        TaskAdapter adapter = (TaskAdapter)getListAdapter();
                        TaskLab taskLab = TaskLab.get(getActivity());
                        for (int i = adapter.getCount() - 1; i >= 0; i--) {
                            if (getListView().isItemChecked(i)) {
                                taskLab.deleteTask(adapter.getItem(i));
                            }
                        }
                        mode.finish();
                        adapter.notifyDataSetChanged();
                        return true;
                    default:
                        return false;
                    }
                }

                @Override
                public void onItemCheckedStateChanged(ActionMode mode, int position,
                        long id, boolean checked) {
                    // TODO Auto-generated method stub

                }
            });

        }


        return v;
    }

    @Override
    public void onResume() {
        super.onResume();
        ((TaskAdapter)getListAdapter()).notifyDataSetChanged();
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        Task t = ((TaskAdapter)getListAdapter()).getItem(position);
        mCallbacks.onTaskSelected(t);
    }

    private class TaskAdapter extends ArrayAdapter<Task> {
        public TaskAdapter(ArrayList<Task> tasks) {
            super(getActivity(), 0, tasks);
        }

        private class ViewHolder {
            TextView titleTextView;
            CheckBox completedCheckBox;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            //If we wern't given a view, inflate one
            if (convertView == null) {
                convertView = getActivity().getLayoutInflater().inflate(R.layout.list_item_task,  null);

                holder = new ViewHolder();
                holder.titleTextView = (TextView)convertView.findViewById(R.id.task_list_item_titleTextView);
                holder.completedCheckBox = (CheckBox)convertView.findViewById(R.id.task_list_item_completedCheckBox);
                convertView.setTag(holder);

                holder.completedCheckBox.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        CheckBox cb = (CheckBox) v;
                        Task task = (Task) cb.getTag();
                        task.setCompleted(cb.isChecked());
                        Log.d(TAG, "Clicked on checkbox for: " + task.getmTitle());
                    }
                });
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            //Configure the view for this Task
            Task t = getItem(position);
            holder.titleTextView.setText(t.getmTitle());
            holder.completedCheckBox.setChecked(t.isCompleted());
            holder.completedCheckBox.setTag(t);

            return convertView;
        }
    }

    public void updateUI() {
        ((TaskAdapter)getListAdapter()).notifyDataSetChanged();
    }

}

list_item_task.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <CheckBox 
        android:id="@+id/task_list_item_completedCheckBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:enabled="false"
        android:focusable="false"  
        android:focusableInTouchMode="false"  
        android:padding="4dp" />

    <TextView 
        android:id="@+id/task_list_item_titleTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:paddingLeft="4dp"
        android:paddingRight="4dp"
        android:text="Task title" />


</LinearLayout>

提前致谢!

2 个答案:

答案 0 :(得分:2)

您的点击次数尚未到达,因为您的 CheckBox

android:enabled="false"

这会阻止 onClickListener 触发。

删除此行,然后重试。

另外,我建议您将TextView的宽度更改为wrap_content,而不是将CheckBox置于右侧。

答案 1 :(得分:1)

list_item_task.xml布局文件中,定义了CheckBoxTextView,您设置为TextView

android:layout_width="match_parent"

应该如教程中那样

android:layout_width="wrap_content"

太宽的文字可能会从复选框中截取点击事件。