2016-05-20 95 views
3

我想只在一個片段上顯示我的FloatingActionButton,並在其餘部分隱藏。我已經嘗試了多個答案,沒有任何工作。我想這個解決方案:如何使用ViewPager中的片段隱藏和顯示FloatingActionButton?

Hide a Floating Action Button of another Layout

但它無法正常工作。我試圖在兩個片段上添加按鈕,其中一個顯示,另一個隱藏FAB並且工作正常,但是一旦我移除按鈕,它就不會自動顯示。這裏是我的代碼:

hidden_​​fragment.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

</LinearLayout> 

shown_fragment.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

</LinearLayout> 

HiddenFragment.java:

public class HiddenFragment extends Fragment { 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     if (container == null) { 
      return null; 
     } 
     View view = inflater.inflate(R.layout.hidden_fragment, container, false); 

     final FloatingActionButton fab = ((MainActivity) getActivity()).getFloatingActionButton(); 

     if (fab != null) { 
      ((MainActivity) getActivity()).hideFloatingActionButton(); 
     } 

     return view; 
    } 

} 

ShownFragment.java:

public class ShownFragment extends Fragment { 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     if (container == null) { 
      return null; 
     } 
     View view = inflater.inflate(R.layout.hidden_fragment, container, false); 

     final FloatingActionButton fab = ((MainActivity) getActivity()).getFloatingActionButton(); 

     if (fab != null) { 
      ((MainActivity) getActivity()).showFloatingActionButton(); 
     } 

     return view; 
    } 

} 

在我MainActivity.java我有這樣的:

public FloatingActionButton getFloatingActionButton() { 
    return fab; 
} 

public void showFloatingActionButton() { 
    fab.show(); 
} 

public void hideFloatingActionButton() { 
    fab.hide(); 
} 

我現在有它不工作的方式。當我啓動應用程序時,第一個啓動的片段是HiddenFragment。但是當我去到ShownFragment時,FAB不會出現。我嘗試了一種不同的方法,我爲每個片段添加了一個按鈕,並將showFloatingActionButton()和hideFloatingActionButton()添加到按鈕,並且工作正常。有誰知道我做錯了什麼?

+0

,你必須得到改變的知名度試試這個 CoordinatorLayout.LayoutParams P =(CoordinatorLayout.LayoutParams)fab.getLayoutParams()之前擺脫了錨; p.setAnchorId(View.NO_ID); fab.setLayoutParams(p); fab.setVisibility(View.GONE); –

+0

@RakshitNawani將這個放置在MainActivity或每個片段? –

+0

當您想要隱藏它時,它不會顯示,直到您將其可見性設置爲可見 –

回答

6

若要在ViewPager中顯示/隱藏具有碎片的FloatingActionButton,只需使用ViewPager.OnPageChangeListener並顯示FloatingActionButton以獲取您想要的位置,然後隱藏它以查找您想要的位置。

這將走在活動:

mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { 

      @Override 
      public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 

      } 

      @Override 
      public void onPageSelected(int position) { 
       switch (position) { 
       case 0: 
        fab.hide(); 
        break; 
       case 1: 
        fab.show(); 
        break; 
       case 3: 
        fab.hide(); 
        break; 
       default: 
        fab.hide(); 
        break; 
       } 
      } 

      @Override 
      public void onPageScrollStateChanged(int state) { 

      } 
     }); 
+1

這工作!這很明顯,我在想太多。非常感謝你的幫助!! :) –

+0

這個例子只適用於晶圓廠的一個片段,但是如果我想要所有的片段都有晶圓廠,這段代碼就不會顯示動畫進/出...因爲開關總是顯示晶圓廠,永遠不會隱藏... –

+0

@確保晶圓廠在活動佈局中。只需切換使用晶圓廠的每個片段的圖標/動作即可。有關更多詳細信息,請參見此處:http:// stackoverflow。COM /文檔/機器人/ 2979/floatingactionbutton/13346 /顯示和隱藏-floatingactionbutton-上滑動#噸= 20160926031742388375 –