0

I try to create slide menu Right To Left slide menu.i searched custom slide menu and i found source,but this source is slide menu left to right this is a source

public class MainLayout extends LinearLayout {

    private static final int SLIDING_DURATION = 500;
    private static final int QUERY_INTERVAL = 16;
    int mainLayoutWidth;
    private View menu;
    private View content;
    private static int menuRightMargin = 5;

    private enum MenuState {
        HIDING, HIDDEN, SHOWING, SHOWN,
    };

    private int contentXOffset;
    private MenuState currentMenuState = MenuState.HIDDEN;
    private Scroller menuScroller = new Scroller(this.getContext(),
            new EaseInInterpolator());
    private Runnable menuRunnable = new MenuRunnable();
    private Handler menuHandler = new Handler();
    int prevX = 0;
    boolean isDragging = false;
    int lastDiffX = 0;

    public MainLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MainLayout(Context context) {
        super(context);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        mainLayoutWidth = MeasureSpec.getSize(widthMeasureSpec);
        menuRightMargin = mainLayoutWidth * 60 / 100;
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();

        menu = this.getChildAt(0);
        content = this.getChildAt(1);
        content.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return MainLayout.this.onContentTouch(v, event);
            }
        });
        menu.setVisibility(View.GONE);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right,
            int bottom) {
        if (changed) {
            LayoutParams contentLayoutParams = (LayoutParams) content
                    .getLayoutParams();
            contentLayoutParams.height = this.getHeight();
            contentLayoutParams.width = this.getWidth();
            LayoutParams menuLayoutParams = (LayoutParams) menu
                    .getLayoutParams();
            menuLayoutParams.height = this.getHeight();
            menuLayoutParams.width = this.getWidth() - menuRightMargin;
        }
        menu.layout(left, top, right - menuRightMargin, bottom);
        content.layout(left + contentXOffset, top, right + contentXOffset,
                bottom);

    }

    public void toggleMenu() {

        if (currentMenuState == MenuState.HIDING
                || currentMenuState == MenuState.SHOWING)
            return;

        switch (currentMenuState) {
        case HIDDEN:
            currentMenuState = MenuState.SHOWING;
            menu.setVisibility(View.VISIBLE);
            menuScroller.startScroll(0, 0, menu.getLayoutParams().width, 0,
                    SLIDING_DURATION);
            break;
        case SHOWN:
            currentMenuState = MenuState.HIDING;
            menuScroller.startScroll(contentXOffset, 0, -contentXOffset, 0,
                    SLIDING_DURATION);
            break;
        default:
            break;
        }
        menuHandler.postDelayed(menuRunnable, QUERY_INTERVAL);
        this.invalidate();
    }

    protected class MenuRunnable implements Runnable {
        @Override
        public void run() {
            boolean isScrolling = menuScroller.computeScrollOffset();
            adjustContentPosition(isScrolling);
        }
    }

    private void adjustContentPosition(boolean isScrolling) {
        int scrollerXOffset = menuScroller.getCurrX();

        content.offsetLeftAndRight(scrollerXOffset - contentXOffset);

        contentXOffset = scrollerXOffset;
        this.invalidate();
        if (isScrolling)
            menuHandler.postDelayed(menuRunnable, QUERY_INTERVAL);
        else
            this.onMenuSlidingComplete();
    }

    private void onMenuSlidingComplete() {
        switch (currentMenuState) {
        case SHOWING:
            currentMenuState = MenuState.SHOWN;
            break;
        case HIDING:
            currentMenuState = MenuState.HIDDEN;
            menu.setVisibility(View.GONE);
            break;
        default:
            return;
        }
    }

    protected class EaseInInterpolator implements Interpolator {
        @Override
        public float getInterpolation(float t) {
            return (float) Math.pow(t - 1, 5) + 1;
        }

    }

    public boolean isMenuShown() {
        return currentMenuState == MenuState.SHOWN;
    }

    public boolean onContentTouch(View v, MotionEvent event) {
        if (currentMenuState == MenuState.HIDING
                || currentMenuState == MenuState.SHOWING)
            return false;
        int curX = (int) event.getRawX();
        int diffX = 0;

        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:

            prevX = curX;
            return true;

        case MotionEvent.ACTION_MOVE:
            if (!isDragging) {
                isDragging = true;
                menu.setVisibility(View.VISIBLE);
            }
            diffX = curX - prevX;
            if (contentXOffset + diffX <= 0) {
                diffX = -contentXOffset;
            } else if (contentXOffset + diffX > mainLayoutWidth
                    - menuRightMargin) {
                diffX = mainLayoutWidth + menuRightMargin + contentXOffset;
            }
            content.offsetLeftAndRight(diffX);
            contentXOffset += diffX;
            this.invalidate();

            prevX = curX;
            lastDiffX = diffX;
            return true;

        case MotionEvent.ACTION_UP:
            Log.d("MainLayout.java onContentTouch()", "Up lastDiffX "
                    + lastDiffX);

            if (lastDiffX > 0) {
                currentMenuState = MenuState.SHOWING;
                menuScroller.startScroll(contentXOffset, 0,
                        menu.getLayoutParams().width - contentXOffset, 0,
                        SLIDING_DURATION);
            } else if (lastDiffX < 0) {
                currentMenuState = MenuState.HIDING;
                menuScroller.startScroll(contentXOffset, 0, -contentXOffset, 0,
                        SLIDING_DURATION);
            }
            menuHandler.postDelayed(menuRunnable, QUERY_INTERVAL);
            this.invalidate();
            isDragging = false;
            prevX = 0;
            lastDiffX = 0;
            return true;

        default:
            break;
        }

        return false;
    }
}

this code working perfec.i can create slide menu,but now i want to rewrite this code.i want to recieve slide menu right to left.i want swap this example. p.s i know same good examples about slide menu but i want to rewrite this source. how i can solve my problem? if anyone knows solution please help me.thanks everyone

Halvor Holsten Strand
  • 19,829
  • 17
  • 83
  • 99
donoachua
  • 193
  • 2
  • 16
  • Are you just trying to create a Navigation drawer? You can just use a [DrawerLayout](http://developer.android.com/reference/android/support/v4/widget/DrawerLayout.html) and get a fully working slide in menu with just a few lines of code. – NoChinDeluxe Nov 25 '15 at 15:07
  • @drschultz thank you i know DrawerLayout but i don't need it because i want to move frame layout in right to left like this http://alexanderblom.se/images/facebook-ios.jpeg – donoachua Nov 25 '15 at 15:15
  • Ah! Ok, so you want your content to slide, revealing the menu below? Take a look at this answer: [http://stackoverflow.com/questions/33587371/android-move-main-content-to-reveal-drawer](http://stackoverflow.com/questions/33587371/android-move-main-content-to-reveal-drawer) – NoChinDeluxe Nov 25 '15 at 15:20
  • wow this is a perfect but my drawerlayout is right side how i can rewrite this code ? drawerContent.setX(drawerView.getWidth() * (1 - slideOffset)); mainContent.setX(drawerView.getWidth() * slideOffset); @ drschultz – donoachua Nov 25 '15 at 15:44
  • @drschultz check my comment – donoachua Nov 25 '15 at 15:44
  • Too much to explain in a comment. See my answer below. Hope it helps! – NoChinDeluxe Nov 25 '15 at 16:22

1 Answers1

0

To have your main content slide, revealing a menu underneath, there is a nice example on how to do that here: https://stackoverflow.com/questions/33587371/android-move-main-content-to-reveal‌​-drawer

Since you want your menu to be revealed on the right side, you would just subtract the X calculations from the width of the parent view, which in the example is a DrawerLayout called drawerLayout. So you would make the following adjustments to these lines:

This is the original code for setting the X position of the drawer and content:

drawerContent.setX(drawerView.getWidth() * (1 - slideOffset));
mainContent.setX(drawerView.getWidth() * slideOffset);

Those calculations start the slide at position 0, and slide the content as far as the width of the drawer. Instead, we want a starting position of the width of the parent layout (our drawerLayout view), and slide back toward zero (as far as the width of the drawer). So we would change those lines to this:

drawerContent.setX(drawerLayout.getWidth() - (drawerView.getWidth() * (1 - slideOffset)));
mainContent.setX(drawerLayout.getWidth() - (drawerView.getWidth() * slideOffset));
Community
  • 1
  • 1
NoChinDeluxe
  • 3,446
  • 1
  • 16
  • 29