ScrollMenuView.java

package com.qf.sxy.customview03.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout; /**
* Created by sxy on 2016/9/29.
* 侧滑菜单 左右滚动
*/
public class ScrollMenuView extends HorizontalScrollView { //ScrollView容器 菜单 内容
private LinearLayout container;
//菜单
private LinearLayout menuLayout;
//内容
private LinearLayout contentLayout; //获取屏幕宽度
private int mScreenWidth =0;
//菜单展示出来 距离右边屏幕的距离
private int menuRightPadding = 200;
//计算出菜单的宽度
private int menuWidth = 0; //标记 是否测量过宽度 测量过 不用再次去测
private boolean isMeasure = false; private boolean isOpen = false; public ScrollMenuView(Context context) {
super(context);
} public ScrollMenuView(Context context, AttributeSet attrs) {
super(context, attrs);
//获取整个屏幕宽度
mScreenWidth = getResources().getDisplayMetrics().widthPixels;
} /**
* 测量自身容器大小
* @param widthMeasureSpec
* @param heightMeasureSpec
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec); if(!isMeasure){ //获取第一个子元素 容器Layout
container = (LinearLayout) getChildAt(0);
//获取菜单
menuLayout = (LinearLayout) container.getChildAt(0);
//获取 内容
contentLayout = (LinearLayout) container.getChildAt(1);
//得到菜单的宽度
menuWidth = mScreenWidth-menuRightPadding;
//设置菜单的宽度
menuLayout.getLayoutParams().width = menuWidth;
//设置内容宽度大小
contentLayout.getLayoutParams().width = mScreenWidth;
//标记测量过了
isMeasure = true;
} } /**
* 子View的位置
*/
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
//如果改变让ScrollView进行滑动 默认状态 隐藏
if(changed){
scrollTo(menuWidth,0);
} } @Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()){
case MotionEvent.ACTION_UP://抬起 //ScrollView滑动距离
int scrollX = getScrollX(); //x轴滑动的距离 大于菜单宽度1/2 关闭菜单
if(scrollX>menuWidth/2){
smoothScrollTo(menuWidth,0);
Log.e("fmy","关闭");
isOpen = true;
}else{
//x轴滑动的距离 小于菜单宽度1/2 打开菜单
smoothScrollTo(0,0);
Log.e("fmy","开启");
isOpen = false;
}
//Up事件自身处理
return true;
}
//Down move的事件
//交给父类处理
return super.onTouchEvent(ev);
} //开关
public void toggle(){
Log.e("AAA","==>"+isOpen);
if(isOpen){
closeContent();
}else{
openContent();
} } //打开内容
private void openContent() {
if(isOpen){
return;
}
Log.e("fmy","关闭");
smoothScrollTo(menuWidth,0);
isOpen = true;
} //打开菜单
private void closeContent() {
if(!isOpen){
return;
}
Log.e("fmy","开启");
smoothScrollTo(0,0);
isOpen = false;
}
}

MainActivity.java

package com.qf.sxy.customview03;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View; import com.qf.sxy.customview03.widget.ScrollMenuView; public class MainActivity extends AppCompatActivity { private ScrollMenuView scrollMenuView; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scrollMenuView = ((ScrollMenuView) findViewById(R.id.scrollMenuView)); } //开关
public void Myclick(View view) {
scrollMenuView.toggle();
}
}

activity_main.xml

<?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.qf.sxy.customview03.MainActivity"> <com.qf.sxy.customview03.widget.ScrollMenuView
android:id="@+id/scrollMenuView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
"<!-- android:scrollbars="none-->
<LinearLayout
android:id="@+id/container"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--菜单-->
<LinearLayout
android:id="@+id/menu_layout"
android:layout_width="wrap_content"
android:orientation="vertical"
android:background="@mipmap/img_frame_background"
android:layout_height="match_parent"> </LinearLayout>
<!--内容-->
<LinearLayout
android:id="@+id/content_layout"
android:orientation="vertical"
android:background="@mipmap/qq"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开关"
android:onClick="Myclick"/> </LinearLayout> </LinearLayout> </com.qf.sxy.customview03.widget.ScrollMenuView>
</RelativeLayout>

28 自定义View侧滑栏的更多相关文章

  1. 28自定义View 模仿联系人字母侧栏

    自定义View LetterView.java package com.qf.sxy.customview02; import android.content.Context; import andr ...

  2. 28 自定义View画坐标和柱状图

    自定义View类 RectView.java package com.qf.sxy.day29_customview.widget; import android.content.Context; i ...

  3. 28 自定义View流式布局

    流式布局每行的行高以本行中最高的元素作为高,如果一个元素放不下到一行时直接到第二行 FlowLayoutView package com.qf.sxy.customview05.widget; imp ...

  4. Android 自定义View修炼-打造完美的自定义侧滑菜单/侧滑View控件

    一.概述 在App中,经常会出现侧滑菜单,侧滑滑出View等效果,虽然说Android有很多第三方开源库,但是实际上 咱们可以自己也写一个自定义的侧滑View控件,其实不难,主要涉及到以下几个要点: ...

  5. 【Android - 自定义View】之自定义颜色渐变的Tab导航栏

    首先来介绍一下这个自定义View: (1)这个自定义View的名称叫做 GradientTab ,继承自View类: (2)这个自定义View实现了颜色渐变的Tab导航栏(仿微信主菜单),用户在左右滑 ...

  6. Android自定义View——实现字母导航栏

    1.自定义View实现字母导航栏 2.ListView实现联系人列表 3.字母导航栏滑动事件处理 4.字母导航栏与中间字母的联动 5.字母导航栏与ListView的联动 1.先看主布局,方便后面代码的 ...

  7. 自定义view实现侧滑菜单

    自定义View public class SlidingMenu extends HorizontalScrollView { private int mScreenWidth; private in ...

  8. 安卓开发28:自定义View类

    自定义View类 通过自定义View类,可以自定义复杂的,按照自己需求的控件. 一个简单的例子 mainActivity.java 这个里面就是最普通的代码,但是给自定义的控件加上了一个onclick ...

  9. 【Android进阶】使用Andbase快速开发框架实现常见侧滑栏和滑动标签页组合效果

    最近闲来无事,在网上寻找源代码看,突然发现了一个国内技术牛人开发的快速开发框架Andbase,花了一天时间研究了下源码和怎么使用,现将开发常见的侧滑栏和滑动标签页组合效果的使用介绍个大家,希望可以减少 ...

随机推荐

  1. 微信小程序之Todo

    wxAppTodos   todomvc提供了在当今大多数流行的JavaScript MV*框架概念实现的相同的Todo应用程序,觉得这个小项目挺有意思,最近在学习微信小程序,故用小程序做一版Todo ...

  2. svg从入门到装逼(一)

    svg文件是基于xml的矢量图,而canvas是基于html和js的位图.关于两者的比较,在粗就不赘述了. 1.  首先来上一个svg的基本结构: <?xml version="1.0 ...

  3. [NOI 2014]起床困难综合症

    Description 21 世纪,许多人得了一种奇怪的病:起床困难综合症,其临床表现为:起床难,起床后精神不佳.作为一名青春阳光好少年,atm 一直坚持与起床困难综合症作斗争.通过研究相关文献,他找 ...

  4. 计蒜客NOIP模拟赛(3) D1T2 信息传递

    一个数据包在一个无向网络中传递.在时刻0,该数据包将依照特定的概率随机抵达网络中的某个节点.网络可以看做一张完全带权无向图,包含N个节点,若t时刻数据包在节点i,则在t+1时刻,数据包被传递到节点j的 ...

  5. P2515 [HAOI2010]软件安装

    树形背包 #include<cstdio> #include<cstdlib> #include<algorithm> #include<cstring> ...

  6. 【NOIP2012-开车旅行】

    这道题:你不仅要学会两人交换开车,还要做到高效驾驶. ·分析:       在拨开花哨题目的迷雾之后,发现两个重要突破口:       ①从每个点开始,他们的路径是一定的,不存在决策选取.       ...

  7. SpringBoot跨域问题解决方案

    一.允许全部请求跨域许可的代码: 需要继承WebMvcConfigurerAdapter类 @Configuration public class MyWebAppConfigurer extends ...

  8. 四个常用的 Rewrite 使用范例

    一.防盗链功能只这四行就实现了防盗链,原理是利用REFERER判断网页来源,缺点是REFERER容易伪造. RewriteEngine On RewriteCond %{HTTP_REFERER} ! ...

  9. Enum枚举

    Java Enum原理 public enum Size{ SMALL, MEDIUM, LARGE, EXTRA_LARGE }; 实际上,这个声明定义的类型是一个类,它刚好有四个实例,在此尽量不要 ...

  10. 自调用匿名函数和js的Module模式

    编写自调用匿名函数的结构一般如下: :(function( window, undefined ) { // your code })(window); 传入的参数window,和参数列表中的unde ...