Long click on ListFragment
Asked Answered
C

4

34

I'm working with a ListFragment and doing a onListItemClick. Everything works fine, but now I want to use a long Item Click (e.g setOnItemLongClickListener(new OnItemLongClickListener() for an Activity). How can I use this in my fragment?

Thanks!

Crittenden answered 18/7, 2011 at 12:5 Comment(1)
I found the way: getListView().setOnItemLongClickListener(new OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> paramAdapterView, View paramView, int position, long paramLong) { //TODO return true; } });Crittenden
O
60

Yes, the solution posted by tsync works for me. I too had ran into same problem and considered that this is not possible. I tried the above suggestion as follows:

public  class ProjectsFragment extends ListFragment {

@Override
public void onActivityCreated(Bundle savedState) {
    super.onActivityCreated(savedState);

    getListView().setOnItemLongClickListener(new OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            Toast.makeText(getActivity(), "On long click listener", Toast.LENGTH_LONG).show();
            return true;
        }
    });

and it worked!

Osteitis answered 28/7, 2011 at 10:55 Comment(2)
does it matter if we return true or false? @OsteitisCarbrey
@Carbrey return false will run the onclick listener, return true prevents thisDeaton
C
13

Depending on what you want to realize you can use the given methods for context menues:

First register the View class which gets long pressed (inside your Fragment class):

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    registerForContextMenu(this.getListView());
}

Than implement these two methods, to create a context menu and do what ever you want when a menu item is clicked:

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);

    MenuInflater inflater = this.getActivity().getMenuInflater();
    inflater.inflate(R.menu.my_context_menu, menu);
}

@Override
public boolean onContextItemSelected(MenuItem item) {

    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
    switch (item.getItemId()) {

        case R.id.add: // <-- your custom menu item id here
            // do something here
            return true;

        default:
            return super.onContextItemSelected(item);
    }
}
Clite answered 25/1, 2013 at 19:49 Comment(2)
Nice! Just want to add that you can't combine registerForContextMenu with setOnItemLongClickListener. May be obvious to some, but not for me. Also it's good to know that the row id can be retrieved from info.idLanie
Today first time I've upped every answer because this was what I was going to do with the longclick event.Upright
L
11

This works for me

getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
    public boolean onItemLongClick(AdapterView<?> av, View v, int position, long id) {
        //Get your item here with the position                   
        return true;
    }
});
Luci answered 8/10, 2011 at 10:54 Comment(2)
this is better answerUpright
This code needs togo into onViewCreated in the FragmentStilbestrol
P
0

Bit late on this but i recently had an issue with the context menu in a fragment. The menu would load but only a popup floating menu with no icons etc.. The answer above helped point me in the right direction.

So with a bit of trial and error i resolved it with this:

 override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    registerForContextMenu(view)
}

And in onItemSelected:

  override fun onItemSelected(itemViewHolder: Presenter.ViewHolder?, item: Any?, rowViewHolder: RowPresenter.ViewHolder?, row: Row?) {
    itemViewHolder?.view?.setOnLongClickListener { v: View ->
        Boolean
        requireActivity().openContextMenu(itemViewHolder.view)
        true
    }
}

This solved the issue, now when i long click an item the full context menu with icons is displayed and the floating popup menu has gone.

Puton answered 25/3, 2024 at 19:30 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.