Gerald Tan
Gerald Tan

Reputation: 91

Drag and drop scrolling in a linear layout

I'm currently working on an app that allows users to add a relative layout, linear layout, or nested linear layout within a linear layout, which is a child of a scroll view. The scrolling seems to work fine when just the relative layout or linear layout is added, however, once the nested linear layout is added, the scrolling becomes a problem. Basically, I want the scroll view to start scrolling once my dragged view comes within x amount of the top/bottom of the screen. Any advice? Below is some of my code to help

    protected class myDragEventListener implements View.OnDragListener {
    // This is the method that the system calls when it dispatches a drag event to the listener.
    public boolean onDrag(View v, DragEvent event) {
        // Defines a variable to store the action type for the incoming event
        final int action = event.getAction();
        Point touchPosition = getTouchPositionFromDragEvent(v, event);
        Log.d("y", String.format("%s", String.format("%s", eventTouchYCoord)));
        //View dragView = (View) event.getLocalState();
        // Handles each of the expected events
        switch(action) {
            case DragEvent.ACTION_DRAG_STARTED:
                return true;
            case DragEvent.ACTION_DRAG_ENTERED:
                return true;
            case DragEvent.ACTION_DRAG_LOCATION:
                Log.d("point", touchPosition.toString());
                if (touchPosition.y > (absoluteBottom + mScrollDistance - 350)){
                    Log.d("bottom", "greater");
                    Log.d("actionbar", String.format("%s", actionBarHeight()));
                    homeScroll.scrollBy(0, 30);
                }
                if (touchPosition.y < (actionBarHeight + 350)){
                    Log.d("top", "greater");
                    homeScroll.scrollBy(0, -30);
                }
                break;

getTouchPositionFromDragEvent Method

public static Point getTouchPositionFromDragEvent(View item, DragEvent event) {
    Rect rItem = new Rect();
    item.getGlobalVisibleRect(rItem);
    return new Point(rItem.left + Math.round(event.getX()), rItem.top + Math.round(event.getY()));
}

onScrollChangedListener Method

    homeScroll.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
        @Override
        public void onScrollChanged() {
            mScrollDistance = homeScroll.getScrollY();
            int[] coords = new int[2];
            homeScroll.getLocationOnScreen(coords);
            absoluteTop = coords[1];
            absoluteBottom = coords[1] + homeScroll.getHeight();
        }
    });

Upvotes: 0

Views: 729

Answers (1)

Irufaan Ali
Irufaan Ali

Reputation: 36

Its always best practice to implement scrolling attribute in xml file. This that you want scroll in side it. or in Palette you will find scrollview option of vertical and horizontal scrolling.. just drag and drop to where you want place it.

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    </ScrollView>

Upvotes: 0

Related Questions