Code Monkey home page Code Monkey logo

Comments (16)

apascual avatar apascual commented on May 26, 2024 3

Another solution less intrusive:

@Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (slidingRootNav != null && !slidingRootNav.isMenuHidden()) {
            boolean menuTouched = rootLinearLayout.dispatchTouchEvent(ev);
            if(!menuTouched) {
                slidingRootNav.closeMenu();
            }
            return true;
        } else {
            return super.dispatchTouchEvent(ev);
        }
    }

Where rootLinearLayout is the root layout of the menu activity...

from slidingrootnav.

zhangchaoxu avatar zhangchaoxu commented on May 26, 2024 2

provide my solution

    boolean inDrag;
    View rootDrawerView;

 private void initSlideMenu() {
            mSlideMenu = new SlidingRootNavBuilder(this)
                    .addDragStateListener(new DragStateListener() {
                        @Override
                        public void onDragStart() {
                            inDrag = true;
                        }

                        @Override
                        public void onDragEnd(boolean isMenuOpened) {
                            inDrag = false;
                        }
                    })
                    .withMenuLayout(R.layout.view_drawer)
                    .inject();

            SlidingRootNavLayout layout = mSlideMenu.getLayout();
            rootDrawerView = layout.findViewById(R.id.root_drawer);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (!inDrag && mSlideMenu != null && rootDrawerView != null && !mSlideMenu.isMenuHidden()) {
            boolean menuTouched = rootDrawerView.dispatchTouchEvent(ev);
            if (!menuTouched) {
                mSlideMenu.closeMenu();
            }
            return true;
        } else {
            return super.dispatchTouchEvent(ev);
        }
    }

from slidingrootnav.

aednlaxer avatar aednlaxer commented on May 26, 2024 1

I prefer to check touch event position and content rectangle.

Activity:

override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
	if (hasDrawer() && isTouchEventConsumedByContentMiniature(ev)) {
		return true
	} else {
		return super.dispatchTouchEvent(ev)
	}
}

Check if touch event happened inside scaled content area:

private fun isTouchEventConsumedByContentMiniature(ev: MotionEvent): Boolean {
	if (!isDrawerOpened() || contentMiniature == null) return false

	if (ev.action == MotionEvent.ACTION_DOWN) {
		val viewLocation = IntArray(2)
		contentMiniature.getLocationOnScreen(viewLocation)
		// Find view bounds rectangle
		val rect = Rect(viewLocation[0], viewLocation[1],
				(viewLocation[0] + contentMiniature.width * SCALE_FACTOR).toInt(),
				(viewLocation[1] + contentMiniature.height * SCALE_FACTOR).toInt())

		// SCALE_FACTOR is a scale value used when initializing a SlideRootNav: .withRootViewScale(SCALE_FACTOR)

		// Check if event is located inside rectangle
		if (rect.contains(ev.x.toInt(), ev.y.toInt())) {
			closeDrawer()
			return true
		}
	}

	return false
}

SlidingRootNav injects its layout to view hierarchy. It's necessary to find main content view which may have different IDs and types. View we're looking for is not R.id.drawerContentLayout and is not of HiddenMenuClickConsumer type (also injected by the library). Check Android Studio Layout Inspector for more info on view hierarchy.

contentMiniature = (0 until rootNavigation.layout.childCount)
		.map { rootNavigation.layout.getChildAt(it) }
		.firstOrNull { it.id != R.id.drawerContentLayout && it !is HiddenMenuClickConsumer }

This may look tricky but works for me.

from slidingrootnav.

dondeepak avatar dondeepak commented on May 26, 2024 1

Thanks @zhangchaoxu
i found my solution from yours
add
SlidingRootNav mSlideMenu;

and in onCreate add mSlideMenu =

mSlideMenu = new SlidingRootNavBuilder(this)
.withToolbarMenuToggle(toolbar)
.withMenuOpened(true)
.withSavedState(savedInstanceState)
.withMenuLayout(R.layout.menu_left_drawer)
.withDragDistance(140)
.withRootViewScale(0.7f)
.withRootViewElevation(10)
.withRootViewYTranslation(4
.inject();

and in onItemSelected add mSlideMenu.closeMenu();

@OverRide
public void onItemSelected(int position) {
if (position == POS_LOGOUT) {
finish();
}
else if (position == POS_ACCOUNT)
{
Fragment selectedScreen = TataFragment.createFor(screenTitles[position]);
showFragment(selectedScreen);
mSlideMenu.closeMenu();
}
else if (position == POS_DASHBOARD) {
Fragment selectedScreen = CenteredTextFragment.createFor(screenTitles[position]);
showFragment(selectedScreen);
mSlideMenu.closeMenu();
}
}
so now whenever i click menu item drawer closing automatically

from slidingrootnav.

yarolegovich avatar yarolegovich commented on May 26, 2024

Currently there is no out of the box solution. You can manually set onClickListener and do something like:

if (!nav.isMenuHidden()) {
   nav.closeMenu();
}

Will soon update the library with this feature.

from slidingrootnav.

gokulkrizh avatar gokulkrizh commented on May 26, 2024

@apascual may i know where to add ur code.

from slidingrootnav.

apascual avatar apascual commented on May 26, 2024

@gokrish6670 place it in the same activity where you are injecting the SlidingRootNavBuilder.

The code checks if rootLinearLayout consumed the touch event (meaning that something was touched on the menu), and only if not, proceed to close it.

from slidingrootnav.

qamalyanaren avatar qamalyanaren commented on May 26, 2024

@apascual but can not be opened the menu by sliding ((

from slidingrootnav.

rayliverified avatar rayliverified commented on May 26, 2024

I really like that you can still interact with the active content while the drawer is open. That is really neat!

from slidingrootnav.

AlexLionne avatar AlexLionne commented on May 26, 2024

declare a drawer variable
private SlidingRootNav drawer;

and close the drawer when u switch fragment

drawer.closeMenu();

from slidingrootnav.

zhangchaoxu avatar zhangchaoxu commented on May 26, 2024

@yarolegovich looking forward to this new feature.

from slidingrootnav.

Rob-- avatar Rob-- commented on May 26, 2024

Just ensure your activity implements the OnItemSelectedListener interface and you can close the menu when an item is selected like this:

SlidingRootNav slidingRootNav;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    slidingRootNav = new SlidingRootNavBuilder(this)
        .withToolbarMenuToggle(toolbar)
        .withMenuOpened(true)
        .withSavedState(savedInstanceState)
        .withMenuLayout(R.layout.drawer)
        .inject();
}

@Override
public void onItemSelected(int position) {
    slidingRootNav.closeMenu(true);
}

from slidingrootnav.

savurajkumar avatar savurajkumar commented on May 26, 2024

How to close menu if we click anywhere other than menu Items,
And
Now we are closing on Drag Start but I want to close drawer menu outside click

from slidingrootnav.

johnernest02 avatar johnernest02 commented on May 26, 2024

I prefer to check touch event position and content rectangle.

Activity:

override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
	if (hasDrawer() && isTouchEventConsumedByContentMiniature(ev)) {
		return true
	} else {
		return super.dispatchTouchEvent(ev)
	}
}

Check if touch event happened inside scaled content area:

private fun isTouchEventConsumedByContentMiniature(ev: MotionEvent): Boolean {
	if (!isDrawerOpened() || contentMiniature == null) return false

	if (ev.action == MotionEvent.ACTION_DOWN) {
		val viewLocation = IntArray(2)
		contentMiniature.getLocationOnScreen(viewLocation)
		// Find view bounds rectangle
		val rect = Rect(viewLocation[0], viewLocation[1],
				(viewLocation[0] + contentMiniature.width * SCALE_FACTOR).toInt(),
				(viewLocation[1] + contentMiniature.height * SCALE_FACTOR).toInt())

		// SCALE_FACTOR is a scale value used when initializing a SlideRootNav: .withRootViewScale(SCALE_FACTOR)

		// Check if event is located inside rectangle
		if (rect.contains(ev.x.toInt(), ev.y.toInt())) {
			closeDrawer()
			return true
		}
	}

	return false
}

SlidingRootNav injects its layout to view hierarchy. It's necessary to find main content view which may have different IDs and types. View we're looking for is not R.id.drawerContentLayout and is not of HiddenMenuClickConsumer type (also injected by the library). Check Android Studio Layout Inspector for more info on view hierarchy.

contentMiniature = (0 until rootNavigation.layout.childCount)
		.map { rootNavigation.layout.getChildAt(it) }
		.firstOrNull { it.id != R.id.drawerContentLayout && it !is HiddenMenuClickConsumer }

This may look tricky but works for me.

Hey @aednlaxer I can't seem to make your solution work but it is the best I can implement. The problem is the getLocationOnScreen method keeps returning 0, so the rectangle always start at the top left of the screen wherein it should be somewhere 1/3 of the screen after the scale has been applied right? Do you have any thoughts about this?

from slidingrootnav.

aednlaxer avatar aednlaxer commented on May 26, 2024

@dev-JE02 I don't have access to that code anymore so I can't help you with your issue. Is it possible to share more code or create a test project that demonstrates the issue?

from slidingrootnav.

gatspy avatar gatspy commented on May 26, 2024

marked, close drawer.

from slidingrootnav.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.