[英]onItemClick doesn't work when onItemLongClick is present (in ListView)
我有一个ListView
,我想根据用户单击还是长按来触发不同的操作。 不幸的是,在为长按添加了侦听器之后,将永远不会调用短的侦听器。
如果我短按一下,什么也不会发生。 如果ai长按,则调用相应的方法。 我有Android 4.2.2。
我该如何解决该问题,即使快捷键起作用?
我的代码:
类声明:
public class ContactListFragment extends Fragment implements
LoaderCallbacks<Cursor>, OnItemClickListener, OnItemLongClickListener
附加侦听器:
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Gets the ListView from the View list of the parent activity
mContactsList = (ListView) getActivity().findViewById(R.id.list_contacts);
// Gets a CursorAdapter
mCursorAdapter = new CustomCursorAdapter(getActivity(),
R.layout.contact_list_item, null, FROM_COLUMNS, TO_IDS, 0);
// Sets the adapter for the ListView
mContactsList.setAdapter(mCursorAdapter);
// Set the item longclick listener to be the current fragment.
mContactsList.setOnItemLongClickListener(this);
// Set the item click listener to be the current fragment.
mContactsList.setOnItemClickListener(this);
}
听众自己:
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Log.i(ContactListFragment.class.getSimpleName(), "Short click detected.");
}
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
Log.i(ContactListFragment.class.getSimpleName(), "Long click detected.");
return false;
}
XML( contact_list_item.xml
):
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:longClickable="true"
android:textAppearance="?android:attr/textAppearanceLarge" />
(由于我对上下文菜单不感兴趣,因此在OnItemClick之后调用的Android GridView OnItemLongClick侦听器不适用。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.