Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Override LongPress in ListFragment?

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?

like image 627
coder_For_Life22 Avatar asked Sep 14 '11 15:09

coder_For_Life22


2 Answers

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.

like image 39
Erich Douglass Avatar answered Nov 16 '22 10:11

Erich Douglass


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);

like image 128
Selvin Avatar answered Nov 16 '22 09:11

Selvin