Android – how do I drag the view horizontally?

In controls such as listview and gallery, you can drag items vertically or horizontally

I have a textview. I successfully drag it. But I want to move it horizontally. How do I implement it?

thank you,

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 activities:

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 display. My gallery can display 10 photos. I want to drag the LinearLayout level of my gallery to contain the contents of the gallery. When the LinearLayout reaches the end of the window like slidingdraw, change its contents and display the next 10 photos. Slidingdraw is a good solution, However, it has some limitations. When the entire LinearLayout is set as its handle, it cannot be clicked. When a separate handle is set for it, the position of the handle relative to the content cannot be determined

resolvent:

Starting with bluefalcon's suggestion, I came up with something similar:

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

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>