Disable long click for headers (or footers) in ListView

My question is similar to this: ListView: disabling click / focus

However, I do not want to disable the default onClick, just a long click. I registered my ListView to create a context menu, and I want to disable it for the title element (or at least change its behavior). How can i do this?

Thanks!

+4
source share
3 answers

I thought! It was not obvious that menuInfo was necessarily AdapterContextMenuInfo (which has a position).

@Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); MenuInflater inflater = getMenuInflater(); if (((AdapterContextMenuInfo)menuInfo).position == 1) { inflater.inflate(R.menu.foo1, menu); return; } inflater.inflate(R.menu.foo2, menu); } 
+4
source

I have not tried this, but it can work.

The ListView extends the ViewGroup, so when the ListView is 'long-clicked', the listener must be passed, the child view of which is under a long click.

Create your own ListView class that implements onLongClickListener, then check if the View is either a header or footer, and if so, return “true” to show that the long click has been consumed and indicate what to accept next measures.

 protected MyListView extends ListView implements onLongClickListener { public boolean onLongClick (View v) { boolean longClickConsumed = false; // Check if v is either your Header or Footer // if so then set longClickConsumed to be true return longClickConsumed; } } 
+1
source

This answer is almost good, the problem is that the long click is not canceled, it just has alternative behavior. It also does nothing, it will confuse the user, as the user interface will act as if there is a long click, but nothing will happen.

The solution is very simple.

  • Disable normal click and long click by adding a header / footer using this function listView.addFooterView(footer, null, false);
  • Manually configure the on-click listener button on the header / footer
0
source

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


All Articles