当前位置: 移动技术网 > IT编程>移动开发>Android > Android使用自定义控件HorizontalScrollView打造史上最简单的侧滑菜单

Android使用自定义控件HorizontalScrollView打造史上最简单的侧滑菜单

2019年07月24日  | 移动技术网IT编程  | 我要评论

汉唐归来,汊河,掌玩斗士网游

侧滑菜单在很多应用中都会见到,最近qq5.0侧滑还玩了点花样~~对于侧滑菜单,一般大家都会自定义viewgroup,然后隐藏菜单栏,当手指滑动时,通过scroller或者不断的改变leftmargin等实现;多少都有点复杂,完成以后还需要对滑动冲突等进行处理~~今天给大家带来一个简单的实现,史上最简单有点夸张,但是的确是我目前遇到过的最简单的一种实现~~~

1、原理分析

既然是侧滑,无非就是在巴掌大的屏幕,塞入大概两巴掌大的布局,需要滑动可以出现另一个,既然这样,大家为啥不考虑使用android提供的horizontalscrollview呢~

如果使用horizontalscrollview,还需要在action_down , action_move里面去监听,判断,不断改变控件位置了么? no!!!horizontalscrollview本身就带了滑动的功能~~

还需要自己的手动处理各种冲突么?no!!!当然了,还是需要了解下事件分发机制的~~~

2、效果图

嗯,主界面搞了qq一张图片,左边盗用了一兄弟的布局文件~~罪过~~ 谁有好看的布局、图片、图标神马的,可以给我发点,感激~

3、布局文件

<com.example.zhy_slidingmenu.slidingmenu xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="wrap_content" 
android:layout_height="fill_parent" 
android:scrollbars="none" > 
<linearlayout 
android:layout_width="wrap_content" 
android:layout_height="fill_parent" 
android:orientation="horizontal" > 
<include layout="@layout/layout_menu" /> 
<linearlayout 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="@drawable/qq" > 
</linearlayout> 
</linearlayout> 
</com.example.zhy_slidingmenu.slidingmenu> 

首先是我们的自定义view,里面一个方向水平的linearlayout,然后就是一个是菜单的布局,一个是主布局了~

4、自定义slidingmenu

接下来就是我们最核心的代码了~

package com.example.zhy_slidingmenu; 
import android.content.context; 
import android.util.attributeset; 
import android.util.typedvalue; 
import android.view.motionevent; 
import android.view.viewgroup; 
import android.widget.horizontalscrollview; 
import android.widget.linearlayout; 
import com.zhy.utils.screenutils; 
public class slidingmenu extends horizontalscrollview 
{ 
/** 
* 屏幕宽度 
*/ 
private int mscreenwidth; 
/** 
* dp 
*/ 
private int mmenurightpadding = 50; 
/** 
* 菜单的宽度 
*/ 
private int mmenuwidth; 
private int mhalfmenuwidth; 
private boolean once; 
public slidingmenu(context context, attributeset attrs) 
{ 
super(context, attrs); 
mscreenwidth = screenutils.getscreenwidth(context); 
} 
@override 
protected void onmeasure(int widthmeasurespec, int heightmeasurespec) 
{ 
/** 
* 显示的设置一个宽度 
*/ 
if (!once) 
{ 
linearlayout wrapper = (linearlayout) getchildat(0); 
viewgroup menu = (viewgroup) wrapper.getchildat(0); 
viewgroup content = (viewgroup) wrapper.getchildat(1); 
// dp to px 
mmenurightpadding = (int) typedvalue.applydimension( 
typedvalue.complex_unit_dip, mmenurightpadding, content 
.getresources().getdisplaymetrics()); 
mmenuwidth = mscreenwidth - mmenurightpadding; 
mhalfmenuwidth = mmenuwidth / 2; 
menu.getlayoutparams().width = mmenuwidth; 
content.getlayoutparams().width = mscreenwidth; 
} 
super.onmeasure(widthmeasurespec, heightmeasurespec); 
} 
@override 
protected void onlayout(boolean changed, int l, int t, int r, int b) 
{ 
super.onlayout(changed, l, t, r, b); 
if (changed) 
{ 
// 将菜单隐藏 
this.scrollto(mmenuwidth, 0); 
once = true; 
} 
} 
@override 
public boolean ontouchevent(motionevent ev) 
{ 
int action = ev.getaction(); 
switch (action) 
{ 
// up时,进行判断,如果显示区域大于菜单宽度一半则完全显示,否则隐藏 
case motionevent.action_up: 
int scrollx = getscrollx(); 
if (scrollx > mhalfmenuwidth) 
this.smoothscrollto(mmenuwidth, 0); 
else 
this.smoothscrollto(0, 0); 
return true; 
} 
return super.ontouchevent(ev); 
} 
}

哈哈,完工~上面的演示图,就用到这么点代码~~

代码怎么样,短不短~除了设置宽度这些杂七杂八的代码~正在处理滑动的代码不过10行~~我说史上最简单不为过吧~

嗯,由于代码过于短,就不解释了,大家自己看下注释~

5、扩展

嗯,就下来,我们完善下程序,我准备首先把菜单布局里面改成listview来证明我们是没有冲突的;然后添加一个属性让用户配置菜单距离右边的边距的值;再对外公布一个方法,点击自动打开菜单,供用户点击某个按钮,菜单慢慢滑出来~

1、添加自定义属性

a、首先在values文件夹下新建一个attr.xml,写入以下内容:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
<attr name="rightpadding" format="dimension" /> 
<declare-styleable name="slidingmenu"> 
<attr name="rightpadding" /> 
</declare-styleable> 
</resources> 

b、在布局中声明命名空间和使用属性

定义完了,肯定要使用么。

<com.example.zhy_slidingmenu.slidingmenu xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
xmlns:zhy="http://schemas.android.com/apk/res/com.example.zhy_slidingmenu" 
android:layout_width="wrap_content" 
android:layout_height="fill_parent" 
android:scrollbars="none" 
zhy:rightpadding="100dp" > 

可以看到我们的命名空间:xmlns:zhy="http://schemas.android.com/apk/res/com.example.zhy_slidingmenu" 是http://schemas.android.com/apk/res/加上我们的包名;

我们的属性:zhy:rightpadding="100dp"这里我设置了100dp;
注:很多人问我,没有提示咋办,这样,你clean下项目,如果你运气好,就有提示了,嗯,运气好~

c、在我们自定义类中获得属性

public slidingmenu(context context, attributeset attrs, int defstyle) 
{ 
super(context, attrs, defstyle); 
mscreenwidth = screenutils.getscreenwidth(context); 
typedarray a = context.gettheme().obtainstyledattributes(attrs, 
r.styleable.slidingmenu, defstyle, 0); 
int n = a.getindexcount(); 
for (int i = 0; i < n; i++) 
{ 
int attr = a.getindex(i); 
switch (attr) 
{ 
case r.styleable.slidingmenu_rightpadding: 
// 默认50 
mmenurightpadding = a.getdimensionpixelsize(attr, 
(int) typedvalue.applydimension( 
typedvalue.complex_unit_dip, 50f, 
getresources().getdisplaymetrics()));// 默认为10dp 
break; 
} 
} 
a.recycle(); 
}

在三个参数的构造方法中,通过typearray获取就行了~

好了,这样就行了~如果你又很多自定义属性,按照上面的步骤来就行了~~

2、对外公布一个打开菜单的方法

首先定义一个boolean isopen变量,用来标识我们当前菜单的状态~~然后记得在action_up的时候改变下状态:

case motionevent.action_up: 
int scrollx = getscrollx(); 
if (scrollx > mhalfmenuwidth) 
{ 
this.smoothscrollto(mmenuwidth, 0); 
isopen = false; 
} else 
{ 
this.smoothscrollto(0, 0); 
isopen = true; 
} 
return true; 
}

下面开始添加方法:

/** 
* 打开菜单 
*/ 
public void openmenu() 
{ 
if (isopen) 
return; 
this.smoothscrollto(0, 0); 
isopen = true; 
} 
/** 
* 关闭菜单 
*/ 
public void closemenu() 
{ 
if (isopen) 
{ 
this.smoothscrollto(mmenuwidth, 0); 
isopen = false; 
} 
} 
/** 
* 切换菜单状态 
*/ 
public void toggle() 
{ 
if (isopen) 
{ 
closemenu(); 
} else 
{ 
openmenu(); 
} 
}

顺手多添加了两个。。。

下面,我们挑一个进行测试:

主布局多添加一个按钮,用于触发togglemenu()方法

主activity

public class mainactivity extends activity 
{ 
private slidingmenu mmenu ; 
@override 
protected void oncreate(bundle savedinstancestate) 
{ 
super.oncreate(savedinstancestate); 
requestwindowfeature(window.feature_no_title); 
setcontentview(r.layout.activity_main); 
mmenu = (slidingmenu) findviewbyid(r.id.id_menu); 
} 
public void togglemenu(view view) 
{ 
mmenu.toggle(); 
} 
}

好了,看下现在的效果图:

我们把padding改成了100dp~

然后点击我们的按钮,看哈效果~~

3、添加listview测试

好了~~listview也测试完了~~大家可以根据自己的需求各种修改~~

对了,今天测试用qq的目的是为了,下次我要拿上面的代码,改造和qq5.0一模一样的效果,大家有兴趣可以提前试一试,qq的菜单好像是隐藏在主界面下面一样,给人感觉不是划出来的,我们这个例子也能做出那样的效果,拭目以待吧;剩下就是各种缩放,透明度的动画了~~~

以上内容是小编给大家分享的android使用自定义控件horizontalscrollview打造史上最简单的侧滑菜单,希望对大家有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网