Android: 无法让 Checkbox OnClickListener 在 ListFragment 中工作
Android: Can't get Checkbox OnClickListener to work in ListFragment
我浏览了几篇文章,但无法获得确认点击的复选框。我想我已经完成了 90%,但还没有跨过最后一道障碍。
我有一个 ListFragment,其中每个项目都有一个 CheckBox 和一个 TextView。当用户单击 TextView 时,他们将被带到一个新片段。但是当他们单击 CheckBox 时,我希望值直接在列表中 ticked/unticked。
这是我的代码,保留它以便仅显示与 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>
提前致谢!
在您的 list_item_task.xml
布局文件中,定义了 CheckBox
和 TextView
,您设置为 TextView
android:layout_width="match_parent"
什么时候应该像教程中那样
android:layout_width="wrap_content"
可能是太宽的文本拦截了来自复选框的点击事件。
您的点击没有成功,因为您的 CheckBox 有
android:enabled="false"
这会阻止 onClickListener 触发。
删除此行,然后重试。
此外,我建议您将 TextView 的宽度更改为 wrap_content,并将 CheckBox 置于右侧。
我浏览了几篇文章,但无法获得确认点击的复选框。我想我已经完成了 90%,但还没有跨过最后一道障碍。
我有一个 ListFragment,其中每个项目都有一个 CheckBox 和一个 TextView。当用户单击 TextView 时,他们将被带到一个新片段。但是当他们单击 CheckBox 时,我希望值直接在列表中 ticked/unticked。
这是我的代码,保留它以便仅显示与 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>
提前致谢!
在您的 list_item_task.xml
布局文件中,定义了 CheckBox
和 TextView
,您设置为 TextView
android:layout_width="match_parent"
什么时候应该像教程中那样
android:layout_width="wrap_content"
可能是太宽的文本拦截了来自复选框的点击事件。
您的点击没有成功,因为您的 CheckBox 有
android:enabled="false"
这会阻止 onClickListener 触发。
删除此行,然后重试。
此外,我建议您将 TextView 的宽度更改为 wrap_content,并将 CheckBox 置于右侧。