博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
DrawerLayout实现双向侧滑
阅读量:5126 次
发布时间:2019-06-13

本文共 6166 字,大约阅读时间需要 20 分钟。

1.首先来欣赏下效果图

2.先看看activity_main的布局,DrawerLayout里第一个布局时主内容,第二个布局是左侧,第三个布局是右侧。

1 
7 8
12 13
20
21 22
29 30
37 38

3.layout_menu是左侧布局

1 
2
6 7
12 13
16 17
25 26
35
36 37
40 41
49 50
59
60 61
64 65
73 74
83
84 85
88 89
97 98
107
108 109
112 113
121 122
131
132 133 134
1 package com.watson.lv.drawerlayoutdemo; 2  3 import android.os.Bundle; 4 import android.support.v4.app.Fragment; 5 import android.view.LayoutInflater; 6 import android.view.View; 7 import android.view.ViewGroup; 8  9 /**10  * Created by dell on 2018/3/28.11  */12 13 public class MenuLeftFragment extends Fragment14 {15 16     @Override17     public View onCreateView(LayoutInflater inflater, ViewGroup container,18                              Bundle savedInstanceState)19     {20         return inflater.inflate(R.layout.layout_menu, container, false);21     }22 }

4.menu_layout_right是右侧布局。

1 
2
7 8
15 16
21 22
28
29 30
37 38
43 44
50
51 52
59 60
65 66
72 73 74
81 82
87 88
94 95 96
1 package com.watson.lv.drawerlayoutdemo; 2  3 import android.os.Bundle; 4 import android.support.v4.app.Fragment; 5 import android.view.LayoutInflater; 6 import android.view.View; 7 import android.view.ViewGroup; 8  9 /**10  * Created by dell on 2018/3/28.11  */12 13 public class MenuRightFragment extends Fragment14 {15 16     @Override17     public View onCreateView(LayoutInflater inflater, ViewGroup container,18                              Bundle savedInstanceState)19     {20         return inflater.inflate(R.layout.menu_layout_right, container, false);21     }22 }

5.最后展示出MainActivity

1 package com.watson.lv.drawerlayoutdemo;  2   3 import android.os.Bundle;  4 import android.support.v4.app.FragmentActivity;  5 import android.support.v4.widget.DrawerLayout;  6 import android.support.v4.widget.DrawerLayout.DrawerListener;  7 import android.view.Gravity;  8 import android.view.View;  9 import android.view.Window; 10  11 import com.nineoldandroids.view.ViewHelper; 12  13 public class MainActivity extends FragmentActivity 14 { 15  16     private DrawerLayout mDrawerLayout; 17  18     @Override 19     protected void onCreate(Bundle savedInstanceState) 20     { 21         super.onCreate(savedInstanceState); 22         requestWindowFeature(Window.FEATURE_NO_TITLE); 23         setContentView(R.layout.activity_main); 24  25         initView(); 26         initEvents(); 27  28     } 29  30     public void OpenRightMenu(View view) 31     { 32         mDrawerLayout.openDrawer(Gravity.RIGHT); 33 //        mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED, 34 //                Gravity.RIGHT);  //右侧菜单只有点击才能将其弹出 35     } 36  37     private void initEvents() 38     { 39         mDrawerLayout.addDrawerListener(new DrawerListener() 40         { 41             @Override 42             public void onDrawerStateChanged(int newState) 43             { 44             } 45  46             @Override 47             public void onDrawerSlide(View drawerView, float slideOffset) 48             { 49                 View mContent = mDrawerLayout.getChildAt(0); 50                 View mMenu = drawerView; 51                 float scale = 1 - slideOffset; 52                 float rightScale = 0.8f + scale * 0.2f; 53  54                 if (drawerView.getTag().equals("LEFT")) 55                 { 56  57                     float leftScale = 1 - 0.3f * scale; 58  59                     ViewHelper.setScaleX(mMenu, leftScale); 60                     ViewHelper.setScaleY(mMenu, leftScale); 61                     ViewHelper.setAlpha(mMenu, 0.6f + 0.4f * (1 - scale)); 62                     ViewHelper.setTranslationX(mContent, 63                             mMenu.getMeasuredWidth() * (1 - scale)); 64                     ViewHelper.setPivotX(mContent, 0); 65                     ViewHelper.setPivotY(mContent, 66                             mContent.getMeasuredHeight() / 2); 67                     mContent.invalidate(); 68                     ViewHelper.setScaleX(mContent, rightScale); 69                     ViewHelper.setScaleY(mContent, rightScale); 70                 } else 71                 { 72                     ViewHelper.setTranslationX(mContent, 73                             -mMenu.getMeasuredWidth() * slideOffset); 74                     ViewHelper.setPivotX(mContent, mContent.getMeasuredWidth()); 75                     ViewHelper.setPivotY(mContent, 76                             mContent.getMeasuredHeight() / 2); 77                     mContent.invalidate(); 78                     ViewHelper.setScaleX(mContent, rightScale); 79                     ViewHelper.setScaleY(mContent, rightScale); 80                 } 81  82             } 83  84             @Override 85             public void onDrawerOpened(View drawerView) 86             { 87             } 88  89             @Override 90             public void onDrawerClosed(View drawerView) 91             { 92 //                mDrawerLayout.setDrawerLockMode( 93 //                        DrawerLayout.LOCK_MODE_LOCKED_CLOSED, Gravity.RIGHT); 94             } 95         }); 96  97  98     } 99 100     private void initView()101     {102         mDrawerLayout = (DrawerLayout) findViewById(R.id.id_drawerLayout);103 //        mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED,104 //                Gravity.RIGHT);105     }106 107 }

转载于:https://www.cnblogs.com/evolutionoflicorice/p/8669905.html

你可能感兴趣的文章
Android设计模式系列--原型模式
查看>>
免费的论文查重网站
查看>>
C语言程序第一次作业
查看>>
leetcode-Sort List
查看>>
中文词频统计
查看>>
了解node.js
查看>>
想做移动开发,先看看别人怎么做
查看>>
Eclipse相关集锦
查看>>
虚拟化架构中小型机构通用虚拟化架构
查看>>
继承条款effecitve c++ 条款41-45
查看>>
Java泛型的基本使用
查看>>
1076 Wifi密码 (15 分)
查看>>
noip模拟赛 党
查看>>
bzoj2038 [2009国家集训队]小Z的袜子(hose)
查看>>
Java反射机制及其Class类浅析
查看>>
Postman-----如何导入和导出
查看>>
移动设备显示尺寸大全 CSS3媒体查询
查看>>
图片等比例缩放及图片上下剧中
查看>>
【转载】Linux screen 命令详解
查看>>
background-clip,background-origin
查看>>