Android ListView 拉动刷新和滑动列表项以显示按钮

标签 android android-listview pull-to-refresh swipe-gesture

我正在开发 Android ListView。我通过 XListView 实现了下拉刷新,但现在我还想实现从左向右滑动以在这个 ListView 上显示按钮列表项。我该怎么做?或者如何在 ListView 上添加 2 个相同的库。

我的 XML ListView 是。

<com.orderlyexpo.www.listview.refresh.XListView
        android:id="@+id/lvOrders"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:divider="@color/gray_text"
        android:dividerHeight="@dimen/dp1x" /> 

最佳答案

@覆盖 公共(public) bool onInterceptTouchEvent(MotionEvent ev){ int action = MotionEventCompat.getActionMasked(ev); 最终 float x = ev.getX(); 最终 float y = ev.getY();

    if (isEnabled() && touchListener.isSwipeEnabled()) {

        if (touchState == TOUCH_STATE_SCROLLING_X) {
            return touchListener.onTouch(this, ev);
        }

        switch (action) {
            case MotionEvent.ACTION_MOVE:
                checkInMoving(x, y);
                return touchState == TOUCH_STATE_SCROLLING_Y;
            case MotionEvent.ACTION_DOWN:
                super.onInterceptTouchEvent(ev);
                touchListener.onTouch(this, ev);
                touchState = TOUCH_STATE_REST;
                lastMotionX = x;
                lastMotionY = y;
                return false;
            case MotionEvent.ACTION_CANCEL:
                touchState = TOUCH_STATE_REST;
                break;
            case MotionEvent.ACTION_UP:
                touchListener.onTouch(this, ev);
                return touchState == TOUCH_STATE_SCROLLING_Y;
            default:
                break;
        }
    }

    return super.onInterceptTouchEvent(ev);
}

/**
 * Check if the user is moving the cell
 *
 * @param x Position X
 * @param y Position Y
 */
private void checkInMoving(float x, float y) {
    final int xDiff = (int) Math.abs(x - lastMotionX);
    final int yDiff = (int) Math.abs(y - lastMotionY);

    final int touchSlop = this.touchSlop;
    boolean xMoved = xDiff > touchSlop;
    boolean yMoved = yDiff > touchSlop;

    if (xMoved) {
        touchState = TOUCH_STATE_SCROLLING_X;
        lastMotionX = x;
        lastMotionY = y;
    }

    if (yMoved) {
        touchState = TOUCH_STATE_SCROLLING_Y;
        lastMotionX = x;
        lastMotionY = y;
    }
}

Use this library

关于Android ListView 拉动刷新和滑动列表项以显示按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30187291/

相关文章:

java - 在 Facebook 上拉取、发布和更新

java - WebDriverException : It is impossible to create a new session because 'createSession' which takes HttpClient, InputStream 和 long 未找到

android - 我可以将多个 Android keystore 文件合并为一个吗?

android - 设备 minSDK 低于使用的 SDK

javascript - 如何从 Json 字符串中分离键值对

ios - refreshcontrol 结束刷新必须等待子类

php - 使用默认 HTTP 客户端的 Http Post 意外延迟 (Android)

ListView 显示重复项目?

java - SectionedListView android错误膨胀类包括

ios - 在 iOS 15 Safari 中禁用下拉刷新