Search code examples
androidontouchlistenertouch-event

onTouch is registered right after LongTouch


I have a ListView that when I hold (long touch) on an item, the item is removed from the list.

It's registering a touch right after, while the long touch is still happening.

Here is my code -

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                Intent intent = new Intent(getApplicationContext(), MapsActivity.class);
                intent.putExtra("placeNumber", i);
                intent.putExtra("Type", "Fav");

                startActivity(intent);
            }
        });

        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                Log.i("Removing", "" + favouriteLocations.get(position));
                favouriteLocations.remove(position);
                Favourites.arrayAdapter.notifyDataSetChanged();
                return false;
            }

        });

After letting go of long touch a 'phantom' touch appears on the item where the long touch had been (a new item has moved up in the list) and the activity loads as if it has been touched.

It worked fine at one point, I'm sure I didn't change the code. Any ideas?

Thanks.


Solution

  • you need to return true from the long click listner, you must have this in the xml too android:longClickable="true" but list view is long clickable by default so no need to update xml at all.