Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6684491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:56:46+00:00 2026-05-26T04:56:46+00:00

I want to implement a Gallery that allows the user to drag items out

  • 0

I want to implement a Gallery that allows the user to drag items out of it. This shouldn’t get in the way of scrolling/flinging.

Given the interface layout, the user can only drag items out of the Gallery in a vertical path, and scroll the Gallery horizontally.

Is this feasible? Is there an easy way of detecting horizontal movements, and defer them to the Gallery’s event handlers, and intercept vertical movements? Or do I have to override onInterceptTouchEvent() and do the math myself?

(edit: I’m giving a try to a GestureListener, overriding onFling and onScroll, and passing the events to the Gallery when the vertical scroll distance is below a threshold)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T04:56:47+00:00Added an answer on May 26, 2026 at 4:56 am

    I inherited Gallery, and overrode the onScroll method. I haven’t implemented the drop logic yet, but the dragging and scrolling work.

    When I can spare the time, I’ll write a full post in my blog with more details, and the drop mechanism. For now, a simple copy-paste in case somebody reaches this page in the future.

    To keep the behavior where it belongs, I created this DraggableView interface:

    public interface DraggableView {
        public void beforeDrag();
    
        public DragView createDragView();
        public Object   getDraggedInfo();
    
        public void afterDrop();
    }
    

    Views in the Gallery can be dragged out of the Gallery area if they implement this view. They are notified before and after, and must implement two methods:

    • createDragView() returns a DragView object. Basically, a transparent hovering bitmap to accompany the user’s movement.

    • getDraggedInfo() returns the information that should reach the drop target.

    Here’s the DragView class:

    public class DragView extends ImageView {
    
        private final LayoutParams  mLayoutParams;
    
        public DragView(Context context, Bitmap bitmap) {
            super(context);
    
            mLayoutParams = new LayoutParams();
    
            mLayoutParams.gravity = Gravity.TOP | Gravity.LEFT;
    
            mLayoutParams.height = LayoutParams.WRAP_CONTENT;
            mLayoutParams.width  = LayoutParams.WRAP_CONTENT;
    
            mLayoutParams.flags = LayoutParams.FLAG_NOT_FOCUSABLE
                                | LayoutParams.FLAG_NOT_TOUCHABLE;
    
            mLayoutParams.format = PixelFormat.TRANSLUCENT;
            mLayoutParams.windowAnimations = 0;
    
            mLayoutParams.alpha = 0.5f;
    
            setImageBitmap(bitmap);
    
            setLayoutParams(mLayoutParams);
        }
    
        public void move(int x, int y) {
            mLayoutParams.x = x;
            mLayoutParams.y = y;
        }
    }
    

    As you can see, it takes a Bitmap in construction, and creates a hovering ImageView. Finally, here is the (just implemented and not very clean) Gallery code to make it all happen:

    public class DraggableItemGallery extends Gallery {
    
        private boolean mDragging;
        private DragView mDragView;
        private DraggableView mDragViewOwner;
    
        private WindowManager mWindowManager;
    
        private boolean mScrollStarted;
    
        public DraggableItemGallery(Context context) {
            super(context);
            initialize();
        }
    
        public DraggableItemGallery(Context context, AttributeSet attrs) {
            super(context, attrs);
            initialize();
        }
    
        public DraggableItemGallery(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            initialize();
        }
    
        public void initialize() {
            mWindowManager = (WindowManager)
                getContext().getSystemService("window");
        }
    
        private void startDraggingItem(DraggableView view, int x, int y) {
            mDragging      = true;
            mDragViewOwner = view;
            mDragView      = view.createDragView();
    
            mDragView.move(x, y);
    
            mWindowManager.addView(mDragView, mDragView.getLayoutParams());
        }
    
        private void continueDraggingItem(int x, int y) {
            DragView dragView = getDragView();
    
            dragView.move(x, y);
            mWindowManager.updateViewLayout(dragView, dragView.getLayoutParams());
        }
    
        private void stopDraggingItem() {
            mDragging = false;
    
            mWindowManager.removeView(mDragView);
    
            mDragViewOwner.afterDrop();
    
            mDragView      = null;
            mDragViewOwner = null;
        }
    
        private DraggableView getDraggedItem() {
            return mDragViewOwner;
        }
    
        private DragView getDragView() {
            return mDragView;
        }
    
        private boolean isDraggingItem() {
            return (mDragging);
        }
    
        private void setScrolling(boolean scrolling) {
            mScrollStarted = scrolling;
            System.out.println("Scrolling " + scrolling);
        }
    
        private boolean isScrolling() {
            return mScrollStarted;
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent event) {
    
            if ((event.getAction() & ACTION_MASK) == ACTION_UP) {
                setScrolling(false);
    
                if (isDraggingItem())
                    stopDraggingItem();
            }
    
                return super.onTouchEvent(event);
        }
    
    
        final Rect onScroll_tempRect = new Rect();
    
        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            if (isScrolling()) {
                if (isDraggingItem()) {
                    int x = (int) e2.getX(),
                        y = (int) e2.getY();
    
                    System.out.println("Moving to " + x + " " + y);
    
                    continueDraggingItem(x, y);
                    return true;
    
                } else {
                    /* Not dragging, let the Gallery handle the event */
                    return super.onScroll(e1, e2, distanceX, distanceY);
                }
    
            } else {
                setScrolling(true);
                boolean isVertical = (Math.abs(distanceY) > Math.abs(distanceX));
    
                if (isVertical) {
                    int x = (int) e1.getX(),
                        y = (int) e1.getY();
    
                    View hitChild = null;
    
                    // A tiny optimization, declared above this method
                    final Rect hitRect = onScroll_tempRect;
    
                    for (int i = 0; i < getChildCount(); i++) {
                        View child = getChildAt(i);
                        child.getHitRect(hitRect);
    
                        if (hitRect.contains(x, y)) {
                            hitChild = child;
                            break;
                        }
                    }
    
                    if (hitChild instanceof DraggableView) {
                        startDraggingItem((DraggableView) hitChild, x, y);
                        return true;
                    }
                }
    
                /* Either the scroll is not vertical, or the point
                 * of origin is not above a DraggableView. Again,
                 * we let the Gallery handle the event.
                 */
                return super.onScroll(e1, e2, distanceX, distanceY);
            }
        }
    }
    

    Hope it helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a requirement in that I want to implement an image gallery with
given this class definition: public class Frame { IFrameStream CapturedFrom; } I want implement
Microsoft has announce that WindowsLiveID become a OpenID provider . I want implement it
I want implement paging same as safari in iphone.When user touch below button of
Trying to implement this gallery on my website. http://coffeescripter.com/code/ad-gallery/ It is noted in the
I have a simple image gallery that I've paginated, and I want to to
Using a widget.Gallery to display a horizontally scrolling list of items. I've implemented paging
i have this example of gallery, and i'm new on jQuery. I want to
I want to implement an image gallery inside my iPhone app which will only
I want to implement something like Iphone Photo Gallery... How to slide images by

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.