Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

開源中國源碼研讀(二):DrawerLayout與可拖拽ScrollView

編輯:關於 Android 開發

開源中國源碼研讀(二):DrawerLayout與可拖拽ScrollView

從啟動界面進入主界面後,采用的是v4包下的DrawerLayout,activity_main.xml如下:

<!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. --> 
<android.support.v4.widget.DrawerLayout 
 android:id="@+id/drawer_layout" 
 xmlns:android="https://schemas.android.com/apk/res/android" 
 xmlns:tools="https://schemas.android.com/tools" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 tools:context="net.oschina.app.ui.MainActivity"> 
 <LinearLayout 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:orientation="vertical"> 
 <FrameLayout 
 android:id="@+id/realtabcontent" 
 android:layout_width="match_parent" 
 android:layout_height="0dip" 
 android:layout_weight="1"/> 
 <FrameLayout 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:background="?attr/windows_bg"> 
 <RelativeLayout 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:layout_marginBottom="4dip"> 
 <net.oschina.app.widget.MyFragmentTabHost 
 android:id="@android:id/tabhost" 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:layout_marginTop="4dip"/> 
 <View 
 android:layout_width="match_parent" 
 android:layout_height="1px" 
 android:background="?attr/lineColor"/> 
 </RelativeLayout> 
 <!-- 快速操作按鈕 --> 
 <ImageView 
 android:id="@+id/quick_option_iv" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:layout_gravity="center" 
 android:contentDescription="@null" 
 android:src="@drawable/btn_quickoption_selector"/> 
 </FrameLayout> 
 </LinearLayout> 
 <!-- 左側側滑菜單 --> 
 <fragment 
 android:id="@+id/navigation_drawer" 
 android:name="net.oschina.app.ui.NavigationDrawerFragment" 
 android:layout_width="@dimen/navigation_drawer_width" 
 android:layout_height="match_parent" 
 android:layout_gravity="start" 
 tools:layout="@layout/fragment_navigation_drawer"/> 
</android.support.v4.widget.DrawerLayout>

側邊欄佈局fragment_navigation_drawer.xml的定義:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:orientation="vertical" 
 android:background="?attr/layout_bg_normal" > 
 <net.oschina.app.widget.CustomerScrollView 
 android:layout_width="match_parent" 
 android:layout_height="0dip" 
 android:layout_weight="1"> 
 <include layout="@layout/fragment_navigation_drawer_items"/> 
 </net.oschina.app.widget.CustomerScrollView > 
 <include layout="@layout/fragment_navigation_drawer_foot"/> 
</LinearLayout>

fragment_navigation_drawer_items.xml與fragment_navigation_drawer_foot.xml佈局較簡單,不再贅述。本文重點介紹自定義控件CustomerScrollView,它繼承自ScrollView:

package net.oschina.app.widget; 
import android.annotation.SuppressLint; 
import android.content.Context; 
import android.graphics.Rect; 
import android.util.AttributeSet; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.animation.TranslateAnimation; 
import android.widget.ScrollView; 
/** 
 * 可以拖動的ScrollView 
 * 
 */ 
public class CustomerScrollView extends ScrollView { 
 private static final int size = 4; 
 private View inner; 
 private float y; 
 private Rect normal = new Rect(); 
 public CustomerScrollView(Context context) { 
 super(context); 
 } 
 public CustomerScrollView(Context context, AttributeSet attrs) { 
 super(context, attrs); 
 } 
 @Override 
 protected void onFinishInflate() { 
 if (getChildCount() > 0) { 
 inner = getChildAt(0); 
 } 
 } 
 @SuppressLint("ClickableViewAccessibility") 
 @Override 
 public boolean onTouchEvent(MotionEvent ev) { 
 if (inner == null) { 
 return super.onTouchEvent(ev); 
 } else { 
 commOnTouchEvent(ev); 
 } 
 return super.onTouchEvent(ev); 
 } 
 public void commOnTouchEvent(MotionEvent ev) { 
 int action = ev.getAction(); 
 switch (action) { 
 case MotionEvent.ACTION_DOWN: 
 y = ev.getY(); 
 break; 
 case MotionEvent.ACTION_UP: 
 if (isNeedAnimation()) { 
 // Log.v("mlguitar", "will up and animation"); 
 animation(); 
 } 
 break; 
 case MotionEvent.ACTION_MOVE: 
 final float preY = y; 
 float nowY = ev.getY(); 
 /** 
 * size=4 表示 拖動的距離為屏幕的高度的1/4 
 */ 
 int deltaY = (int) (preY - nowY) / size; 
 // 滾動 
 // scrollBy(0, deltaY); 
 y = nowY; 
 if (isNeedMove()) { 
 if (normal.isEmpty()) { 
 normal.set(inner.getLeft(), inner.getTop(), 
 inner.getRight(), inner.getBottom()); 
 return; 
 } 
 int yy = inner.getTop() - deltaY; 
 // 移動布局 
 inner.layout(inner.getLeft(), yy, inner.getRight(), 
 inner.getBottom() - deltaY); 
 } 
 break; 
 default: 
 break; 
 } 
 } 
 public void animation() { 
 TranslateAnimation ta = new TranslateAnimation(0, 0, inner.getTop(), 
 normal.top); 
 ta.setDuration(200); 
 inner.startAnimation(ta); 
 inner.layout(normal.left, normal.top, normal.right, normal.bottom); 
 normal.setEmpty(); 
 } 
 public boolean isNeedAnimation() { 
 return !normal.isEmpty(); 
 } 
 public boolean isNeedMove() { 
 int offset = inner.getMeasuredHeight() - getHeight(); 
 int scrollY = getScrollY(); 
 if (scrollY == 0 || scrollY == offset) { 
 return true; 
 } 
 return false; 
 } 
} 

再來看效果:

可以看到,向上滑動時會有明顯的回彈效果。oschina客戶端側滑菜單的View採用的正是這個可拖拽的ScrollView,類文件為CustomerScrollView。

  • 拖拽的目標是ScrollView內的菜單佈局View,所以在onFinishInflate()中先通過getChildAt(0)取出菜單佈局的View,這就是第一步——獲取要拖拽的對象。onFinishInflate在加載完View後回調,這裡的View指的是源碼中<include layout="@layout/fragment_navigation_drawer_items"/>所加載的佈局。
  • 拖拽本質上是「按下—移動—抬起」的過程,因此要重寫onTouchEvent(MotionEvent ev)。移動過程就是把菜單View按移動方向與距離平移,源碼中最關鍵的一行是 inner.layout(inner.getLeft(), yy, inner.getRight(), inner.getBottom() - deltaY);,這四個參數都是相對於其父容器ScrollView的坐標原點而言的,不熟悉的同學可補一補坐標知識。
  • 當手指抬起(MotionEvent.ACTION_UP)時,需要把菜單View動畫式地恢復原位。由於移動本質是位置變化,這裡用到了TranslateAnimation:上下拖拽,所以X的起止坐標都是0,Y的起止坐標如上。觸發時機在ACTION_UP中,而原來的位置需要保存——移動要左上右下四個參數,因此有了 private Rect normal = new Rect();,通過 normal.set(...) 記錄菜單View的初始位置。
  • 仔細揣摩會發現,scrollY == 0 代表滾到了最頂部,scrollY == offset 代表滾到了最底部,兩者滿足其一即可實現拖拽。int offset = inner.getMeasuredHeight() - getHeight(); 即「自身總高減去可見高度」,等於沒被看到的那部分高度。
//是否需要移動 
public boolean isNeedMove() { 
 int offset = inner.getMeasuredHeight() - getHeight(); 
 int scrollY = getScrollY(); 
 if (scrollY == 0 || scrollY == offset) { 
 return true; 
 } 
 return false; 
}
  • 為什麼需要isNeedAnimation()?因為恢復原位也用到 inner.layout(normal.left, normal.top, normal.right, normal.bottom);,前提是normal必須已有四個值,而normal只有在滿足上述條件之一後才有值。
  • 為什麼拖拽又恢復後要把 normal.setEmpty(); 置空?因為normal的左上右下會隨位置不同而變化:在頂部時是(0,0,菜單寬度240dp,菜單實際測量高度),滾到最底部時則是(0,負的【菜單實際高度減去屏幕高度】,菜單寬度240dp,屏幕高度)。兩種Rect不同,故需清空以備下次重新記錄。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved