开发者

Disable Long Click for Headers (or Footers) in a ListView

开发者 https://www.devze.com 2023-02-03 11:53 出处:网络
My question is similar to this one: ListView: disabling clicking/focus However, I don\'t want to disable the default onClick, just the long click. I\'ve registered my ListView for context menu creati

My question is similar to this one: ListView: disabling clicking/focus

However, I don't want to disable the default onClick, just the long click. I've registered my ListView for context menu creation, and I want to disable it for a header element (or at least change开发者_运维百科 its behavior). How would I go about this?

Thanks!


Figured it out! It wasn't obvious that the menuInfo was necessarily an AdapterContextMenuInfo (which has 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);
}


I haven't tried this but it may work.

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

Create your own ListView class which implements onLongClickListener then check to see if the View is either your Header or Footer and, if so, return 'true' to show the long-click has been 'consumed' and to indicate that no further action should be taken.

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;
    }
}


This answer is almost good, the problem with that is that the long click in not cancelled, it just have alternative behavior. Also doing nothing, will confuse the user, since the UI will act as if there is a long click but nothing would happen.

The solution is very simple.

  1. Disable both regular click and long click by adding the header/footer with that function listView.addFooterView(footer, null, false);
  2. Set the on click listener manually on the header/footer
0

精彩评论

暂无评论...
验证码 换一张
取 消