AndroidSlidingUpPanel控件的用法以及簡單解析
一個能夠向上滑動的時候往上飛出一個顯示區域的控件,類似于play music中的效果。該控件在主界面中有一個如下圖紅色箭頭所指的底部觸發區域:

該區域點擊的時候被隱藏在下方的內容將網上漂移到頂部,直到被隱藏的內容完全擋住原來的布局,但是這個觸發區域仍然存在,如圖。

當被隱藏區域完全顯示,這時再次點擊觸發區域(或者是通過下滑的手勢)將恢復到最初的狀態。
一般再未點擊的時候,這個觸發區域顯示一些被隱藏內容的簡要信息。
這就是AndroidSlidingUpPanel的效果了。
AndroidSlidingUpPanel的實現是使用ViewdragHelper實現的,其實ViewdragHelper在surport v4中已經可以直接使用了,但是作者直接將ViewdragHelper的所有源碼放到了自己的項目中。下面是AndroidSlidingUpPanel庫的代碼結構:

其中SlidingUpPanelLayout是一個繼承自ViewGroup的類。
使用方法:
.將com.sothree.slidinguppanel.SlidingUpPanelLayout作為根節點放到你activity的layout文件中。
.layout必須設置gravity屬性為top 或者bottom
.確保SlidingUpPanelLayout有兩個子view,一個是主界面,另外一個是向上滑動的界面。
.SlidingUpPanelLayout的width需要設置成match_parent,height可以是match_parent或者是固定值。
.默認情況下,整個界面都可以相應滑動和點擊事件,你可以通過調用setDragView來約束可滑動的View范圍。
更多的使用請參考demo。
<com.sothree.slidinguppanel.SlidingUpPanelLayout
xmlns:sothree="http://schemas.android.com/apk/res-auto"
android:id="@+id/sliding_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
sothree:panelHeight="68dp"
sothree:shadowHeight="4dp">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Main Content"
android:textSize="16sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center|top"
android:text="The Awesome Sliding Up Panel"
android:textSize="16sp" />
</com.sothree.slidinguppanel.SlidingUpPanelLayout>
項目給出的demo中當向上滑動的時候actionbar也是跟著慢慢隱藏的,這種效果必須使用ActionBarOverlay模式:
<style name="AppTheme">
<item name="android:windowActionBarOverlay">true</item>
</style> 同時這種情況你需要為主區域的布局設置margintop為actionbar的高度:
?android:attr/actionBarSize
還需要在代碼中動態的改變actionbar:
public void setActionBarTranslation(float y) {
// Figure out the actionbar height
int actionBarHeight = getActionBarHeight();
// A hack to add the translation to the action bar
ViewGroup content = ((ViewGroup) findViewById(android.R.id.content).getParent());
int children = content.getChildCount();
for (int i = 0; i < children; i++) {
View child = content.getChildAt(i);
if (child.getId() != android.R.id.content) {
if (y <= -actionBarHeight) {
child.setVisibility(View.GONE);
} else {
child.setVisibility(View.VISIBLE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
child.setTranslationY(y);
} else {
AnimatorProxy.wrap(child).setTranslationY(y);
}
}
}
}
} 最后要說的是,AndroidSlidingUpPanel在某些方面有點類似與垂直的ViewPager,但是不同點也很多,如果你想用ViewPager來實現AndroidSlidingUpPanel的效果是非常不明智的。
項目地址:http://jcodecraeer.com/a/opensource/2014/1016/1782.html