开发者

How to handle long tap on ListView item?

How can I c开发者_JAVA百科atch such event? onCreateContextMenu is quite similiar, but I don't need menu.


It's hard to know what you need to achieve. But my guess is that you want to perform some acion over the item that receives the long click. For that, you have two options:

  • add an AdapterView.OnItemLongClickListener. See setOnItemLongClickListener.

.

listView.setOnItemLongClickListener (new OnItemLongClickListener() {
  public boolean onItemLongClick(AdapterView parent, View view, int position, long id) {
    //do your stuff here
  }
});
  • if you are creating a custom adapter, add a View.OnLongClickListener when creating the View in the method Adapter#getView(...)


Normally, you'd associate a long click on a list view with a context menu, which you can do by registering the listView with Activity.registerForContextMenu(View view) to give a more consistent user interface experience with other android apps.

and then override the onContextItemSelected method in your app like this:

@Override
public void onCreate(Bundle savedInstanceState) {
    listView = (ListView) findViewById(R.id.your_list_view);
    registerForContextMenu(listView);
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.setHeaderTitle(getString(R.string.menu_context_title));
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.your_context_menu, menu);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();

    switch (item.getItemId()) {
    case R.id.some_item:
        // do something useful
        return true;
    default:
        return super.onContextItemSelected(item);
    }

The position in the list is also held in info.id

If you just want to capture the long click event, then I think what Snicolas is suggesting would work.


Add a custom View.OnLongClickListener to your views. It can be shared by many instances, then you can use the parameter of

onLongClick(View v)

to know which view has been clicked and react accordingly.

Regards, Stéphane


//Deleted individual cart items
    //on list view cell long press
    cartItemList.setOnItemLongClickListener (new OnItemLongClickListener() {
          @SuppressWarnings("rawtypes")
        public boolean onItemLongClick(AdapterView parent, View view, final int position, long id) {
              final CharSequence[] items = { "Delete" };

                AlertDialog.Builder builder = new AlertDialog.Builder(context);

                builder.setTitle("Action:");
                builder.setItems(items, new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int item) {
                        cart = cartList.get(position);
                        db.removeProductFromCart(context, cart);

                        new AlertDialog.Builder(context)
                        .setTitle(getString(R.string.success))
                        .setMessage(getString(R.string.item_removed))
                        .setPositiveButton("Done", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) { 
                                Intent intent = new Intent(CartDetailsActivity.this, HomeScreen.class);
                                startActivity(intent);
                            }
                         })
                         .show();

                    }

                });

                AlertDialog alert = builder.create();

                alert.show();
            //do your stuff here
              return false;
          }
        });
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜