热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何处理列表视图项上的长点击?-HowtohandlelongtaponListViewitem?

HowcanIcatchsuchevent?onCreateContextMenuisquitesimiliar,butIdontneedmenu.我怎么能赶上这样的活

How can I catch such event? onCreateContextMenu is quite similiar, but I don't need menu.

我怎么能赶上这样的活动呢?onCreateContextMenu非常相似,但是我不需要menu。

4 个解决方案

#1


26  

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:

很难知道你需要实现什么。但我的猜测是,您希望对接收长单击的项执行一些acion。为此,你有两个选择:

  • add an AdapterView.OnItemLongClickListener. See setOnItemLongClickListener.
  • 添加一个AdapterView.OnItemLongClickListener。看到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(...)
  • 如果您正在创建一个自定义适配器,请添加一个视图。在方法适配器#getView(…)中创建视图时的OnLongClickListener

#2


14  

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.

通常,您会将列表视图上的长单击与上下文菜单相关联,这可以通过将listView注册为Activity来实现。registerForContextMenu(视图视图),以提供与其他android应用程序更一致的用户界面体验。

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

然后重写app中的oncontext titemselected方法如下:

@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

列表中的位置也在info.id中

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

如果你只是想捕捉长点击事件,那么我认为Snicolas所提出的建议是可行的。

#3


0  

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

添加一个自定义视图。OnLongClickListener你的观点。它可以被许多实例共享,然后您可以使用参数of

onLongClick(View v)

to know which view has been clicked and react accordingly.

知道已单击哪个视图并相应地作出反应。

Regards, Stéphane

问候,史蒂芬

#4


0  

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

推荐阅读
  • 标题: ... [详细]
  • 使用圣杯布局模式实现网站首页的内容布局
    本文介绍了使用圣杯布局模式实现网站首页的内容布局的方法,包括HTML部分代码和实例。同时还提供了公司新闻、最新产品、关于我们、联系我们等页面的布局示例。商品展示区包括了车里子和农家生态土鸡蛋等产品的价格信息。 ... [详细]
  • 十大经典排序算法动图演示+Python实现
    本文介绍了十大经典排序算法的原理、演示和Python实现。排序算法分为内部排序和外部排序,常见的内部排序算法有插入排序、希尔排序、选择排序、冒泡排序、归并排序、快速排序、堆排序、基数排序等。文章还解释了时间复杂度和稳定性的概念,并提供了相关的名词解释。 ... [详细]
  • Activiti7流程定义开发笔记
    本文介绍了Activiti7流程定义的开发笔记,包括流程定义的概念、使用activiti-explorer和activiti-eclipse-designer进行建模的方式,以及生成流程图的方法。还介绍了流程定义部署的概念和步骤,包括将bpmn和png文件添加部署到activiti数据库中的方法,以及使用ZIP包进行部署的方式。同时还提到了activiti.cfg.xml文件的作用。 ... [详细]
  • 带添加按钮的GridView,item的删除事件
    先上图片效果;gridView无数据时显示添加按钮,有数据时,第一格显示添加按钮,后面显示数据:布局文件:addr_manage.xml<?xmlve ... [详细]
  • Android源码中的Builder模式及其作用
    本文主要解释了什么是Builder模式以及其作用,并结合Android源码来分析Builder模式的实现。Builder模式是将产品的设计、表示和构建进行分离,通过引入建造者角色,简化了构建复杂产品的流程,并且使得产品的构建可以灵活适应变化。使用Builder模式可以解决开发者需要关注产品表示和构建步骤的问题,并且当构建流程发生变化时,无需修改代码即可适配新的构建流程。 ... [详细]
  • Question该提问来源于开源项目:react-native-device-info/react-native-device-info ... [详细]
  • GetWindowLong函数
    今天在看一个代码里头写了GetWindowLong(hwnd,0),我当时就有点费解,靠,上网搜索函数原型说明,死活找不到第 ... [详细]
  • 在Android开发中,使用Picasso库可以实现对网络图片的等比例缩放。本文介绍了使用Picasso库进行图片缩放的方法,并提供了具体的代码实现。通过获取图片的宽高,计算目标宽度和高度,并创建新图实现等比例缩放。 ... [详细]
  • IOS开发之短信发送与拨打电话的方法详解
    本文详细介绍了在IOS开发中实现短信发送和拨打电话的两种方式,一种是使用系统底层发送,虽然无法自定义短信内容和返回原应用,但是简单方便;另一种是使用第三方框架发送,需要导入MessageUI头文件,并遵守MFMessageComposeViewControllerDelegate协议,可以实现自定义短信内容和返回原应用的功能。 ... [详细]
  • 本文介绍了RxJava在Android开发中的广泛应用以及其在事件总线(Event Bus)实现中的使用方法。RxJava是一种基于观察者模式的异步java库,可以提高开发效率、降低维护成本。通过RxJava,开发者可以实现事件的异步处理和链式操作。对于已经具备RxJava基础的开发者来说,本文将详细介绍如何利用RxJava实现事件总线,并提供了使用建议。 ... [详细]
  • Iamtryingtocreateanarrayofstructinstanceslikethis:我试图创建一个这样的struct实例数组:letinstallers: ... [详细]
  • 抽空写了一个ICON图标的转换程序
    抽空写了一个ICON图标的转换程序,支持png\jpe\bmp格式到ico的转换。具体的程序就在下面,如果看的人多,过两天再把思路写一下。 ... [详细]
  • 第一步:PyQt4Designer设计程序界面该部分设计类同VisvalStudio内的设计,改下各部件的objectName!设计 ... [详细]
  • VS2010MFC(对话框:为对话框添加控件)
    转自:http:www.jizhuomi.comsoftware151.html上一讲创建了一个名为“Addition”的工程,目的是生成一个实现加法运 ... [详细]
author-avatar
cshaadi_915
这个家伙很懒,什么也没留下!
Tags | 热门标签
RankList | 热门文章
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有