Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the side-slip menu effect by DrawerLayout in Android

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "how to achieve side-slip menu effect in DrawerLayout in Android". The editor shows you the operation process through an actual case. The operation method is simple and fast, and it is practical. I hope this article "how to achieve side-slip menu effect in DrawerLayout in Android" can help you solve the problem.

Effect picture:

Start writing code.

DrawerLayout is included in the v4 package, so you need to add a v4 package to the project. I won't say much about how to add it.

NavigationView needs to add compile 'com.android.support:design:25.1.0' to build.gradle

Activity_main.xml

Then add a menu menu folder to the res folder, and you don't need to add what you have already added.

In the above code, there is an app:menu= "@ menu/activity_main_drawer" in NavigationView that corresponds to the following:

Add a menu file with a random name. Mine is activity_main_drawer.xml.

Activity_main_drawer.xml

Add another layout

The app:headerLayout= "@ layout/nav_header_main" in the above code corresponds to the following:

Add a layout with the name nav_header_main.xml

Nav_header_main.xml

MainActivity:

Public class MainActivity extends BaseActivity implements NavigationView.OnNavigationItemSelectedListener {private LinearLayout right; private NavigationView left; private boolean isDrawer=false; private DrawerLayout drawer; @ Override public int getLayoutId () {return R.layout.activitymainmains;} @ Override public void initPresenter () {} @ Override public void initView () {drawer = (DrawerLayout) findViewById (R.id.drawer_layout); right = (LinearLayout) findViewById (R.id.right); left = (NavigationView) findViewById (R.id.nav_view); NavigationView navigationView = (NavigationView) findViewById (R.id.nav_view) NavigationView.setNavigationItemSelectedListener (this); right.setOnTouchListener (new View.OnTouchListener () {@ Override public boolean onTouch (View view, MotionEvent motionEvent) {if (isDrawer) {return left.dispatchTouchEvent (motionEvent);} else {return false;}}); drawer.setDrawerListener (new DrawerLayout.DrawerListener () {@ Override public void onDrawerSlide (View drawerView, float slideOffset) {isDrawer=true; / / get the width and height of the screen WindowManager manager = (WindowManager) getSystemService (Context.WINDOW_SERVICE); Display display = manager.getDefaultDisplay () / / set the layout position on the right according to the width of the right on the left menu as the left of the right layout (or the width of the right is equal here) is the right right.layout (left.getRight (), 0, left.getRight () + display.getWidth (), display.getHeight ()) of the right layout;} @ Override public void onDrawerOpened (View drawerView) {} @ Override public void onDrawerClosed (View drawerView) {isDrawer=false } @ Override public void onDrawerStateChanged (int newState) {}}); @ Override public void onBackPressed () {if (drawer.isDrawerOpen (GravityCompat.START)) {drawer.closeDrawer (GravityCompat.START);} else {super.onBackPressed ();}} @ SuppressWarnings ("StatementWithEmptyBody") @ Override public boolean onNavigationItemSelected (MenuItem item) {/ / Handle navigation view item clicks here. Int id = item.getItemId () If (id = = R.id.nav_camera) {/ / Handle the camera action} else if (id = = R.id.nav_gallery) {} else if (id = = R.id.nav_slideshow) {} else if (id = = R.id.nav_manage) {} else if (id = = R.id.nav_share) {} else if (id = = R.id.nav_send) {} DrawerLayout drawer = (DrawerLayout) findViewById (R.id.drawer_layout) Drawer.closeDrawer (GravityCompat.START); return true;}

Drawer.openDrawer (GravityCompat.START); / / Open the slide menu

Drawer.closeDrawer (GravityCompat.START); / / close the slide menu

Drawer.isDrawerOpen (GravityCompat.START); / / determine whether the slide menu is open

This is the end of the introduction on "how to achieve the side-slip menu effect in DrawerLayout in Android". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report