28 自定义View侧滑栏
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侧滑栏的更多相关文章
- 28自定义View 模仿联系人字母侧栏
自定义View LetterView.java package com.qf.sxy.customview02; import android.content.Context; import andr ...
- 28 自定义View画坐标和柱状图
自定义View类 RectView.java package com.qf.sxy.day29_customview.widget; import android.content.Context; i ...
- 28 自定义View流式布局
流式布局每行的行高以本行中最高的元素作为高,如果一个元素放不下到一行时直接到第二行 FlowLayoutView package com.qf.sxy.customview05.widget; imp ...
- Android 自定义View修炼-打造完美的自定义侧滑菜单/侧滑View控件
一.概述 在App中,经常会出现侧滑菜单,侧滑滑出View等效果,虽然说Android有很多第三方开源库,但是实际上 咱们可以自己也写一个自定义的侧滑View控件,其实不难,主要涉及到以下几个要点: ...
- 【Android - 自定义View】之自定义颜色渐变的Tab导航栏
首先来介绍一下这个自定义View: (1)这个自定义View的名称叫做 GradientTab ,继承自View类: (2)这个自定义View实现了颜色渐变的Tab导航栏(仿微信主菜单),用户在左右滑 ...
- Android自定义View——实现字母导航栏
1.自定义View实现字母导航栏 2.ListView实现联系人列表 3.字母导航栏滑动事件处理 4.字母导航栏与中间字母的联动 5.字母导航栏与ListView的联动 1.先看主布局,方便后面代码的 ...
- 自定义view实现侧滑菜单
自定义View public class SlidingMenu extends HorizontalScrollView { private int mScreenWidth; private in ...
- 安卓开发28:自定义View类
自定义View类 通过自定义View类,可以自定义复杂的,按照自己需求的控件. 一个简单的例子 mainActivity.java 这个里面就是最普通的代码,但是给自定义的控件加上了一个onclick ...
- 【Android进阶】使用Andbase快速开发框架实现常见侧滑栏和滑动标签页组合效果
最近闲来无事,在网上寻找源代码看,突然发现了一个国内技术牛人开发的快速开发框架Andbase,花了一天时间研究了下源码和怎么使用,现将开发常见的侧滑栏和滑动标签页组合效果的使用介绍个大家,希望可以减少 ...
随机推荐
- RPO(Relative Path Overwrite)
Conception(Relative vs Absolute) Abosolute Path: "/etc/hosts"(in Linux), "C:\Windows\ ...
- 原生JS面向对象方法实现万年历
###面向对象的方法实现万年历 实现思路: 1.创建构造函数constructor ``` function Calender(main){ this.current ...
- [Codeforces 100633J]Ceizenpok’s formula
Description 题库链接 求 \[C_n^m \mod p\] \(1\leq m\leq n\leq 10^{18},2\leq p\leq 1000000\) Solution 一般的 \ ...
- [Awson原创]网络(network)
Description Awson是某国际学校信竞组的一只菜鸡.学校为了使教育信息化,打算在学校内新建机房,并且为机房联网.但吝啬的学校又不想花费过多的开销,于是将规划 网络路线的任务交给了信竞组的A ...
- [COGS 2524]__完全平方数
Description 一个数如果是另一个整数的完全平方,那么我们就称这个数为完全平方数(Pefect Sqaure),也称平方数.小A认为所有的平方数都是很perfect的~ 于是他给了小B一个任务 ...
- [NOIp 2012]国王游戏
Description 恰逢 H 国国庆,国王邀请 n 位大臣来玩一个有奖游戏.首先,他让每个大臣在左.右手上面分别写下一个整数,国王自己也在左.右手上各写一个整数.然后,让这 n 位大臣排成一排,国 ...
- 【USACO】股票市场
题目描述 尽管奶牛天生谨慎,它们仍然在住房抵押信贷市场中大受打击,现在它们准备在股市上碰碰运 气.贝西有内部消息,她知道 S 只股票在今后 D 天内的价格. 假设在一开始,她筹集了 M 元钱,那么她该 ...
- ●POJ 1329 Circle Through Three Points
题链: http://poj.org/problem?id=1329 题解: 计算几何,求过不共线的三点的圆 就是用向量暴力算出来的东西... (设出外心M的坐标,由于$|\vec{MA}|=|\ve ...
- VK Cup 2017 - Round 1
和FallDream组队瞎打一通--B两个人写的都挂了233,最后只剩下FallDream写的A和我写的C,最后我yy了个E靠谱做法结果打挂了,结束之后改了改就A了,难受. AC:AC Rank:18 ...
- CodeForces346 C. Number Transformation II
C. Number Transformation II time limit per test 1 second memory limit per test 256 megabytes input s ...