1.自定义viewgroup继承horizonScrollview
public class MySliding extends HorizontalScrollView {
private int pwidth;
LinearLayout par;
boolean once=true;
ViewGroup chil1;//左边菜单
ViewGroup chil2;//右边内容
int chil1w;
int chil2w;
public MySliding(Context context, AttributeSet attrs) {
super(context, attrs);
DisplayMetrics out=new DisplayMetrics();
WindowManager wm= (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
wm.getDefaultDisplay().getMetrics(out);
pwidth=out.widthPixels;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (once){
par= (LinearLayout) getChildAt(0);
chil1= (ViewGroup) par.getChildAt(0);
chil2= (ViewGroup) par.getChildAt(1);
chil1w=chil1.getLayoutParams().width=pwidth-200;
chil2w=chil2.getLayoutParams().width=pwidth;
once=false;
}
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);
Log.d("changed",changed+"");
if(changed){
this.scrollTo(chil1w,0);//隐藏chil1
}
}
boolean first=true;
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
//l代表子视图的最左边距离屏幕左上角的距离
Log.d("x",l+"");
ViewHelper.setTranslationX(chil1,l);//加上此句可以实现抽屉布局,去掉此局普通滑动菜单
// ViewHelper.setPivotX(chil2,0);
//ViewHelper.setPivotY(chil2,chil2.getHeight()/2);
// ViewHelper.setScaleX(chil2,1-l/chil1w);
//ViewHelper.setScaleY(chil2,1.0f-l/600.0f);
// ViewHelper.setAlpha(chil2,l/600.0f);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
int action=ev.getAction();
switch (action){
case MotionEvent.ACTION_UP:
int scrollx=getScrollX();//子视图左上角距离屏幕左上角的距离
if (scrollx>chil1w/2){//滑动距离小于chil1宽度的一半
this.smoothScrollTo(chil1w,0);
isopen=false;
}else {
this.smoothScrollTo(0,0);
isopen=true;
}
return true;
}
return super.onTouchEvent(ev);
}
boolean isopen=false;//菜单是否打开
public void toogleMenu(){
if(isopen){
this.smoothScrollTo(chil1w,0);
isopen=false;
}else {
this.smoothScrollTo(0,0);
isopen=true;
}
}
}
2.布局文件
<?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">
<com.example.administrator.slidingmenu.MySliding
android:id="@+id/mySlide"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:background="@color/colorPrimary"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="sdasda"
android:textSize="50sp"/>
</LinearLayout>
<LinearLayout
android:background="@color/colorAccent"
android:src="@mipmap/ic_launcher"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="@+id/menuColl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="切换"/>
</LinearLayout>
</LinearLayout>
</com.example.administrator.slidingmenu.MySliding>
</RelativeLayout>
3测试类
public class MainActivity extends AppCompatActivity {
private Button menuColl;
private MySliding mySliding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mySliding= (MySliding) findViewById(R.id.mySlide);
menuColl= (Button) findViewById(R.id.menuColl);
menuColl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mySliding.toogleMenu();
}
});
}
}
其中使用了一个动画jar包nineoldandroids.jar