How to override LongPress in ListFragment?

I have a ListFragment function.

I want to create a method for onItemClickedLongPress so that the user does this. a menu will appear. I am familiar with creating a menu.

So, if someone likes it, give me further instructions on how to set Override longpress to ListFragment activity?

+6
source share
4 answers

edit: this example shows how to show something else, and then the fx system menu. 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 did I make this such a weird method? because eclipse intellisense did not glorify the β€œgood” setOnLongClickListener for the ListView (since the ListView has at least 2 setOnLongClickListener methods ... one from the View and the second from the AdapterView class) ... the simplest way allows your ListFragment implement AdapterView.OnItemLongClickListener , and then add the code getListView().setOnLongClickListener(this);

+8
source

By "long press", I think you mean the context menu. For ListFragment all you have to do is register in the context menu:

 @Override public void onActivityCreated(Bundle icicle) { registerForContextMenu(getListView()); } 

Once you do this, ListFragment should call onCreateContextMenu() and onContextItemSelected() when it detects a long press.

+5
source

Erich Douglass's modified answer is further .. for some reason, my own application will crash until I change my code and put the post in onViewCreated as follows:

 @Override public void onViewCreated (View view, Bundle savedInstanceState) { registerForContextMenu(getListView()); } 
0
source
 getListView().setOnItemLongClickListener(new OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { // Show your popout menu here. } }); 
0
source

Source: https://habr.com/ru/post/897326/


All Articles