Comments (16)
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.
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.
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.
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.
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.
@apascual may i know where to add ur code.
from slidingrootnav.
@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.
@apascual but can not be opened the menu by sliding ((
from slidingrootnav.
I really like that you can still interact with the active content while the drawer is open. That is really neat!
from slidingrootnav.
declare a drawer variable
private SlidingRootNav drawer;
and close the drawer when u switch fragment
drawer.closeMenu();
from slidingrootnav.
@yarolegovich looking forward to this new feature.
from slidingrootnav.
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.
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.
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.
@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.
marked, close drawer.
from slidingrootnav.
Related Issues (20)
- Change edgeTouched boolean value? HOT 1
- onKeyDown() was not be called
- Fatal Exception: java.lang.IllegalArgumentException parameter must be a descendant of this view HOT 3
- Jcenter will be deprecated as of May
- Hamburger icon doesnt work when using .withContentClickableWhenMenuOpened(false)
- Icon Colors and Title Color and Text Not Visible
- Upgrade this library
- Fragment Issue
- 依赖不成功
- Dragg the main layout
- 导入第三方库不成功
- questions HOT 1
- Type mismatch. Required: Nothing Found: DrawerAdapter.ViewHolder migrate to Kotlin
- Failed to resolve: com.yarolegovich:sliding-root-nav:1.1.1 HOT 13
- convert side menu to default navigation menu
- How can I set event listener?
- issue with landscape mode HOT 1
- Full Drag Screen
- 3D Effect
- Kotlin
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from slidingrootnav.