MaterialDesign--(4)SnackBar的使用及其源码分析

Snackbars 与 Toasts

Snackbar 是一种针对操作的轻量级反馈机制,常以一个小的弹出框的形式,出现在手机屏幕下方或者桌面左下方。它们出现在屏幕所有层的最上方,包括浮动操作按钮。

它们会在超时或者用户在屏幕其他地方触摸之后自动消失。Snackbar 可以在屏幕上滑动关闭。当它们出现时,不会阻碍用户在屏幕上的输入,并且也不支持输入。屏幕上同时最多只能现实一个 Snackbar。

Android 也提供了一种主要用于提示系统消息的胶囊状的提示框 Toast。Toast 同 Snackbar 非常相似,但是 Toast 并不包含操作也不能从屏幕上滑动关闭。

SnackBar 的使用及玩转

使用

SnackBar 的基本使用很简单,和 Toast 差不多。

Snackbar.make(view, message_text, duration)
    .setAction(action_text, click_listener)
    .show();

玩转 SnackBar

要玩转 SnackBar,我们得先知道 SnackBar 提供了哪些可定制的方法。一张图看完 SnackBar结构~

MaterialDesign--(4)SnackBar的使用及其源码分析_第1张图片
SnackBar structure.png

那就根据上图类结构,一个一个分析吧

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 {
    /** 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()回调,不多解释了。

Duration枚举和 setDuration()、getDuration()方法

设置 SnackBar 显示时长,有如下几种状态

  • 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 在显示和隐藏时给执行相应的动画,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 的静态方法,用于创建一个 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 的重点,需要重点掌握
首先我来看前几第一行代码:用方法参数里面传进来的 view 做为参数,去调用了findSuitableParent()方法。这个方法很简单,根据所给的 view 不断去寻找 parent,直到找到DecorView里面的 contentView 即Activity 里面 setContentView 的父节点 FrameLayout 或者找到CoordinatorLayoutView,如果没找到则返回 null,在 make()方法里面抛出异常。
1.为什么要寻找这个 parent?因为这个 parent 是 SnackBar 构造方法的必要参数,并且SnackBar 在 show 的时候需要依附在一个 view 上并且显示在屏幕底部。
2.为什么CoordinatorLayoutView也可以并且优先使用。CoordinatorLayoutView是一个协调 ViewGroup,配合 Behavior 可以显示很多动画。这里的父节点如果是CoordinatorLayoutView可以让 SnackBar 在弹出的时候不会遮住FloatActionBar。不要问我为什么知道的,SnackBar的构造方法上已经告诉我们了。

刚刚我们拿到了用于SnackBar 显示在屏幕底部的 parentView,继续往下走

final SnackbarContentLayout content =
        (SnackbarContentLayout) inflater.inflate(
                R.layout.design_layout_snackbar_include, parent, false);

这里我们从 xml 里面 inflate 了一个SnackbarContentLayout,它集成自 LinearLayout 并且实现了BaseTransientBottomBar.ContentViewCallback接口,并实现了 SnackBar 在显示和隐藏的回调动画。

然后通过 private 的构造方法Snackbar(ViewGroup parent, View content, ContentViewCallback contentViewCallback)创建了一个 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 方法里面创建的那个SnackbarContentLayout。于是可以得出结论,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,设置显示文本和点击事件,并且这里再次证实了上面的猜想。

SetActionTextColor(int)

设置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 显示到屏幕上的方法,里面调用了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~~~

你可能感兴趣的:(MaterialDesign--(4)SnackBar的使用及其源码分析)