双击列表视图项时获取选定项 [英] Get selected item when double click on listview item

查看:37
本文介绍了双击列表视图项时获取选定项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是显示列表视图项和 onclick 侦听器操作的代码.

Here is the code to display listview items and onclick listener action.

ListView list = (ListView) findViewById(R.id.list);
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.list,
                android.R.layout.simple_list_item_1);
        list.setAdapter(adapter);
        list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> l, View v, int position,
                    long id) {
                String sel = (String) adapterView
                            .getItemAtPosition(position);
                Toast.makeText(MyExample.this, "Your selection: " + sel, Toast.LENGTH_SHORT).show();
                if (sel.equals("Photos"){
                    startActivity(new Intent(MyExample.this, Photos.class));
                }   
            }

        });

现在,我需要实现仅在双击时选择列表项.我尝试使用 GestureDetector 如下:

Now, I need to implement to select the list-item only on double-tapped. I tried to use GestureDetector as follows:

GestureDetector gestureDectector = new GestureDetector(this, new GestureListener());        
list.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                gestureDectector.onTouchEvent(event);
                return true;
            }
        });



public class GestureListener extends GestureDetector.SimpleOnGestureListener {

    public boolean onDown(MotionEvent e) {
        return true;
    }

    public boolean onDoubleTap(MotionEvent e) {
        Log.d("Double_Tap", "Yes, Clicked");
        return true;
    }
}

但我不知道如何像在 ItemClickListener 中一样在 GestureDetector 实现中获取所选项目并根据所选列表项启动另一个活动.

But I don't know how to get the selected item in GestureDetector implementation like in ItemClickListener and start another activity based on the selected list-item.

请任何人帮助我.

推荐答案

在你的 onDoubleTap 方法中使用列表视图的 pointToPosition 方法:

Use the pointToPosition method of the listview in your onDoubleTap method:

int position = list.pointToPosition(e.getX(), e.getY());

这篇关于双击列表视图项时获取选定项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆