转载:http://blog.csdn.net/lmj623565791/article/details/39670935
public class SlidingMenu extends HorizontalScrollView {
//屏幕宽度
private int mScreenWidth;
private int mMenuRightPadding;
//菜单宽度
private int mMenuWidth;
private int mHalfMenuWidth;
private boolean once;
private ViewGroup mContent;
private ViewGroup mLeftMenu;
private ViewGroup mRightMenu;
//接口回调
public interface OnMenuOpenListener{
/**
*
* @param isOpen true 打开菜单,false 关闭菜单
* @param flag 0 左侧,1 右侧
*/
void onMenuOpen(boolean isOpen, int flag);
}
public OnMenuOpenListener mOnMenuOpenListener;
public void setOnMenuOpenListener(OnMenuOpenListener mOnMenuOpenListener){
this.mOnMenuOpenListener = mOnMenuOpenListener;
}
public SlidingMenu(Context context){
this(context, null,0);
}
public SlidingMenu(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public SlidingMenu(Context context,AttributeSet attrs,int defStyle){
super(context,attrs,defStyle);
WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics metrics = new DisplayMetrics();
manager.getDefaultDisplay().getMetrics(metrics);
mScreenWidth = metrics.widthPixels;
TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.SlidingMenu, defStyle, 0);
int indexCount = typedArray.getIndexCount();
for (int i = 0; i < indexCount; i++) {
int attr = typedArray.getIndex(i);
switch (attr){
case R.styleable.SlidingMenu_rightpadding:
//默认50
mMenuRightPadding = typedArray.getDimensionPixelSize(attr, (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,50f,getResources().getDisplayMetrics()));
break;
}
}
typedArray.recycle();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//显示的设置一个宽度
if(!once){
LinearLayout wrapper = (LinearLayout) getChildAt(0);
mLeftMenu = (ViewGroup) wrapper.getChildAt(0);
mContent = (ViewGroup) wrapper.getChildAt(1);
mRightMenu = (ViewGroup) wrapper.getChildAt(2);
mMenuWidth = mScreenWidth - mMenuRightPadding;
mHalfMenuWidth = mMenuWidth/2;
mLeftMenu.getLayoutParams().width = mMenuWidth;
mRightMenu.getLayoutParams().width = mMenuWidth;
mContent.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;
}
}
public boolean isOpen;
private boolean isOperateRight;
private boolean isOperateLeft;
private boolean isLeftMenuOpen;
private boolean isRightMenuOpen;
@Override
public boolean onTouchEvent(MotionEvent ev) {
int actin = ev.getAction();
switch (actin) {
//up时,进行判断,如果显示区域大于区域 菜单宽度一半则完全显示,否则隐藏
case MotionEvent.ACTION_UP:
int scrollX = getScrollX();
//如果是操作左侧菜单
if (isOperateLeft) {
//如果隐藏的区域大于菜单的一半,则隐藏菜单
if (scrollX > mHalfMenuWidth) {
this.smoothScrollTo(mMenuWidth, 0);
//如果当前左侧菜单是开启状态,且mOnMenuOpenListener 不为空,则回调关闭菜单
if (isLeftMenuOpen && mOnMenuOpenListener != null){
//第一个菜单true,打开菜单,false:关闭菜单;第二个参数0 代表左侧,1代表右侧
mOnMenuOpenListener.onMenuOpen(false, 0);
}
isLeftMenuOpen =false;
} else {
//打开做左侧菜单
this.smoothScrollTo(0, 0);
//如果当前左侧菜单关闭状态,且mOnMenuOpenListener不为空,则回调打开菜单
if (!isLeftMenuOpen && mOnMenuOpenListener != null){
mOnMenuOpenListener.onMenuOpen(true,0);
}
isLeftMenuOpen = true;
}
}
//操作右侧
if(isOperateRight){
//打开右侧侧滑菜单
if(scrollX >mHalfMenuWidth + mMenuWidth){
this.smoothScrollTo(mMenuWidth+mMenuWidth,0);
}else{
this.smoothScrollTo(mMenuWidth,0);
}
}
return true;
}
return super.onTouchEvent(ev);
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
float scale = l * 1.0f / mMenuWidth;
if (l > mMenuWidth) {
isOperateRight = true;
isOperateLeft = false;
} else {
isOperateRight = false;
isOperateLeft = true;
}
// ViewHelper.setTranslationX(mContent, mMenuWidth * (scale -1));
}
}
main
<?xml version="1.0" encoding="utf-8"?>
<com.example.administrator.testapplication.SlidingMenu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:xxx="http://schemas.android.com/apk/res-auto"
android:id="@+id/id_menu"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@mipmap/img_frame_background"
android:scrollbars="none"
tools:context="com.example.administrator.testapplication.Main2Activity"
xxx:rightpadding="100dp">
<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:gravity="center"
android:orientation="horizontal"
android:background="@mipmap/eee">
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<include layout="@layout/layout_menu2" />
</LinearLayout>
</com.example.administrator.testapplication.SlidingMenu>
layout_menu
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/ddd"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:orientation="vertical"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/one"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:src="@mipmap/img_1"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_toRightOf="@id/one"
android:text="第一个Item"
android:textColor="#f0f0f0"
android:textSize="20sp"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/two"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:src="@mipmap/img_2" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_toRightOf="@id/two"
android:text="第2个Item"
android:textColor="#f0f0f0"
android:textSize="20sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/three"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:src="@mipmap/img_3" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_toRightOf="@id/three"
android:text="第3个Item"
android:textColor="#f0f0f0"
android:textSize="20sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/four"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:src="@mipmap/img_4" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_toRightOf="@id/four"
android:text="第一个Item"
android:textColor="#f0f0f0"
android:textSize="20sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/five"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:src="@mipmap/img_5" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_toRightOf="@id/five"
android:text="第5个Item"
android:textColor="#f0f0f0"
android:textSize="20sp" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
item
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textColor="#ffffff"
android:id="@android:id/text1"
android:gravity="center">
</TextView>
Main
public class Main2Activity extends ListActivity {
private SlidingMenu slidingMenu;
private List<String> mDatas = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main2);
slidingMenu = (SlidingMenu) findViewById(R.id.id_menu);
slidingMenu.setOnMenuOpenListener(new SlidingMenu.OnMenuOpenListener() {
@Override
public void onMenuOpen(boolean isOpen, int flag) {
if (isOpen)
{
Toast.makeText(getApplicationContext(),
flag == 0 ? "LeftMenu Open" : "RightMenu Open",
Toast.LENGTH_SHORT).show();
} else
{
Toast.makeText(getApplicationContext(),
flag == 0 ? "LeftMenu Close" : "RightMenu Close",
Toast.LENGTH_SHORT).show();
}
}
});
// 初始化数据
for (int i = 'A'; i <= 'Z'; i++)
{
mDatas.add((char) i + "");
}
// 设置适配器
setListAdapter(new ArrayAdapter<String>(this, R.layout.item, mDatas));
}
}
layout_menu2
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/ddd" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/one"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:src="@mipmap/img_1" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_toRightOf="@id/one"
android:text="第1个Item"
android:textColor="#000000"
android:textSize="20sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/two"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:src="@mipmap/img_2" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_toRightOf="@id/two"
android:text="第2个Item"
android:textColor="#000000"
android:textSize="20sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/three"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:src="@mipmap/img_3" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_toRightOf="@id/three"
android:text="第3个Item"
android:textColor="#000000"
android:textSize="20sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/four"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:src="@mipmap/img_4" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_toRightOf="@id/four"
android:text="第4个Item"
android:textColor="#000000"
android:textSize="20sp" />
</RelativeLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="RightMenuBtn"
/>
</LinearLayout>
</RelativeLayout>
改变LinearLayout的子View顺序
public class MyLinearLayout extends LinearLayout
{
public MyLinearLayout(Context context, AttributeSet attrs)
{
super(context, attrs);
setChildrenDrawingOrderEnabled(true);
}
@Override
protected int getChildDrawingOrder(int childCount, int i)
{
if (i == 0)
return 1;
if (i == 2)
return 2;
if (i == 1)
return 0;
return super.getChildDrawingOrder(childCount, i);
}
}
getChildDrawingOrder 用于 返回当前迭代子视图的索引.获取当前正在绘制的视图索引. 如果需要改变ViewGroup子视图绘制的顺序,则需要重载这个方法.并且需要先调用 setChildrenDrawingOrderEnabled(boolean) 方法来启用子视图排序功能.
isChildrenDrawingOrderEnabled()则是 获取当前这个ViewGroup是否是按照顺序进行绘制的