本文介绍了Android - ListView 像三星联系人 ListView 一样向左/向右滑动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,我需要一个类似于我的三星 Galaxy S 的联系人 ListView 的 ListView:

I am developing an application, and I need a ListView like conctact ListView of my Samsung Galaxy S:

当我向右滑动手指时,我可以向该联系人发送消息.

When I slide my finger to the right I can send message to this contact.

当我向右滑动手指时,我可以呼叫我的联系人.

When I slide my finger to the right I can call to my contact.

我有我的 ListView,只需要执行它的功能...

I have my ListView and only need the function for do it...

提前致谢.

PD:我搜索了很多,没有找到任何东西.最相似的:Android 资源在列表视图上轻微向左/向右滑动操作

PD:I searched a lot and have not found anything. The most similar:Resource for Android Slight Left/Right Slide action on listview

推荐答案

您可能在这里要做的是为列表视图创建一个新视图(称为 ListViewFlinger 或其他名称).然后在此视图中,覆盖其 onTouchEvent 方法并在其中放置一些代码以确定滑动手势.完成滑动手势后,触发 onSlideComplete 事件(您必须创建该侦听器),这是一个包含滑动激活内容的 ListView.

What you might what to do here is create a new view especially for the list view (call it ListViewFlinger or something). Then in this view, override its onTouchEvent method and place some code in there to determine a slide gesture. Once you have the slide gesture, fire a onSlideComplete event (you'll have to make that listener) an voialla, you a ListView with slide activated content.

float historicX = Float.NaN, historicY = Float.NaN;
static final TRIGGER_DELTA = 50; // Number of pixels to travel till trigger

@Override public boolean onTouchEvent(MotionEvent e) {

    switch (e.getAction()) {
    case MotionEvent.ACTION_DOWN:
        historicX = e.getX();
        historicY = e.getY();
        break;
    case MotionEvent.ACTION_UP:
        if (e.getX() - historicX > -TRIGGER_DELTA) {
            onSlideComplete(Direction.LEFT);
            return true;
        }
        else if (e.getX() - historicX > TRIGGER_DELTA)  {
            onSlideComplete(Direction.RIGHT);
            return true;
        } break;
    default:
        return super.onTouchEvent(e);
    }
}

enum Direction {
    LEFT, RIGHT;
}

interface OnSlideCompleteListener {
    void onSlideComplete(Direction dir);
}

这篇关于Android - ListView 像三星联系人 ListView 一样向左/向右滑动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 17:08