I have 5 sliding tabs in ViewPager, each of which contains a different fragment. If the data does not load, I want to notify the user through Snackbar. It works great.
I am trying to achieve if the data is not loading in a specific fragment, I want to show Snackbaronly in this specific fragment. Is it possible?
I will clarify:
Consider a sliding layout of tabs with 3 fragments like A, B and C. All 3 fragments fulfill some network request and load data. If for some reason the data is not loaded in fragment A, the snack bar should be displayed indefinitely. And if the user goes to fragment B (in which the data is loaded successfully), the snackbar should not be visible.
Thus, the snack bar should be visible only in the fragment that could not load the data.
Fragment Code:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment_latest"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:attr/windowBackground"
tools:context=".MainFragment">
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe_view"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/date_layout"
android:visibility="gone"
android:layout_gravity="center_horizontal"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<android.support.v7.widget.AppCompatSpinner
android:id="@+id/country_select"
style="@style/Base.Widget.AppCompat.Spinner.Underlined"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="@array/country"
/>
<android.support.v7.widget.AppCompatSpinner
android:id="@+id/month_select"
style="@style/Base.Widget.AppCompat.Spinner.Underlined"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="@array/month"
/>
<android.support.v7.widget.AppCompatSpinner
android:id="@+id/year_select"
style="@style/Base.Widget.AppCompat.Spinner.Underlined"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="@array/year"
/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/latest_recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</android.support.v4.widget.SwipeRefreshLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<TextView
android:id="@+id/not_available"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium"
android:gravity="center"
android:textSize="16sp" />
</LinearLayout>
Diner Code:
if (Utils.isNetworkAvailable(getActivity())) {
adapter.notifyDataSetChanged();
loadData();
} else {
swipeRefreshLayout.setRefreshing(false);
textView.setText(getString(R.string.check_internet));
Snackbar.make(frameLayout, "Loading failed.", Snackbar.LENGTH_INDEFINITE).setAction("Retry", new View.OnClickListener() {
@Override
public void onClick(View view) {
((MainActivity) getActivity()).loadAds();
refreshData();
}
}).show();
}
Where
FrameLayout frameLayout = (FrameLayout) v.findViewById(R.id.fragment_latest);
source
share