如何水平拖动视图?

发布于 2024-12-11 21:30:19 字数 1453 浏览 0 评论 0原文

在 ListView 和 Gallery 等控件中,您可以沿垂直或水平方向拖动项目。

我有一个 TextView,我成功拖动了它。但我想将其水平移动。我怎样才能实现它?

谢谢,

我的代码:

public class MyDragShadowBuilder extends View.DragShadowBuilder {
    private static View view;

    public MyDragShadowBuilder(View v) {
        super(v);
        view = v;
    }

    @Override
    public void onProvideShadowMetrics(Point size, Point touch) {
        width = getView().getWidth();
        height = getView().getHeight();
        size.set(width, height);
        touch.set(width / 2, height / 2);
    }

    private int width, height;

    @Override
    public void onDrawShadow(Canvas canvas) {
        view.draw(canvas);
    }
}

在我的活动中:

tv.setOnLongClickListener(new OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            ClipData data = ClipData.newPlainText("dot",
                    "Dot : " + v.toString());
            View.DragShadowBuilder myShadow = new MyDragShadowBuilder(v);
            v.startDrag(data, myShadow,(Object) v, 0);
            return false;
            }
        });

我想模拟一个画廊。我有 10000 张照片可以用它来展示。我的画廊可以显示 10 张照片。我想水平拖动包含画廊内容的画廊的 LinearLayout,当 LinearLayout 像 SlidingDrawer 一样到达窗口末尾时,更改其内容并显示接下来的 10 张照片。 SlidingDrawer 是一个很好的解决方案,但它有一些局限性。当您将整个 LinearLayout 设置为其句柄时,它无法被单击。当你为其设置单独的句柄时,你无法确定句柄相对于内容的位置。

In controls like ListView and Gallery you can drag items in one direction vertical or horizontal.

I have a TextView that I drag it successfully. But I want to move it in horizontal direction. how can i implement it?

Thanks,

my code:

public class MyDragShadowBuilder extends View.DragShadowBuilder {
    private static View view;

    public MyDragShadowBuilder(View v) {
        super(v);
        view = v;
    }

    @Override
    public void onProvideShadowMetrics(Point size, Point touch) {
        width = getView().getWidth();
        height = getView().getHeight();
        size.set(width, height);
        touch.set(width / 2, height / 2);
    }

    private int width, height;

    @Override
    public void onDrawShadow(Canvas canvas) {
        view.draw(canvas);
    }
}

in my activity:

tv.setOnLongClickListener(new OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            ClipData data = ClipData.newPlainText("dot",
                    "Dot : " + v.toString());
            View.DragShadowBuilder myShadow = new MyDragShadowBuilder(v);
            v.startDrag(data, myShadow,(Object) v, 0);
            return false;
            }
        });

I want to simulate a Gallery. I have 10000 photos to show by it. My gallery can show 10 photos. I want to drag the LinearLayout of my gallery which is containing the gallery's content horizontally and when the LinearLayout reached to the end of window like SlidingDrawer, change its content and show the next 10 photos. SlidingDrawer is a good solution but it has some limitations. When you set your whole LinearLayout as its handle it can not be clicked. When you set a separated handle for it, you can not determine the position of handle relative to the content.

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

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

发布评论

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

评论(5

惯饮孤独 2024-12-18 21:30:19

从 Bluefalcon 的建议开始,我想出了这样的建议:

// in onCreate of the containing view
dragButton.setOnTouchListener(new DragExperimentTouchListener(dragButton.getX(), dragButton.getY()));

......

public class DragExperimentTouchListener implements View.OnTouchListener {

    public DragExperimentTouchListener(float initalX, float initialY) {
        lastX = initalX;
        lastY = initialY;
    }

    boolean isDragging = false;
    float lastX;
    float lastY;
    float deltaX;

    @Override
    public boolean onTouch(View arg0, MotionEvent arg1) {

        int action = arg1.getAction();

        if (action == MotionEvent.ACTION_DOWN && !isDragging) {
            isDragging = true; 
            deltaX = arg1.getX();
            return true;
        } else if (isDragging) {
            if (action == MotionEvent.ACTION_MOVE) {

                arg0.setX(arg0.getX() + arg1.getX() - deltaX);
                arg0.setY(arg0.getY());
                return true;
            } else if (action == MotionEvent.ACTION_UP) {
                isDragging = false;
                lastX = arg1.getX();
                lastY = arg1.getY();
                return true;
            } else if (action == MotionEvent.ACTION_CANCEL) {
                arg0.setX(lastX);
                arg0.setY(lastY);
                isDragging = false;
                return true;
            }
        }

        return false;
    }
}

Starting with Bluefalcon's advice, I came up with something like:

// in onCreate of the containing view
dragButton.setOnTouchListener(new DragExperimentTouchListener(dragButton.getX(), dragButton.getY()));

...

public class DragExperimentTouchListener implements View.OnTouchListener {

    public DragExperimentTouchListener(float initalX, float initialY) {
        lastX = initalX;
        lastY = initialY;
    }

    boolean isDragging = false;
    float lastX;
    float lastY;
    float deltaX;

    @Override
    public boolean onTouch(View arg0, MotionEvent arg1) {

        int action = arg1.getAction();

        if (action == MotionEvent.ACTION_DOWN && !isDragging) {
            isDragging = true; 
            deltaX = arg1.getX();
            return true;
        } else if (isDragging) {
            if (action == MotionEvent.ACTION_MOVE) {

                arg0.setX(arg0.getX() + arg1.getX() - deltaX);
                arg0.setY(arg0.getY());
                return true;
            } else if (action == MotionEvent.ACTION_UP) {
                isDragging = false;
                lastX = arg1.getX();
                lastY = arg1.getY();
                return true;
            } else if (action == MotionEvent.ACTION_CANCEL) {
                arg0.setX(lastX);
                arg0.setY(lastY);
                isDragging = false;
                return true;
            }
        }

        return false;
    }
}
思念满溢 2024-12-18 21:30:19

水平滚动视图是 android 中最简单的水平拖动选项...

水平滚动视图

尝试一下....在指定的范围内,给出文本视图或您喜欢使用的任何布局或小部件...甚至对于动态过程它也可以工作...

Horizontal Scroll view is the simplest horizontal drag option in android...

HORIZONTAL SCROLL VIEW

Try that....and inside the specified,give the textview or any layouts or widgets you prefer to use...and it will work even for dynamic procedures...

挽你眉间 2024-12-18 21:30:19

如果您想滚动,请按照其余部分的建议使用水平滚动视图。

但如果您的意思是“拖放”中的拖动,那么您可以执行以下操作。

  • TextView 上设置触摸监听器,
  • 监听 ACTION_DOWNACTION_MOVE 事件之后的所有事件,
  • 获取 x 和 <上述事件中的 code>y 位置
  • 使用 API setTopsetLeft 并将它们设置为 xy< /code> 你得到上面
  • 然后打电话整个视图上的 invalidate

这应该将 UI 元素拖动到屏幕周围。

If you want to Scroll the use the Horizontal scroll view as suggested by the rest.

But if you mean drag as in "drag and drop" then you can do the following.

  • Set up a touch listener on the TextView
  • Listen to all events after ACTION_DOWN and events ACTION_MOVE
  • Get the x and y position from the above events
  • use the API setTop and setLeft and set them to x and y you get above
  • then call invalidate on the whole view

This should drag the UI element around the screen.

依 靠 2024-12-18 21:30:19

Horizo​​ntalScroll 视图是完美的解决方案,请参阅此处的博客
http://android-coding.blogspot.com/2011/01 /scrollview-and-horizo​​ntalscrollview.html

HorizontalScroll view is perfect solution for this refer the blog here
http://android-coding.blogspot.com/2011/01/scrollview-and-horizontalscrollview.html

自找没趣 2024-12-18 21:30:19

您可以使用水平滚动视图来模拟水平滑动画廊。

You can use horizontal scroll view to emulate a horizontal sliding gallery.

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