工具類之SnackbarUtils

Snackbar作為Android design support library下的一個控件,可以方便地在屏幕下方彈出消息,和Toast很相似,但更為靈活,這兩天也一直在為它的封裝搞事,因為一開始snackbar持有靜態變量導致內存泄漏,現在已將其改為弱引用,完美解決其自動回收,真是被自己雞汁到了;為了我的AndroidUtilCode庫中不compile其他庫,我不得不用provided com.android.support:design:xxx,但帶來的后果就是我需要把如下這三個東西抽出來,真是煞費苦心了我,但是如果只是用我這單個工具類的話那就直接Ctrl + C帶走即可,無需像我那般折騰,老司機們只需直接compile com.android.support:design:xxx即可。

SnackbarXml

可自定義背景色和文字顏色及action按鈕的背景色和文字顏色,并可新增自定義view,是不是很爽,下面來展示下效果:

SnackbarDemo

下方展示其工具類目錄及Demo地址和源碼:

showShortSnackbar      : 顯示短時snackbar
showLongSnackbar       : 顯示長時snackbar
showIndefiniteSnackbar : 顯示自定義時長snackbar
addView                : 為SnackBar添加布局
dismissSnackbar        : 取消snackbar顯示

import android.support.design.widget.Snackbar;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.R;

import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;

/**
 * <pre>
 *     author: Blankj
 *     blog  : http://blankj.com
 *     time  : 2016/10/16
 *     desc  : Snackbar相關工具類
 * </pre>
 */
public class SnackbarUtils {

    private SnackbarUtils() {
        throw new UnsupportedOperationException("u can't instantiate me...");
    }

    private static WeakReference<Snackbar> snackbarWeakReference;

    /**
     * 顯示短時snackbar
     *
     * @param parent    父視圖(CoordinatorLayout或者DecorView)
     * @param text      文本
     * @param textColor 文本顏色
     * @param bgColor   背景色
     */
    public static void showShortSnackbar(View parent, CharSequence text, int textColor, int bgColor) {
        showSnackbar(parent, text, Snackbar.LENGTH_SHORT, textColor, bgColor, null, -1, null);
    }

    /**
     * 顯示短時snackbar
     *
     * @param parent          父視圖(CoordinatorLayout或者DecorView)
     * @param text            文本
     * @param textColor       文本顏色
     * @param bgColor         背景色
     * @param actionText      事件文本
     * @param actionTextColor 事件文本顏色
     * @param listener        監聽器
     */
    public static void showShortSnackbar(View parent, CharSequence text, int textColor, int bgColor,
                                         CharSequence actionText, int actionTextColor, View.OnClickListener listener) {
        showSnackbar(parent, text, Snackbar.LENGTH_SHORT, textColor, bgColor,
                actionText, actionTextColor, listener);
    }

    /**
     * 顯示長時snackbar
     *
     * @param parent    視圖(CoordinatorLayout或者DecorView)
     * @param text      文本
     * @param textColor 文本顏色
     * @param bgColor   背景色
     */
    public static void showLongSnackbar(View parent, CharSequence text, int textColor, int bgColor) {
        showSnackbar(parent, text, Snackbar.LENGTH_LONG, textColor, bgColor, null, -1, null);
    }

    /**
     * 顯示長時snackbar
     *
     * @param parent          視圖(CoordinatorLayout或者DecorView)
     * @param text            文本
     * @param textColor       文本顏色
     * @param bgColor         背景色
     * @param actionText      事件文本
     * @param actionTextColor 事件文本顏色
     * @param listener        監聽器
     */
    public static void showLongSnackbar(View parent, CharSequence text, int textColor, int bgColor,
                                        CharSequence actionText, int actionTextColor, View.OnClickListener listener) {
        showSnackbar(parent, text, Snackbar.LENGTH_LONG, textColor, bgColor,
                actionText, actionTextColor, listener);
    }

    /**
     * 顯示自定義時長snackbar
     *
     * @param parent    父視圖(CoordinatorLayout或者DecorView)
     * @param text      文本
     * @param duration  自定義時長
     * @param textColor 文本顏色
     * @param bgColor   背景色
     */
    public static void showIndefiniteSnackbar(View parent, CharSequence text, int duration, int textColor, int bgColor) {
        showSnackbar(parent, text, duration, textColor, bgColor, null, -1, null);
    }

    /**
     * 顯示自定義時長snackbar
     *
     * @param parent          父視圖(CoordinatorLayout或者DecorView)
     * @param text            文本
     * @param duration        自定義時長
     * @param textColor       文本顏色
     * @param bgColor         背景色
     * @param actionText      事件文本
     * @param actionTextColor 事件文本顏色
     * @param listener        監聽器
     */
    public static void showIndefiniteSnackbar(View parent, CharSequence text, int duration, int textColor, int bgColor,
                                              CharSequence actionText, int actionTextColor, View.OnClickListener listener) {
        showSnackbar(parent, text, duration, textColor, bgColor,
                actionText, actionTextColor, listener);
    }

    /**
     * 設置snackbar文字和背景顏色
     *
     * @param parent          父視圖(CoordinatorLayout或者DecorView)
     * @param text            文本
     * @param duration        顯示時長
     * @param textColor       文本顏色
     * @param bgColor         背景色
     * @param actionText      事件文本
     * @param actionTextColor 事件文本顏色
     * @param listener        監聽器
     */
    private static void showSnackbar(View parent, CharSequence text, int duration, int textColor, int bgColor,
                                     CharSequence actionText, int actionTextColor, View.OnClickListener listener) {
        switch (duration) {
            default:
            case Snackbar.LENGTH_SHORT:
            case Snackbar.LENGTH_LONG:
                snackbarWeakReference = new WeakReference<>(Snackbar.make(parent, text, duration));
                break;
            case Snackbar.LENGTH_INDEFINITE:
                snackbarWeakReference = new WeakReference<>(Snackbar.make(parent, text, Snackbar.LENGTH_INDEFINITE).setDuration(duration));
        }
        View view = snackbarWeakReference.get().getView();
        ((TextView) view.findViewById(R.id.snackbar_text)).setTextColor(textColor);
        view.setBackgroundColor(bgColor);
        if (actionText != null && actionText.length() > 0 && listener != null) {
            snackbarWeakReference.get().setActionTextColor(actionTextColor);
            snackbarWeakReference.get().setAction(actionText, listener);
        }
        snackbarWeakReference.get().show();
    }

    /**
     * 為snackbar添加布局
     * <p>在show...Snackbar之后調用</p>
     *
     * @param layoutId 布局文件
     * @param index    位置(the position at which to add the child or -1 to add last)
     */
    public static void addView(int layoutId, int index) {
        Snackbar snackbar = snackbarWeakReference.get();
        if (snackbar != null) {
            View view = snackbar.getView();
            Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout) view;
            View child = LayoutInflater.from(view.getContext()).inflate(layoutId, null);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            params.gravity = Gravity.CENTER_VERTICAL;
            layout.addView(child, index, params);
        }
    }

    /**
     * 取消snackbar顯示
     */
    public static void dismissSnackbar() {
        if (snackbarWeakReference != null && snackbarWeakReference.get() != null) {
            snackbarWeakReference.get().dismiss();
            snackbarWeakReference = null;
        }
    }
}

如果該工具類依賴其他工具類,都可以在我的Android開發人員不得不收集的代碼(持續更新中)中找到。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容