Android:无法让 Checkbox OnClickListener 在 ListFragment 中工作
Posted
技术标签:
【中文标题】Android:无法让 Checkbox OnClickListener 在 ListFragment 中工作【英文标题】:Android: Can't get Checkbox OnClickListener to work in ListFragment 【发布时间】:2015-10-18 16:51:55 【问题描述】:我浏览了一些帖子,但无法获得确认点击的复选框。我想我已经完成了 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_
android:layout_
android:orientation="horizontal" >
<CheckBox
android:id="@+id/task_list_item_completedCheckBox"
android:layout_
android:layout_
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_
android:layout_
android:textStyle="bold"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:text="Task title" />
</LinearLayout>
提前致谢!
【问题讨论】:
您需要在复选框上使用的监听器是 OnCheckedChangeListener ! 为什么enabled=false
在你的复选框中?
【参考方案1】:
您的点击没有通过,因为您的 CheckBox 有
android:enabled="false"
这会阻止 onClickListener 触发。
删除此行,然后重试。
另外,我建议你将 TextView 的宽度更改为 wrap_content,而不是将 CheckBox 放在右侧。
【讨论】:
谢谢马尔科!从我的 XML 中删除以下行解决了这个问题。我将可聚焦和可聚焦InTouchMode 留在了我读到这些需要标记为false 否则listView 滚动不响应? android:enabled="false" 这可能是正确的,点击不会通过。我没有使用这两行,但我在我的主视图中连续使用 android:descendantFocusability="blocksDescendants" 来获得点击(这可能相当于将这两行放入 CheckBox) .【参考方案2】:在您的 list_item_task.xml
布局文件中,定义了 CheckBox
和 TextView
,您将其设置为 TextView
android:layout_
什么时候应该和教程一样
android:layout_
可能是太宽的文本从复选框中截取了点击事件。
【讨论】:
谢谢。现在改了。以上是关于Android:无法让 Checkbox OnClickListener 在 ListFragment 中工作的主要内容,如果未能解决你的问题,请参考以下文章
android如何让checkbox实现互斥以及android验证端cession登录注意事项