2016-10-13 7 views
4

ViewPagerに5つのスライディングタブがあり、それぞれが異なるフラグメントを保持しています。データの読み込みに失敗した場合は、Snackbar経由でユーザーに通知します。それは正常に動作しています。特定のフラグメントのスナックバー

私が達成しようとしているのは、データが特定のフラグメントに読み込まれなかった場合、その特定のフラグメントにのみSnackbarを表示することです。出来ますか?

私は詳しく説明します:

をすべて3つのフラグメントは、いくつかのネットワークのクエリを実行し、データをロードA、Bのように3つの断片、& C.とスライドタブレイアウトを考えてみましょう。なんらかの理由でフラグメントAにデータがロードされない場合は、スナップバーを無期限に表示する必要があります。また、ユーザーがフラグメントB(データが正常に読み込まれる)にスライドすると、スナックバーは表示されません。

したがって、スナップバーは、データのロードに失敗したフラグメントでのみ表示する必要があります。

断片コード:

<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> 

スナックバーコード:

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(); 
    } 

あなたは何ができるかどこ

FrameLayout frameLayout = (FrameLayout) v.findViewById(R.id.fragment_latest); 
+0

の下に使用してスナックバーを閉じ見えなくなったときの断片でグローバル変数にsetUserVisibleHintや店舗snakbarをオーバーライドしている**私はスナックバーを表示したいですその特定の断片にのみそれは可能ですか?**これによってあなたは何を意味しますか?そのフラグメントが表示されたら、snakcbarだけを表示することを意味しますか? –

+0

@NJ私はいくつかの詳細で投稿を更新しました。それを見てください。 – hemantv

+0

フラグメントからのsnackbarのパスビューを表示しているので、ビューが非表示になったときにsanckbarは自動的に非表示になります –

答えて

5

フラグメントは方法

@Override 
public void setUserVisibleHint(boolean visible) 
{ 
    super.setUserVisibleHint(visible); 
    if (!visible && snackbar != null){ 
     snackbar.dismiss() 
    } 
} 
+1

それは働いた!私は今、忍耐強く、大学に行くことができます、ありがとう。 :) – hemantv

+0

@hemantvようこそ、よろしくお願いします:) –

関連する問題