布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="qb.example.com.myslidingmenu.MainActivity">
<mycustom.MySlidingMenu
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbarThumbHorizontal="@android:color/transparent"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:background="#f00"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/lv_left"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</RelativeLayout>
<RelativeLayout
android:background="#0f0"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/iv"
android:visibility="gone"
android:background="@color/mytranslate"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="@+id/lv_right"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</RelativeLayout>
</LinearLayout>
</mycustom.MySlidingMenu>
</RelativeLayout>
public class MySlidingMenu extends HorizontalScrollView implements View.OnTouchListener{
private int mScreenWidth;
private int mMenuRightPadding=40;
private int mMenuWidth;
private int mHalfMenuWidth;
private boolean once;
private boolean isOpenMenu=false;
private static final int SNAP_VELOCITY=200;
private VelocityTracker mVelocityTracker;
private float xDown,xMove,xUp;
private ImageView iv;
public MySlidingMenu(Context context) {
super(context);
}
public MySlidingMenu(Context context, AttributeSet attrs) {
super(context, attrs);
WindowManager manager= (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics metrics=new DisplayMetrics();
manager.getDefaultDisplay().getMetrics(metrics);
mScreenWidth=metrics.widthPixels;
this.setOnTouchListener(this);
}
public MySlidingMenu(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@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);
iv= (ImageView) content.getChildAt(0);
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();
craeteVelocityTracker(ev);
switch (action){
case MotionEvent.ACTION_DOWN:
xDown=ev.getRawX();
Log.e("text","xDown==="+xDown);
break;
case MotionEvent.ACTION_MOVE:
xMove=getX();
break;
case MotionEvent.ACTION_UP:
xUp=ev.getRawX();
Log.e("text","xUp==="+xUp);
int scrollX=getScrollX();
Log.e("text","scrollX==="+scrollX);
if(isOpenMenu){
Log.e("text","isOpenMenu==="+isOpenMenu);
if(Math.abs(xDown-xUp)<40){
return true;
}
if((xDown-xUp>mHalfMenuWidth)||(getScrollVelocity()>SNAP_VELOCITY)&&(xDown-xUp>0)){
this.smoothScrollTo(mMenuWidth,0);
Log.e("text","回到主页");
iv.setVisibility(View.GONE);
isOpenMenu=false;
}else{
Log.e("text","打开菜单");
this.smoothScrollTo(0,0);
iv.setVisibility(View.VISIBLE);
isOpenMenu=true;
}
}else{
if(Math.abs(xDown-xUp)<40){
return true;
}
if((xUp-xDown>mHalfMenuWidth)||(getScrollVelocity()>SNAP_VELOCITY)&&(xUp-xDown>0)){
Log.e("text","打开菜单");
this.smoothScrollTo(0,0);
iv.setVisibility(View.VISIBLE);
isOpenMenu=true;
}else{
Log.e("text","回到主页");
this.smoothScrollTo(mMenuWidth,0);
iv.setVisibility(View.GONE);
isOpenMenu=false;
}
}
recyclerVelocityTracker();
return true;
}
return super.onTouchEvent(ev);
}
private void craeteVelocityTracker(MotionEvent event){
if(mVelocityTracker==null){
mVelocityTracker=VelocityTracker.obtain();
}
mVelocityTracker.addMovement(event);
}
private void recyclerVelocityTracker(){
mVelocityTracker.recycle();
mVelocityTracker=null;
}
private int getScrollVelocity(){
mVelocityTracker.computeCurrentVelocity(1000);
int velocity= (int) mVelocityTracker.getXVelocity();
return Math.abs(velocity);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
int action=ev.getAction();
switch (action){
case MotionEvent.ACTION_DOWN:
xDown=ev.getRawX();
Log.e("text","onInterceptTouchEvent()==="+xDown);
break;
case MotionEvent.ACTION_MOVE:
xMove=ev.getRawX();
// if(isOpenMenu){
// if(xMove-xDown>20){
// return true;
// }
// }else{
// if(xMove-xUp>20){
// return true;
// }
// }
break;
case MotionEvent.ACTION_UP:
xUp=ev.getRawX();
Log.e("text","onInterceptTouchEvent()==="+xUp);
if(isOpenMenu){
if(xUp>mMenuWidth){
this.smoothScrollTo(mMenuWidth,0);
iv.setVisibility(View.GONE);
return true;
}
if(xUp-xDown>20){
return true;
}
}else{
if(xDown-xUp>20){
return true;
}
}
break;
}
return super.onInterceptTouchEvent(ev);
}
public void openMenu(){
isOpenMenu=true;
this.scrollTo(0,0);
}
public void openContent(){
isOpenMenu=false;
this.scrollTo(mMenuWidth,0);
}
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
return false;
}
}