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. python-文件操作和集合

    1.打开文件 如果文件不存在会报错 f = open('information.txt','r+') 2.读取文件 read 读取文件 readline 读取文件的一行内容 readlines 读取文 ...

  2. 用C# (.NET Core) 实现迭代器设计模式

    本文的概念来自深入浅出设计模式一书 项目需求 有两个饭店合并了, 它们各自有自己的菜单. 饭店合并之后要保留这两份菜单. 这两个菜单是这样的: 菜单项MenuItem的代码是这样的: 最初我们是这样设 ...

  3. 关于装双系统Ubantu16.04+Win10引导问题

    1.装完双系统,必定会遇到时间不一致的问题解决问题如下 sudo apt-get install ntpdate sudo ntpdate time.windows.com sudo hwclock ...

  4. 词向量:part 1 WordNet、SoW、BoW、TF-IDF、Hash Trick、共现矩阵、SVD

    1.基于知识的表征 如WordNet(图1-1),包含同义词集(synonym sets)和上位词(hypernyms,is a关系). 存在的问题: 作为资源来说是好的,但是它失去了词间的细微差别, ...

  5. Office 365 应用开发的 .NET Core 模板库

    概述 前不久我写过一篇文章拥抱开源,Office 365开发迎来新时代,给大家介绍了Office 365开发的典型场景是如何支持开源平台的:Office 365通过Microsoft Graph,以R ...

  6. 【USACO】 洞穴奶牛

    题目描述 贝西喜欢去洞穴探险.这次她去的地方由 N 个洞穴组成,编号分别是 1 到 N,1 号洞穴是出发 的起点. 洞穴之间由 M 条隧道相连,双向通行,第 i 条隧道连接 A i 和 B i .每条 ...

  7. hdu 5428

    题意:一个数是这n个数的乘,找出它一个不是素数的最小因子 求出所有数的所有质因子中最小的两个,相乘就是答案.如果所有数字的质因子个数不到两个,那么就是无解. #include<iostream& ...

  8. poj2406 连续重复子串

    Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 41110   Accepted: 17099 D ...

  9. 几种常用hash算法及原理

    计算理论中,没有Hash函数的说法,只有单向函数的说法.所谓的单向函数,是一个复杂的定义,大家可以去看计算理论或者密码学方面的数据.用“人 类”的语言描述单向函数就是:如果某个函数在给定输入的时候,很 ...

  10. Python中def及lambda的功能介绍

    函数def及lambda的功能介绍 1. def函数的功能介绍 1. 函数的参数 无参数函数 格式:def func_name(): '''__doc__'''#函数的说明文档(内容) express ...