Android 開發實踐 ViewGroup 實現左右滑出視窗(一)

來源:互聯網
上載者:User

利用假期把以前做的東西總結整理一下,先從簡單的開始吧。
實現的效果是這樣的:

  做了個截屏動畫,比例有點不對了,湊合著看吧。

整個視窗有3部分組成,中間的主介面是個列表,左邊的滑出介面是個菜單,右邊的滑出介面是編輯框等。左邊菜單是半屏的,右邊的是全屏的。
最後其實我只用到了左邊的滑出視窗,但還是把左右的演算法都做全了。
並且設計了一個方法,讓activity可以指定兩個滑出視窗的寬度。

 源碼: http://files.cnblogs.com/inkheart0124/PushSlider.zip

 

一、自訂PushSlider類 繼承ViewGroup 並實現手勢listener

PushSlider.java

  PushSlider  ViewGroup  OnGestureListener
    SLIDER_PAGE_LEFT = 0    SLIDER_PAGE_MIDDLE = 1    SLIDER_PAGE_RIGHT = 2;

三個Child View的index,其中SLIDER_PAGE_MIDDLE是定寬的(全屏寬度),SLIDER_PAGE_LEFT和SLIDER_PAGE_RIGHT可以設定View的寬度。

 

           setHapticFeedbackEnabled(     mContext =     mForbidden =      mGDetector =  GestureDetector(mContext,     focusedIndex =     mMoveFlag = MOVE_FLAG_ALLOW_LEFT|     mDensity =     mScroller =  Scroller(mContext,      mChildWidth =  [3              }

初始化資料:

5行,mForbidden = false; 初始狀態預設允許滑動,activity可以通過mPushView.pushForbid(true)方法禁止滑動。

6行,mGDetector = GestureDetector(mContext, 建立手勢監聽對象。

7行,初始focus的view。

9行,擷取像素密度,後面要用它來計算位置。

10行,Scroller是一個實現View group平滑滾動的一個helper類。mScroller和mGDetector就是後面手勢滑動的主角。

11行,數組mChildWidth[] 記錄每個子view的寬度

 

重寫onMeasure和onLayout方法:

    onMeasure( widthMeasureSpec,            widthSize =      heightSize =     (mChild ==                  newSpec =   }      

入參是父view的尺寸,第7行第一次執行onMeasure時會根據父view的寬和高 (這裡等於屏的寬和高) 來初始化子view。

   initChildren( width,      (mChild ==          mChild =  View[3         mChildWidth[SLIDER_PAGE_FIX]=               ( i=SLIDER_PAGE_LEFT; i<=SLIDER_PAGE_RIGHT; i++         mChild[i] =         (mChild[i] !=                childWidth =             (childWidth >                 childWidth =                 mChildWidth[i] =               widthMeasureSpec =              heightMeasureSpec =     }            

initChildren方法 初始化數組mChild,記錄每個子view的對象。

4行,定寬的那個子view直接設定成全屏寬度。定寬的是索引為SLIDER_PAGE_MIDDLE的子view。

7~19行,獲得ViewGroup中的每個子View,根據每個子view的寬度設定調用它們的measure方法。

因此,主activity必須在onCreate方法中執行pushSlider的setPageWidth方法,設定左右兩個子view的寬度。

           mPushView.setPageWidth(PushSlider.SLIDER_PAGE_LEFT, screenWidth/2);
           mPushView.setPageWidth(PushSlider.SLIDER_PAGE_RIGHT, screenWidth);

  setPageWidth( index, (index ===

 

onLayout

    onLayout( changed,  l,  t,  r,                 (mChild[focusedIndex] !=               w =             (w == 0                 w = r-              mChild[focusedIndex].layout(l,t,l+                       ( next=SLIDER_PAGE_LEFT; next<=SLIDER_PAGE_RIGHT; next++             (next ==                               (mChild[next] !=                   w1 =                  x = 0                 ((next== SLIDER_PAGE_LEFT)&&(focusedIndex ==                     x = l-                 } ((next== SLIDER_PAGE_LEFT)&&(focusedIndex ==                     x = l- mChildWidth[SLIDER_PAGE_MIDDLE] -                 } ((next== SLIDER_PAGE_MIDDLE)&&(focusedIndex ==                     x = l+                 } ((next== SLIDER_PAGE_MIDDLE)&&(focusedIndex ==                     x = l-                 } ((next== SLIDER_PAGE_RIGHT)&&(focusedIndex ==                     x = l+                  ((next== SLIDER_PAGE_RIGHT)&&(focusedIndex ==                     x = l+mChildWidth[SLIDER_PAGE_LEFT]+                  mChild[next].layout(x,t,x+                        mLeftPosX = -         (mChild[SLIDER_PAGE_RIGHT] !=              mRightPosX =                      mRightPosX = 0     }          (mChild[focusedIndex] !=               w =             mChild[focusedIndex].layout(l,t,l+   }    

onLayout 沒什麼特別的,就是把每個子view布局一下,調用子view的layout(l,t,r,b)方法。

以My Phone屏寬720pixels,中間的view從l=0到r=719,左邊的就是-359到0,右邊的720到1439。






 OnPageChangedListener mPageChangedListener =   onPageChanged(View v,  =

activity需要調用mPushView.setOnPageChangedListener(this);註冊監聽,並實現 onPageChanged(View v,

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.