Long click on ListFragment

tsync picture tsync · Jul 18, 2011 · Viewed 20.6k times · Source

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!

Answer

Narayanan picture Narayanan · Jul 28, 2011

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!