slidingMenu

繼承HorizontalScrollView

自定義ViewGroup
1、onMeasure
決定內部view(子view)的寬和高,以及呢,自己的寬和高
2、onLayout
決定view的 放置的位置
3、onTouchEvent


先簡單的建立一個XML文件就是menu菜單的佈局
然後創建一個slidingmenu繼承自HorizontalScrollView
實現兩個參數的構造方法
重寫上面三個方法
public class SliddingMenu extends HorizontalScrollView {
 
 private static final String WindowManager = null;
 private LinearLayout mWapper;
 private ViewGroup mMenu;
 private ViewGroup mContent;
 
 private int mScreenWidth;
 
 private int mMenuWidth;
 //dp
 private int mMenuRightPadding = 50;
 
 private boolean once;
 /**
  * 未使用自定義屬性時,調用
  * @param context
  * @param attrs
  */
 public SliddingMenu(Context context, AttributeSet attrs) {
  super(context, attrs);
  // TODO Auto-generated constructor stub
  WindowManager wm = (android.view.WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
  DisplayMetrics outMetrics = new DisplayMetrics();
  wm.getDefaultDisplay().getMetrics(outMetrics);
  //獲得屏幕的寬度
  mScreenWidth = outMetrics.widthPixels;
 
  //把dp轉化爲px
  mMenuRightPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, context.getResources().getDisplayMetrics());
 }
 
 /**
  * 設置子view的寬和高,設置自己的寬和高
  */
 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  // TODO Auto-generated method stub
 
  if (!once) {
   mWapper = (LinearLayout) getChildAt(0);
   mMenu = (ViewGroup) mWapper.getChildAt(0);
   mContent = (ViewGroup) mWapper.getChildAt(1);
   
   mMenuWidth = mMenu.getLayoutParams().width = mScreenWidth - mMenuRightPadding;
   mContent.getLayoutParams().width = mScreenWidth;
   once = true;
  }
  super.onMeasure(widthMeasureSpec, heightMeasureSpec);
 
 
 
 }
 
 /**
  * 通過設置偏移量,講menu隱藏
  */
 @Override
 protected void onLayout(boolean changed, int l, int t, int r, int b) {
  // TODO Auto-generated method stub
  if (!changed) {
   this.smoothScrollTo(mMenuWidth, 0);
  }
  super.onLayout(changed, l, t, r, b);
 }
 
 @Override
 public boolean onTouchEvent(MotionEvent ev) {
  // TODO Auto-generated method stub
  int action = ev.getAction();
  switch (action) {
  case MotionEvent.ACTION_UP:
   int scrollX = getScrollX();
   if (scrollX>=mMenuWidth/2) {
    this.smoothScrollTo(mMenuWidth, 0);
   }else {
    this.smoothScrollTo(0, 0);
   }
   return true;
  }
  return super.onTouchEvent(ev);
 }
}


佈局文件
<com.example.sliddingmenudemo.view.SliddingMenu
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="horizontal" >
            <include layout="@layout/left_menu" >
            </include>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/qq" >
            </LinearLayout>
        </LinearLayout>
    </com.example.sliddingmenudemo.view.SliddingMenu>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章