我有5个滑动标签ViewPager,每个标签都有一个不同的片段.如果数据无法加载,我想通过Snackbar通知用户.它工作正常.
我想要实现的是,如果数据无法加载到特定片段中,我只想Snackbar在该特定片段中显示.可能吗?
我会详细说明:
考虑一个滑动标签布局,其中包含3个片段,如A,B和C.所有3个片段执行一些网络查询并加载数据.如果由于某种原因数据无法加载到片段A中,则应该无限期地显示快餐栏.如果用户滑动到片段B(成功加载数据),则不应显示snackbar.
因此,零食栏应仅在未能加载数据的片段中可见.
片段代码:
<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>
Run Code Online (Sandbox Code Playgroud)
小吃代码:
if (Utils.isNetworkAvailable(getActivity())) { …Run Code Online (Sandbox Code Playgroud)