1. 程式人生 > >簡單實現側滑選單

簡單實現側滑選單

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();
            }
        });
    }
}