自定义View 实现抽屉式侧滑菜单 MySlidingMenu
来源:互联网 发布:keeper是什么软件 编辑:程序博客网 时间:2024/04/25 16:16
###一、截图
###二、主要技术框架 整体的侧滑菜单的实现是王一个帧布局中放置两个子布局。分别是bottom和top,我们只需要控制top的位移来显示bottom的内容就呈现了侧滑的效果。###三、关于我 目前就职于北京的一家上市公司,喜欢Android,热爱技术。 QQ:洪荒之力(2632545852) email:xuchen1009@gmail.com###四、声明 个人兴趣开发,如有雷同,纯属巧合,3q.
public class MainActivity extends AppCompatActivity { private MySlidingMenu slidingMenu; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化 slidingMenu = (MySlidingMenu) findViewById(R.id.mysliding); //映射view LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View top = inflater.inflate(R.layout.top, null); View bottom = View.inflate(this, R.layout.bottom, null); //给SlidingMenu添加两个View slidingMenu.addBottom(bottom); slidingMenu.addTop(top); }}
/** * Created by xuchen on 2016/8/17. */public class MySlidingMenu extends FrameLayout { private LinearLayout toplayout,bottomlayout;//嵌套内容的两个子布局 private Point p = new Point();//记录当前的坐标位置 private int maxWidth ; private boolean isFirst = true;//表示是否第一次 public MySlidingMenu(Context context, AttributeSet attrs) { super(context, attrs); initViews(); } //初始化的方法 private void initViews() { WindowManager manager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE); maxWidth = (int) (manager.getDefaultDisplay().getWidth()*0.7); toplayout = new LinearLayout(getContext()); //指定屏幕的宽和高等参数----LayoutParams是父类的类型 toplayout.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT)); bottomlayout = new LinearLayout(getContext()); bottomlayout.setLayoutParams(new FrameLayout.LayoutParams(maxWidth, -1)); //添加到frameLayout中 addView(bottomlayout); addView(toplayout); } //添加底部的方法 public void addBottom(View bottom){ //因为映射出来的view没有指定任何的父布局,所以没有给定具体的LayoutParams,需要自己手动给定,否则无法呈现对应布局的属性 bottom.setLayoutParams(new LinearLayout.LayoutParams(-1,-1)); bottomlayout.addView(bottom); } //添加top的方法 public void addTop(View top){ //因为映射出来的view没有指定任何的父布局,所以没有给定具体的LayoutParams,需要自己手动给定,否则无法呈现对应布局的属性 top.setLayoutParams(new LinearLayout.LayoutParams(-1,-1)); toplayout.addView(top); } //移动需要处理touch @Override public boolean dispatchTouchEvent(MotionEvent ev) { int action = ev.getAction(); switch (action){ case MotionEvent.ACTION_DOWN://按下 //记录下当前按下的位置 p.x = (int) ev.getX(); p.y = (int) ev.getY(); break; case MotionEvent.ACTION_MOVE://移动 float x = ev.getX(); float y = ev.getY(); //获取偏移量作用在toplayout中 int disX = (int) (x-p.x); int disY = (int) (y-p.y); if (disX>0){//表示从左往右进行滑动 //首先获取topLayout的layotuParams才能进行margin 的设定 FrameLayout.LayoutParams lp = (LayoutParams) toplayout.getLayoutParams(); lp.leftMargin = lp.leftMargin+disX; lp.rightMargin = lp.rightMargin-disX; //处理边界范围 if (lp.leftMargin>=maxWidth){ lp.leftMargin = maxWidth; lp.rightMargin = -maxWidth; } //从新将lp设置给topLayout toplayout.setLayoutParams(lp); //请求从新布局 requestLayout(); }else if (disX<0){//从右向左滑动 FrameLayout.LayoutParams lp = (LayoutParams) toplayout.getLayoutParams(); lp.leftMargin = lp.leftMargin+disX; lp.rightMargin = lp.rightMargin-disX; //处理边界范围 if (lp.leftMargin<=0){ lp.leftMargin = 0; lp.rightMargin = 0; } //从新将lp设置给topLayout toplayout.setLayoutParams(lp); //请求从新布局 requestLayout(); } //将点从新定义 p.x = (int) x; p.y = (int) y; break; case MotionEvent.ACTION_UP://抬起 //处理惯性 如果滑动的时候已经达到一半以上就直接滑动到最大 如果是一半一下就直接归位0 FrameLayout.LayoutParams lp = (LayoutParams) toplayout.getLayoutParams(); //判断是否滑动到最大限度的一半 if (lp.leftMargin-maxWidth/2>0){ lp.leftMargin = maxWidth; lp.rightMargin = -maxWidth; }else{ lp.leftMargin = 0; lp.rightMargin = 0; } toplayout.setLayoutParams(lp); requestLayout();; break; } return super.dispatchTouchEvent(ev); }}
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.xuchen.myslidingmenu.MainActivity"> <com.xuchen.myslidingmenu.MySlidingMenu android:id="@+id/mysliding" android:layout_width="match_parent" android:layout_height="match_parent"/></RelativeLayout>
源码链接github:
https://github.com/ITtrap/MySlidingMenu
0 0
- 自定义View 实现抽屉式侧滑菜单 MySlidingMenu
- 自定义View---抽屉效果
- Android使用DrawerLayout实现抽屉式侧滑菜单
- 【android开发】类抽屉菜单(自定义ViewGroup)的实现
- VC实现抽屉菜单
- android 抽屉式侧滑菜单
- 自定义View控件实现卫星菜单
- 自定义View:侧滑菜单动画实现
- 自定义View:侧滑菜单实现
- ViewDragHelper实现自定义view侧滑菜单
- SlidingMenu 实现抽屉样式菜单
- 下拉抽屉菜单的实现
- Android 抽屉菜单的实现
- Android-自定义滑动菜单(抽屉效果)
- android自定义viewgroup初步之一----抽屉菜单
- React Native自定义控件【底部抽屉菜单】
- React Native自定义控件【底部抽屉菜单】
- React Native自定义控件【底部抽屉菜单】
- .DS_Store 文件怎么彻底删除?
- iOS面向切面编程
- Leetcode 328. Odd Even Linked List (Medium) (cpp)
- 如何释放含有NSTimer的UITableViewCell(SubView)
- zepto tap事件的一个bug
- 自定义View 实现抽屉式侧滑菜单 MySlidingMenu
- 2809: [Apio2012]dispatching
- 3年职业规划
- Ecplise 配置Python,Jython开发环境
- 在ubuntu系统环境下搭建java环境,以及各种软件的安装
- JNDI全面总结
- 用Shiro实现身份认证
- 卡巴斯基表示NSA被泄漏的恶意软件的确真实存在
- 运动目标跟踪(十)--CSK跟踪