Snackbars 與 Toasts
Snackbar 是一種針對操作的輕量級反饋機制,常以一個小的彈出框的形式,出現(xiàn)在手機屏幕下方或者桌面左下方。它們出現(xiàn)在屏幕所有層的最上方,包括浮動操作按鈕党饮。
它們會在超時或者用戶在屏幕其他地方觸摸之后自動消失。Snackbar 可以在屏幕上滑動關(guān)閉笆焰。當它們出現(xiàn)時劫谅,不會阻礙用戶在屏幕上的輸入见坑,并且也不支持輸入嚷掠。屏幕上同時最多只能現(xiàn)實一個 Snackbar捏检。
Android 也提供了一種主要用于提示系統(tǒng)消息的膠囊狀的提示框 Toast。Toast 同 Snackbar 非常相似不皆,但是 Toast 并不包含操作也不能從屏幕上滑動關(guān)閉。
SnackBar 的使用及玩轉(zhuǎn)
使用
SnackBar 的基本使用很簡單霹娄,和 Toast 差不多能犯。
Snackbar.make(view, message_text, duration)
.setAction(action_text, click_listener)
.show();
玩轉(zhuǎn) SnackBar
要玩轉(zhuǎn) SnackBar,我們得先知道 SnackBar 提供了哪些可定制的方法犬耻。一張圖看完 SnackBar結(jié)構(gòu)~
那就根據(jù)上圖類結(jié)構(gòu)踩晶,一個一個分析吧
SnackBar.Callback、setCallback()枕磁、removeCallback()以及BaseTransientBottomBar.BaseCallback
/**
* Callback class for {@link Snackbar} instances.
*
* Note: this class is here to provide backwards-compatible way for apps written before
* the existence of the base {@link BaseTransientBottomBar} class.
*
* @see BaseTransientBottomBar#addCallback(BaseCallback)
*/
public static class Callback extends BaseCallback<Snackbar> {
/** Indicates that the Snackbar was dismissed via a swipe.*/
public static final int DISMISS_EVENT_SWIPE = BaseCallback.DISMISS_EVENT_SWIPE;
/** Indicates that the Snackbar was dismissed via an action click.*/
public static final int DISMISS_EVENT_ACTION = BaseCallback.DISMISS_EVENT_ACTION;
/** Indicates that the Snackbar was dismissed via a timeout.*/
public static final int DISMISS_EVENT_TIMEOUT = BaseCallback.DISMISS_EVENT_TIMEOUT;
/** Indicates that the Snackbar was dismissed via a call to {@link #dismiss()}.*/
public static final int DISMISS_EVENT_MANUAL = BaseCallback.DISMISS_EVENT_MANUAL;
/** Indicates that the Snackbar was dismissed from a new Snackbar being shown.*/
public static final int DISMISS_EVENT_CONSECUTIVE = BaseCallback.DISMISS_EVENT_CONSECUTIVE;
@Override
public void onShown(Snackbar sb) {
// Stub implementation to make API check happy.
}
@Override
public void onDismissed(Snackbar transientBottomBar, @DismissEvent int event) {
// Stub implementation to make API check happy.
}
}
SnackBar 繼承自BaseTransientBottomBar渡蜻;
SnackBar.Callback繼承自BaseCallback;
就是一個 SnackBar 的 show()方法和 onDisMissed()回調(diào)计济,不多解釋了茸苇。
Duration枚舉和 setDuration()、getDuration()方法
設(shè)置 SnackBar 顯示時長沦寂,有如下幾種狀態(tài)
LENGTH_INDEFINITE 一直顯示学密,直到手動 dismissed 或者另一個 SnackBar show
LENGTH_SHORT 顯示一段時間
-
LENGTH_LONG 顯示一段長時間
具體顯示多長時間,我們在SnackBarManger 類里面可以找到一下兩個常量private static final int SHORT_DURATION_MS = 1500; private static final int LONG_DURATION_MS = 2750;
即short 顯示1.5秒传藏,long 顯示2.75秒
ContentViewCallback
SnackBar 在顯示和隱藏時給執(zhí)行相應的動畫腻暮,make方法里面還會講
/**
* Interface that defines the behavior of the main content of a transient bottom bar.
*/
public interface ContentViewCallback {
/**
* Animates the content of the transient bottom bar in.
*
* @param delay Animation delay.
* @param duration Animation duration.
*/
void animateContentIn(int delay, int duration);
/**
* Animates the content of the transient bottom bar out.
*
* @param delay Animation delay.
* @param duration Animation duration.
*/
void animateContentOut(int delay, int duration);
}
make(View,CharSequence,int)方法
這是 SnackBar 的靜態(tài)方法,用于創(chuàng)建一個 SnackBar 并且做一些默認操作
public static Snackbar make(@NonNull View view, @NonNull CharSequence text,
@Duration int duration) {
final ViewGroup parent = findSuitableParent(view);
if (parent == null) {
throw new IllegalArgumentException("No suitable parent found from the given view. "
+ "Please provide a valid view.");
}
final LayoutInflater inflater = LayoutInflater.from(parent.getContext());
final SnackbarContentLayout content =
(SnackbarContentLayout) inflater.inflate(
R.layout.design_layout_snackbar_include, parent, false);
final Snackbar snackbar = new Snackbar(parent, content, content);
snackbar.setText(text);
snackbar.setDuration(duration);
return snackbar;
}
private static ViewGroup findSuitableParent(View view) {
ViewGroup fallback = null;
do {
if (view instanceof CoordinatorLayout) {
// We've found a CoordinatorLayout, use it
return (ViewGroup) view;
} else if (view instanceof FrameLayout) {
if (view.getId() == android.R.id.content) {
// If we've hit the decor content view, then we didn't find a CoL in the
// hierarchy, so use it.
return (ViewGroup) view;
} else {
// It's not the content view but we'll use it as our fallback
fallback = (ViewGroup) view;
}
}
if (view != null) {
// Else, we will loop and crawl up the view hierarchy and try to find a parent
final ViewParent parent = view.getParent();
view = parent instanceof View ? (View) parent : null;
}
} while (view != null);
// If we reach here then we didn't find a CoL or a suitable content view so we'll fallback
return fallback;
}
這個方法是SnackBar 的重點毯侦,需要重點掌握
首先我來看前幾第一行代碼:用方法參數(shù)里面?zhèn)鬟M來的 view 做為參數(shù)西壮,去調(diào)用了findSuitableParent()方法。這個方法很簡單叫惊,根據(jù)所給的 view 不斷去尋找 parent款青,直到找到DecorView里面的 contentView 即Activity 里面 setContentView 的父節(jié)點 FrameLayout 或者找到CoordinatorLayoutView,如果沒找到則返回 null霍狰,在 make()方法里面拋出異常抡草。
1.為什么要尋找這個 parent?因為這個 parent 是 SnackBar 構(gòu)造方法的必要參數(shù)蔗坯,并且SnackBar 在 show 的時候需要依附在一個 view 上并且顯示在屏幕底部康震。
2.為什么CoordinatorLayoutView也可以并且優(yōu)先使用。CoordinatorLayoutView是一個協(xié)調(diào) ViewGroup宾濒,配合 Behavior 可以顯示很多動畫腿短。這里的父節(jié)點如果是CoordinatorLayoutView可以讓 SnackBar 在彈出的時候不會遮住FloatActionBar。不要問我為什么知道的,SnackBar的構(gòu)造方法上已經(jīng)告訴我們了橘忱。
剛剛我們拿到了用于SnackBar 顯示在屏幕底部的 parentView赴魁,繼續(xù)往下走
final SnackbarContentLayout content =
(SnackbarContentLayout) inflater.inflate(
R.layout.design_layout_snackbar_include, parent, false);
這里我們從 xml 里面 inflate 了一個SnackbarContentLayout,它集成自 LinearLayout 并且實現(xiàn)了BaseTransientBottomBar.ContentViewCallback接口,并實現(xiàn)了 SnackBar 在顯示和隱藏的回調(diào)動畫钝诚。
然后通過 private 的構(gòu)造方法Snackbar(ViewGroup parent, View content, ContentViewCallback contentViewCallback)創(chuàng)建了一個 SnackBar 實例颖御。
setText(CharSequence)方法
public Snackbar setText(@NonNull CharSequence message) {
final SnackbarContentLayout contentLayout = (SnackbarContentLayout) mView.getChildAt(0);
final TextView tv = contentLayout.getMessageView();
tv.setText(message);
return this;
}
這里取到了一個SnackbarContentLayout,不用想凝颇,肯定就是我們剛剛在 make 方法里面創(chuàng)建的那個SnackbarContentLayout潘拱。于是可以得出結(jié)論,SnackBar 里面的布局就是SnackbarContentLayout拧略。
setAction(CharSequence,OnclickListener)方法
public Snackbar setAction(CharSequence text, final View.OnClickListener listener) {
final SnackbarContentLayout contentLayout = (SnackbarContentLayout) mView.getChildAt(0);
final TextView tv = contentLayout.getActionView();
if (TextUtils.isEmpty(text) || listener == null) {
tv.setVisibility(View.GONE);
tv.setOnClickListener(null);
} else {
tv.setVisibility(View.VISIBLE);
tv.setText(text);
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
listener.onClick(view);
// Now dismiss the Snackbar
dispatchDismiss(BaseCallback.DISMISS_EVENT_ACTION);
}
});
}
return this;
}
方法很簡單芦岂,取到SnackbarContentLayout里面的 ActionView,設(shè)置顯示文本和點擊事件垫蛆,并且這里再次證實了上面的猜想盔腔。
SetActionTextColor(int)
設(shè)置SnackbarContentLayout里面 ActionView 的字體顏色
getContext()
。月褥。弛随。跳過
getView()方法
public View getView() {
return mView;
}
返回 mView,好像沒什么卵用宁赤,仔細想想~~
還記得 setAction舀透、setText等方法么,里面的SnackbarContentLayout是通過mView.getChildAt(0)獲取到的决左,那么我們拿到了這個 View 的引用愕够,SnackBar 的樣式還不隨我們自由修改?
甚至可以SnackbarContentLayout.removeAllViews();然后再SnackbarContentLayout.addView(任意 view)佛猛。
show()方法
這就是 SnackBar 顯示到屏幕上的方法惑芭,里面調(diào)用了SnackBarManger。
public void show(int duration, Callback callback) {
synchronized (mLock) {
if (isCurrentSnackbarLocked(callback)) {
// Means that the callback is already in the queue. We'll just update the duration
mCurrentSnackbar.duration = duration;
// If this is the Snackbar currently being shown, call re-schedule it's
// timeout
mHandler.removeCallbacksAndMessages(mCurrentSnackbar);
scheduleTimeoutLocked(mCurrentSnackbar);
return;
} else if (isNextSnackbarLocked(callback)) {
// We'll just update the duration
mNextSnackbar.duration = duration;
} else {
// Else, we need to create a new record and queue it
mNextSnackbar = new SnackbarRecord(duration, callback);
}
if (mCurrentSnackbar != null && cancelSnackbarLocked(mCurrentSnackbar,
Snackbar.Callback.DISMISS_EVENT_CONSECUTIVE)) {
// If we currently have a Snackbar, try and cancel it and wait in line
return;
} else {
// Clear out the current snackbar
mCurrentSnackbar = null;
// Otherwise, just show it now
showNextSnackbarLocked();
}
}
}
SnackBarManger是一個單例继找,并且使用了同步鎖遂跟,因此保證了 SnackBar 在屏幕上不會同時顯示連個.
Over~~~