Snackbar In Support Library Doesn't Include Ondismisslistener()?
Solution 1:
Now it does
Snackbar.make(getView(), "Hi there!", Snackbar.LENGTH_LONG).setCallback( newSnackbar.Callback() {
@OverridepublicvoidonDismissed(Snackbar snackbar, int event) {
switch(event) {
caseSnackbar.Callback.DISMISS_EVENT_ACTION:
Toast.makeText(getActivity(), "Clicked the action", Toast.LENGTH_LONG).show();
break;
caseSnackbar.Callback.DISMISS_EVENT_TIMEOUT:
Toast.makeText(getActivity(), "Time out", Toast.LENGTH_LONG).show();
break;
}
}
@OverridepublicvoidonShown(Snackbar snackbar) {
Toast.makeText(getActivity(), "This is my annoying step-brother", Toast.LENGTH_LONG).show();
}
}).setAction("Go away!", newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
}
}).show();
Solution 2:
Check out my helper class for snackbars.
publicclassSnackbarUtils {
privatestaticfinalStringLOG_TAG= SnackbarUtils.class.getSimpleName();
privateSnackbarUtils() {
}
publicinterfaceSnackbarDismissListener {
voidonSnackbarDismissed();
}
publicstaticvoidshowSnackbar(View rootView, String message, String actionMessage, View.OnClickListener callbacks, final SnackbarDismissListener dismissListener){
Snackbarsnackbar= Snackbar.make(rootView,message,Snackbar.LENGTH_LONG);
snackbar.setAction(actionMessage,callbacks);
if (dismissListener != null){
snackbar.getView().addOnAttachStateChangeListener(newView.OnAttachStateChangeListener() {
@OverridepublicvoidonViewAttachedToWindow(View v) {
}
@OverridepublicvoidonViewDetachedFromWindow(View v) {
dismissListener.onSnackbarDismissed();
}
});
}
snackbar.show();
}
}
Solution 3:
I have the same problem, but I'm providing an 'undo' for deleting data. This is how I deal with it:
- Pretend the data is deleted from db (hide from UI, which is a list of items)
- Wait until the snack bar 'should' have dissapeared
- Send the delete call to the database
- IF, the user used the undo action, block the pending DB call
This may not work for you, since you are inserting data and you may (?) need it to be available in the database after the first action, but it will work if 'faking' the data (in the UI) is feasible. My code is not optimal, and I would call it a hack, but its the best I could find while staying within the official libraries.
// Control objectsbooleancanRemoveData=true;
ObjectremovedData= getData(id);
UI.remove(id);
// Snackbar codeSnackbarsnackbar= Snackbar.make(view, "Data removed", Snackbar.LENGTH_LONG);
snackbar.setAction("Undo", newView.OnClickListener() {
@OverridepublicvoidonClick(View v){
canRemoveData = false;
DB.remove(id);
}
});
// Handler to time the dismissal of the snackbarnewHandler(getActivity().getMainLooper()).postDelayed(newRunnable() {
@Overridepublicvoidrun() {
if(canRemoveData){
DB.remove(id);
}
}
}, (int)(snackbar.getDuration() * 1.05f));
// Here I am using a slightly longer delay before sending the db delete call,// just because I don't trust the accuracy of the Handler timing and I want// to be on the safe side. Either way this is dirty code, but the best I could do.My actual code is more complicated (dealing with the issue of canRemoveData not being accessible inside sub classes without being final, but this is basically how I managed to achieve what you are talking about. Hope someone can figure out a better solution.
Solution 4:
publicclassCustomCoordinatorLayoutextendsCoordinatorLayout {
privatebooleanmIsSnackBar=false;
privateViewmSnakBarView=null;
privateOnSnackBarListenermOnSnackBarListener=null;
publicCustomCoordinatorLayout(Context context) {
super(context);
}
publicCustomCoordinatorLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@OverrideprotectedvoidonMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if(mIsSnackBar){
// Check whether the snackbar is existed.// If it is not existed then index of the snackbar is -1.if(indexOfChild(mSnakBarView) == -1){
mSnakBarView = null;
mIsSnackBar = false;
if(mOnSnackBarListener != null)
mOnSnackBarListener.onDismiss();
Log.d("NEOSARCHIZO","SnackBar is dismissed!");
}
}
}
@OverridepublicvoidonMeasureChild(View child, int parentWidthMeasureSpec, int widthUsed, int parentHeightMeasureSpec, int heightUsed) {
super.onMeasureChild(child, parentWidthMeasureSpec, widthUsed, parentHeightMeasureSpec, heightUsed);
// onMeaureChild is called before onMeasure.// The view of SnackBar doesn't have an id.if(child.getId() == -1){
mIsSnackBar = true;
// Store the view of SnackBar.
mSnakBarView = child;
if(mOnSnackBarListener != null)
mOnSnackBarListener.onShow();
Log.d("NEOSARCHIZO","SnackBar is showed!");
}
}
publicvoidsetOnSnackBarListener(OnSnackBarListener onSnackBarListener){
mOnSnackBarListener = onSnackBarListener;
}
publicinterfaceOnSnackBarListener{
publicvoidonShow();
publicvoidonDismiss();
}
}
I use custom coordinatorlayout. When Snackbar is showed then onMeasure and onMeasureChild of CoordinatorLayout are called. So I overrided these methods.
Please note that you must set ids of children of the custom coordinator layout. Because I find the view of SnackBar by id. The id of SnackBar is -1.
CustomCoordinatorLayoutlayout= (CustomCoordinatorLayout)findViewById(R.id.main_content);
layout.setOnSnackBarListener(this);
Snackbar.make(layout, "Hello!", Snackbar.LENGTH_LONG).setAction("UNDO", newView.OnClickListener() {
@OverridepublicvoidonClick(View v) {
//TODO something
}
}).show();
Implement OnSnackBarListener in your activity or fragment. When the snackbar is showed then it will call onShow. And the one is dismissed then it will call onDismiss.
Solution 5:
To improve Hitch.united answer
boolean mAllowedToRemove = true;
Snackbar snack = Snackbar.make(mView, mSnackTitle, Snackbar.LENGTH_LONG);
snack.setAction(getString(R.string.snackbar_undo), newOnClickListener() {
@OverridepublicvoidonClick(View v) {
mAllowedToRemove = false;
// undo
...
}
});
snack.getView().addOnAttachStateChangeListener(newView.OnAttachStateChangeListener() {
@OverridepublicvoidonViewAttachedToWindow(View v) {
}
@OverridepublicvoidonViewDetachedFromWindow(View v) {
if(!mAllowedToRemove){
// handle actions like http requests
...
}
}
});
snack.show();
Post a Comment for "Snackbar In Support Library Doesn't Include Ondismisslistener()?"