Question

I have this OnLongClickListener which is not getting fired. I think I have setup everything correctly and it has worked with a contextmenu before but right now it's not firing and the log is not even giving me any error.

I really hope you can give me any ideas. It is probably a silly mistake somewhere.

package fragments;

import interfaces.AsyncTaskCompleteListener;

import java.util.List;

import com.example.slidingmenu.R;

import dialog.EditDialog;

import services.GetRequest;

import model.Post;

import adapter.PostAdapter;
import android.os.Bundle;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.ProgressDialog;
import android.content.Context;
import android.util.Log;
import android.view.ActionMode;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.AdapterContextMenuInfo;

public class HomeFragment extends Fragment implements AsyncTaskCompleteListener, View.OnLongClickListener {
    private ListView mainListView;  
    private GetRequest service;
    private ProgressDialog dialog;
    private ActionMode mActionMode;
    public HomeFragment() {}

    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
            Bundle savedIntanceState) {

        View rootView = inflater.inflate(R.layout.fragment_home, container, false);
        mainListView = (ListView) rootView.findViewById(R.id.mainListView);
        mainListView.setOnLongClickListener(this);
        dialog = new ProgressDialog(getActivity());
        dialog.setMessage("Please wait...");    
        service = new GetRequest(this);
        service.execute();
        dialog.show();
        registerForContextMenu(mainListView);
        return rootView;
    }   

    @Override
    public void onCreate(Bundle savedInstanceState) {
        setHasOptionsMenu(true);
        super.onCreate(savedInstanceState);
    }

    private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {
            mActionMode = null; 
        }

        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            MenuInflater inflater = mode.getMenuInflater();
            inflater.inflate(R.menu.main, menu);
            return true;
        }

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            switch(item.getItemId()) {
                case R.id.edit:
                    Log.d("edit", "pressed");
                    FragmentManager fragmentManager = getFragmentManager();
                    EditDialog editDialog = new EditDialog();
                    FragmentTransaction transaction = fragmentManager.beginTransaction();
                    transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                    transaction.add(android.R.id.content, editDialog).addToBackStack(null).commit();
                    mode.finish();
                    return true;

                default:
                    return false;
            }
        }
    };

    @Override
    public boolean onLongClick(View v) {
        Toast.makeText(getActivity(), "PRESSED", Toast.LENGTH_SHORT).show();
        Log.d("hey", "pressed");
        if(mActionMode != null) {
            return false;
        }   
        mActionMode = getActivity().startActionMode(mActionModeCallback);
        mainListView.setSelected(true);
        return true;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.action_refresh:
            Toast.makeText(getActivity(), "Refresh!", Toast.LENGTH_SHORT).show();
            dialog = new ProgressDialog(getActivity());
            dialog.setMessage("Please wait while refreshing...");
            dialog.show();
            service = new GetRequest(this);
            service.execute();
            break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onTaskComplete(List<Post> posts) {
        PostAdapter adapter = new PostAdapter(getActivity(), R.layout.listview_item_row, posts);
        LayoutInflater inflater = (LayoutInflater)getActivity().
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View header = (View)inflater.inflate(R.layout.listview_header_row, null);
        if(mainListView.getHeaderViewsCount() == 0) {
            mainListView.addHeaderView(header);
        }
        mainListView.setAdapter(adapter);
        if(dialog.isShowing()) {
            dialog.dismiss();
        }       
    }

    @Override
    public void onTaskComplete(String result) {
        // TODO Auto-generated method stub

    }
}

Here is the xml file where I have my ListView:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ListView 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/mainListView"
        android:longClickable="true"
        android:clickable="true"
        ></ListView>

</LinearLayout>
Was it helpful?

Solution

You are attaching a listener to the entire view instead of each row. I'm not sure why nothing is being fired, but those touch events are probably being intercepted by other views.

Instead of ListView.setOnLongClickListener(), you should use ListView.setOnItemLongClickListener().

Here is the link for the Android Docs for this method.

OTHER TIPS

Since you are registerForContextMenu for your listview already. setOnLongClickListener is not longer needed. What you needs is onCreateContextMenu method and onContextItemSelected.

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo){
    // create menu item here
}

@Override
public boolean onContextItemSelected(MenuItem item){
    // do your long click listener here
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top