I have a ListFragment Activity.
I want to create a method for onItemClickedLongPress, so that when the user does this. a menu pops up. I am familiar with creating the menu.
So if some one would please, give me further instructions on how to set Override the longpress in a ListFragment activity?
By "long press", I think you are referring to the context menu. For a ListFragment
, all you should have to do is to register for the context menu:
@Override
public void onActivityCreated(Bundle icicle) {
registerForContextMenu(getListView());
}
Once you do that, the ListFragment
should call onCreateContextMenu()
and onContextItemSelected()
when it detects a long press.
edit: this sample shows how to show something other then system menu fx. QuickAction from https://github.com/lorensiuswlt/NewQuickAction
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//.......
registerForContextMenu(getListView());
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
AdapterView.AdapterContextMenuInfo amenuInfo = (AdapterView.AdapterContextMenuInfo) menuInfo;
Object item = getListAdapter().getItem(amenuInfo.position);
//item could be Cursor/String/YourObject it depends on Adapter
//show popup fx. QuickAction from https://github.com/lorensiuswlt/NewQuickAction
QuickAction qa = new QuickAction(getActivity());
qa.setAnimStyle(QuickAction.ANIM_AUTO);
qa.show(amenuInfo.targetView);
}
EDIT:
This ansewer is not good ... why i did this such strange method? because eclipse intellisense did not propmt "good" setOnLongClickListener
for ListView
(since ListView
has at least 2 setOnLongClickListener
methods ... one from View
and second from AdapterView
class) ... the easiest way is let your ListFragment
implement AdapterView.OnItemLongClickListener
and then in onViewCreated
add code getListView().setOnLongClickListener(this);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With