双击列表视图项目时获取所选项目

发布于 2024-11-24 15:55:33 字数 1693 浏览 0 评论 0原文

以下是显示列表视图项目和 onclick 侦听器操作的代码。

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:

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

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

任何人请帮助我。

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

        });

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

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.

Anyone please help me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苦妄 2024-12-01 15:55:34

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

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

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

int position = list.pointToPosition(e.getX(), e.getY());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文