Android ListView 滑动操作,如 Twitter 列表

发布于 2024-11-18 07:44:49 字数 208 浏览 2 评论 0原文

我正在尝试实现“滑动操作”功能 - 就像您在“新”Twitter 应用程序中看到的那样。您可以在滑动查看操作说明中找到更详细的说明。

现在,有什么想法/解决方案如何实现这个功能吗?

谢谢!

I'm trying to implement the "swipe for action" feature - like you can see in the "new" twitter app. A closer description you find at swipe for action description.

Now, are there any ideas/solution how to implement this feature?

Thanks!

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

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

发布评论

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

评论(2

岁月静好 2024-11-25 07:44:49

在每行中使用 GestureDetector,并将行内容本身包装在 ViewFlipper 中。通过滑动,切换 ViewFlipper 的子级。

我有一个 ViewSwiper ,它结合了 GestureDetector 和 ViewFlipper,但它被设计为在任一方向上工作(例如,从常规行向左或向右滑动将切换到操作),这可能或可能并不理想。但是,它应该让您了解这是如何工作的。

Use a GestureDetector in each row, and wrap the row contents itself in a ViewFlipper. On a swipe, switch children of the ViewFlipper.

I have a ViewSwiper that combines a GestureDetector and ViewFlipper, but it is designed to work in either direction (e.g., from the regular row, a swipe left or right would switch to the actions), which may or may not be desirable. But, it should give you an idea of how this might work.

折戟 2024-11-25 07:44:49

我做了一些与您需要的类似的事情 - 您可以找到一些有关那里的信息。我在那里做了一些手动的事情,但效果很好。为了您的方便,这里是我使用的代码:

OnTouchListener gestureListener = new View.OnTouchListener() {

    private int padding = 0;
    private int initialx = 0;
    private int currentx = 0;
    private ViewHolder viewHolder;

    public boolean onTouch(View v, MotionEvent event) {

        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            padding = 0;
            initialx = (int) event.getX();
            currentx = (int) event.getX();
            viewHolder = ((ViewHolder) v.getTag());
        }

        if (event.getAction() == MotionEvent.ACTION_MOVE) {
            currentx = (int) event.getX();
            padding = currentx - initialx;
        }

        if (event.getAction() == MotionEvent.ACTION_UP ||
            event.getAction() == MotionEvent.ACTION_CANCEL) {
            padding = 0;
            initialx = 0;
            currentx = 0;
        }

        if (viewHolder != null) {
            if (padding == 0) {
                v.setBackgroundColor(0xFF000000);
                if (viewHolder.running)
                    v.setBackgroundColor(0xFF058805);
            }
            if (padding > 75) {
                viewHolder.running = true;
                v.setBackgroundColor(0xFF00FF00);
                viewHolder.icon.setImageResource(R.drawable.clock_running);
            }
            if (padding < -75) {
                viewHolder.running = false;
                v.setBackgroundColor(0xFFFF0000);
            }
            v.setPadding(padding, 0, 0, 0);
        }
        return false;
    }
};

I did something similiar to what you need - you can find some info about there. I'm doing some manual stuff there but it works nice. For your convenience here is the code I've used:

OnTouchListener gestureListener = new View.OnTouchListener() {

    private int padding = 0;
    private int initialx = 0;
    private int currentx = 0;
    private ViewHolder viewHolder;

    public boolean onTouch(View v, MotionEvent event) {

        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            padding = 0;
            initialx = (int) event.getX();
            currentx = (int) event.getX();
            viewHolder = ((ViewHolder) v.getTag());
        }

        if (event.getAction() == MotionEvent.ACTION_MOVE) {
            currentx = (int) event.getX();
            padding = currentx - initialx;
        }

        if (event.getAction() == MotionEvent.ACTION_UP ||
            event.getAction() == MotionEvent.ACTION_CANCEL) {
            padding = 0;
            initialx = 0;
            currentx = 0;
        }

        if (viewHolder != null) {
            if (padding == 0) {
                v.setBackgroundColor(0xFF000000);
                if (viewHolder.running)
                    v.setBackgroundColor(0xFF058805);
            }
            if (padding > 75) {
                viewHolder.running = true;
                v.setBackgroundColor(0xFF00FF00);
                viewHolder.icon.setImageResource(R.drawable.clock_running);
            }
            if (padding < -75) {
                viewHolder.running = false;
                v.setBackgroundColor(0xFFFF0000);
            }
            v.setPadding(padding, 0, 0, 0);
        }
        return false;
    }
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文