如何水平拖动视图? [英] How can I drag a view horizontally?

查看:85
本文介绍了如何水平拖动视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我有一个TextView可以成功拖动。但我想把它移动到水平方向。如何实现?

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

谢谢,

我的代码:

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设置为句柄时,无法单击。当您为其设置一个分离的句柄时,您无法确定相对于内容句柄的位置。

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.

推荐答案

从Bluefalcon的建议开始,我想出了一些类似于:

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

这篇关于如何水平拖动视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆