主目錄見:Android高級進階知識(這是總目錄索引)
分析那么多的源碼,我只想說一句:Talk is cheap!!! Show me the code!!!!
已經按捺不住心中寫代碼的熱情,那我們就開始我們的代碼之旅。。。
一.目標
1.利用《SnackBar的源碼分析》的知識
好吧!這是今天的唯一目標,效果如下,當然你可以用這個機制做出更酷炫吊炸天的效果,但是不要在意:
二.基礎使用
基礎使用跟Toast和SnackBar有點類似:
ActionSheet.makeActionSheet(btnPopupActionSheet,R.layout.custom_action_sheet,"添加圖片").show()
.registerItemClick(new ActionSheet.IItemClick() {
@Override
public void onItemClick(int index) {
Toast.makeText(MainActivity.this,"第"+index+"個按鈕",Toast.LENGTH_SHORT).show();
}
});
三.實現分析
1.findSuitableParent
依照看代碼的套路,我們首先進入makeActionSheet來看到底做了些什么?
public static ActionSheet makeActionSheet(View view,int resId,String title){
return new ActionSheet(view,resId,title);
}
很簡單,就是套路,啥也沒有。。。。這邊new出一個ActionSheet對象,所以我們看構造函數:
public ActionSheet(View view,int resId,String title) {
parentView = findSuitableParent(view);
actionSheet = LayoutInflater.from(parentView.getContext()).inflate(R.layout.action_sheet,parentView,false);
lLayout_content = (LinearLayout) actionSheet.findViewById(R.id.lLayout_content);
tvTitle = (TextView)actionSheet.findViewById(R.id.txt_title);
tvCancel = (TextView) actionSheet.findViewById(R.id.txt_cancel);
rootView = LayoutInflater.from(view.getContext()).inflate(resId,lLayout_content,true);
tvTitle.setText(title == null || "".equalsIgnoreCase(title) ? "" : title);
initEvent();
}
果然一坨代碼,我們第一眼就看到第一句的findSuitableParent,這個是不是無比熟悉,對的這個就是snackBar源碼里面摳出來的!!!
/**
* 向上追溯找到id為content的FrameLayout
* */
private static ViewGroup findSuitableParent(View view) {
ViewGroup fallback = null;
do {
if (view instanceof FrameLayout) {
if (view.getId() == android.R.id.content) {
return (ViewGroup) view;
} else {
fallback = (ViewGroup) view;
}
}
if (view != null) {
final ViewParent parent = view.getParent();
view = parent instanceof View ? (View) parent : null;
}
} while (view != null);
return fallback;
}
這個地方又要啰嗦一下.放出那張鎮貼的圖片,邪惡地笑了一下(很有湊字數嫌疑):
接下來的一句代碼大家應該都知道,就是inflate出來action_sheet布局,為把湊字數發揚光大,決定也貼出來:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="bottom"
android:background="@color/actionsheet_bg"
android:padding="8dp" >
<TextView
android:id="@+id/txt_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/actionsheet_top_normal"
android:gravity="center"
android:minHeight="45dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:textColor="@color/actionsheet_gray"
android:textSize="13sp"
/>
<ScrollView
android:id="@+id/sLayout_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fadingEdge="none"
>
<LinearLayout
android:id="@+id/lLayout_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
</LinearLayout>
</ScrollView>
<TextView
android:id="@+id/txt_cancel"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_marginTop="8dp"
android:background="@drawable/actionsheet_single_selector"
android:gravity="center"
android:clickable="true"
android:text="取消"
android:textColor="@color/actionsheet_blue"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
大家都是明眼人,就表示敬意小瞄一眼即可。好了,懂了。
2. LayoutInflater.from(view.getContext()).inflate(resId,lLayout_content,true);
接著我們看rootView = LayoutInflater.from(view.getContext()).inflate(resId,lLayout_content,true);這句,這句主要就是將我們傳進來的布局放進上面布局里面id為lLayout_content的LinearLayout里面。雖然只是寫個demo,但是也要有點節操,稍微可以定制點。所以用戶可以自己傳進一個自己的布局
3.show
好的,前面都輕松地完成了,但是我們的視圖還是沒有顯示出來,那顯示出來是很簡單的,因為我們的父視圖已經找到了,我們只要把我們的布局添加進去即可。
public ActionSheet show(){
if (actionSheet.getParent() != null){
parentView.removeView(actionSheet);
}
//將布局添加進FrameLayout
parentView.addView(actionSheet);
return this;
}
出奇地簡單,沒錯。。。就是這么簡單,當然想要復雜可以自己作起來!!!
4.dismiss
最后當然是取消時候彈掉這個彈出框:
public void dismiss(){
ObjectAnimator ani = ObjectAnimator.ofFloat(actionSheet,"translationY",0,actionSheet.getHeight());
ani.setDuration(400);
ani.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
parentView.removeView(actionSheet);
actionSheet = null;
}
});
ani.start();
}
這個地方略微用了下屬性動畫,到時動畫地方會講一些高級知識,敬請期待。在這個天氣晴朗,萬里有云的節日里,我們的代碼也就結束了。。。
最后附上Demo地址,要看的請[重擊]謝謝.