I have a recyclerview which implements the swipetorefresh
layout - if you swipe your finger down the list, it will load newer posts. It also has a onloadmorelistener
which serves to paginate my recyclerview - if you swipe your finger up the recyclerview, it will load older posts.
I'm trying to build in a snackbar
notification where if the user is offline and he tries to complete some type of network activity, like loading old or new posts, the snackbar will appear telling him that there is "No internet".
In the case where there is no internet and the user swipes up and swipes down on the recyclerview, two snackbar notification displaying the same "No internet" message is shown. I actually just want one to be shown instead.
Therefore, I'm looking for some unique way to identify the text which is shown in the snackbar so that if the text is the same in both snackbar, only one snackbar gets displayed but there is no getText()
method in snackbar and there is only setText()
: http://developer.android.com/intl/es/reference/android/support/design/widget/Snackbar.html
I also see that there are callback methods: http://developer.android.com/intl/es/reference/android/support/design/widget/Snackbar.Callback.html but none of which seems to help in my scenario.
I wish there was a unique flag that I could have given to the snackbar when it was created for identification purposes but the Snackbar.make
method doesn't allow so.
Has anyone been successful in solving an issue similar to this?
The variable _isSnackbarActive to true when snackBar is displayed and set it to false when it is closed. In your onPressed event, just check the status of the snackbar and decide if new snackbar is to be shown or not.
Snackbars provide lightweight feedback about an operation. They show a brief message at the bottom of the screen on mobile and lower left on larger devices. Snackbars appear above all other elements on screen and only one can be displayed at a time.
First, we need to show the Modal Bottom Sheet by clicking the button. We can have the bottom sheet with showModalBottomSheet method. As we can see above, we are using Scaffold in the builder of showModalBottomSheet , it is because it provides us the context for our SnackBar.
Use the snackbar callback. when it's shown add it to a list for example , then when the callback is called , remove it , so you have only still visible snackbars. example of callback :
snackbar.setCallback(new Snackbar.Callback() {
@Override
public void onDismissed(Snackbar snackbar, int event) {
//see Snackbar.Callback docs for event details
...
}
@Override
public void onShown(Snackbar snackbar) {
...
}
});
I'll try to put what Helmi said on code
private Map<String, String> snackbarList = new HashMap<>();
private CoordinatorLayout mCoordinatorLayout;
private void displaySnackbarIfNotShown(String snackbarText) {
//Check if the snackbar is already on the list
if(snackbarList.containsKey(snackbarText)){
//Snackbar already exist, you may update its text or ignore it
} else {
Snackbar mySnackbar = Snackbar.make(mCoordinatorLayout, R.string.prompt_adjust_map,
Snackbar.LENGTH_INDEFINITE);
mySnackbar.setCallback(new Snackbar.Callback() {
@Override
public void onDismissed(Snackbar snackbar, int event) {
//Remove from the snackbar list
snackbarList.remove(snackbarText);
}
@Override
public void onShown(Snackbar snackbar) {
//Add to the snackbar list
snackbarList.put(snackbarText, "");
}
});
mySnackbar.show();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With