Android Snackbar类

Android Snackbar类

Snackbar类似于Toast,显示在屏幕的底部,包含文字信息与一个可选的操作按钮。

1、导入Snackbar

Snackbar类是5.0版本出现的控件,需要添加依赖库,并且使用Theme.AppCompat主题。
dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:support-v4:25.1.0'
    compile 'com.android.support:appcompat-v7:25.1.0'
    compile 'com.android.support:design:25.1.0'
}

2、Snackbar创建

Snackbar提供了静态的make方法来创建。
public static Snackbar make(View view, CharSequence text, int duration)
public static Snackbar make(View view, int resId, int duration)
在make方法中,加载View为父控件
(1) 如果view是CoordinatorLayout类,使用该控件作为父控件
(2) 找到id为android.R.id.content的那个FrameLayout作为父控件
(3) 返回根View作为父控件

public static Snackbar make(@NonNull View view, @NonNull CharSequence text,
		@Duration int duration) {
	final ViewGroup parent = findSuitableParent(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;
}

3、设置点击事件

Snackbar调用setAction设置点击事件
public Snackbar setAction(int resId, View.OnClickListener listener)
public Snackbar setAction(CharSequence text, final View.OnClickListener listener)
public Snackbar setActionTextColor(ColorStateList colors)
public Snackbar setActionTextColor(int color)

4、监听Snackbar

Snackbar调用addCallback添加监听器。
public B addCallback(@NonNull BaseCallback<B> callback)
BaseCallback类包含onShown和onDismissed两个方法
public abstract static class BaseCallback<B> {
	public void onDismissed(B transientBottomBar, @DismissEvent int event) {
		// empty
	}

	public void onShown(B transientBottomBar) {
		// empty
	}
}
Snackbar消失的几种方式
  • DISMISS_EVENT_SWIPE:向右滑动消失
  • DISMISS_EVENT_ACTION:点击右侧按钮消失
  • DISMISS_EVENT_TIMEOUT:设置的显示时间到了消失
  • DISMISS_EVENT_MANUAL:调用Snackbar的dismiss方法消失
  • DISMISS_EVENT_CONSECUTIVE:新的Snackbar出现导致旧的消失

5、示例

Snackbar.make(v, R.string.app_name, Snackbar.LENGTH_LONG)
	.setAction(R.string.btn_click, new View.OnClickListener(){
		@Override
		public void onClick(View v) {
		}
	}).setActionTextColor(Color.GREEN)
	.addCallback(new BaseTransientBottomBar.BaseCallback<Snackbar>() {
		@Override
		public void onDismissed(Snackbar transientBottomBar, int event) {
			super.onDismissed(transientBottomBar, event);
			switch (event) {
				case BaseTransientBottomBar.BaseCallback.DISMISS_EVENT_SWIPE:
					LogUtil.log("Snackbar", "SWIPE");
					break;
				case BaseTransientBottomBar.BaseCallback.DISMISS_EVENT_ACTION:
					LogUtil.log("Snackbar", "ACTION");
					break;
				case BaseTransientBottomBar.BaseCallback.DISMISS_EVENT_TIMEOUT:
					LogUtil.log("Snackbar", "TIMEOUT");
					break;
				case BaseTransientBottomBar.BaseCallback.DISMISS_EVENT_MANUAL:
					LogUtil.log("Snackbar", "MANUAL");
					break;
				case BaseTransientBottomBar.BaseCallback.DISMISS_EVENT_CONSECUTIVE:
					LogUtil.log("Snackbar", "CONSECUTIVE");
					break;
			}
		}
		@Override
		public void onShown(Snackbar transientBottomBar) {
			super.onShown(transientBottomBar);
			LogUtil.log("Snackbar", "onShown");
		}
	}).show();

参考资料:http://blog.csdn.net/qq_22706515/article/details/51151654
参考资料:http://blog.csdn.net/qq_19431333/article/details/52862348


猜你喜欢

转载自blog.csdn.net/chennai1101/article/details/79613065