Bob
Bob

Reputation: 23000

How can I drag a view horizontally?

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.

Upvotes: 3

Views: 7294

Answers (5)

Frank Schwieterman
Frank Schwieterman

Reputation: 24480

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

Upvotes: 8

alchemist
alchemist

Reputation: 1081

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

Upvotes: 0

bluefalcon
bluefalcon

Reputation: 4235

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.

Upvotes: 3

Senthil Mg
Senthil Mg

Reputation: 3313

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

Upvotes: 1

subrussn90
subrussn90

Reputation: 1142

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...

Upvotes: 3

Related Questions