2015-10-29 80 views
0

我有一個ListFragment浮動操作按鈕。當按鈕被點擊時,我想用CreateListFragment替換ListFragment。相反,CreateListFragment放置在ListFragment的頂部,並且ListFragment中的按鈕和文本視圖仍然可見。我不確定哪裏出了問題。我如何完全用CreateListFragment替換ListFragment?片段交易

這裏是按鈕被點擊之前 http://i.stack.imgur.com/k2HxP.png

這裏是按鈕被點擊後,屏幕截圖的截圖 http://i.stack.imgur.com/TYN47.png

list_fragment

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       xmlns:app="http://schemas.android.com/apk/res-auto" 
       android:orientation="vertical" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
     android:id="@+id/listFrame"> 

    <android.support.v7.widget.RecyclerView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"/> 

    <android.support.design.widget.FloatingActionButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/fab" 
     android:layout_gravity="bottom|end" 
     app:backgroundTint="@color/colorAccent" 
     android:src="@drawable/create"/> 
    <TextView 
      android:id="@+id/tvEmpty" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:hint="No Shopping Lists Yet" 
      android:layout_gravity="center" 
      android:textSize="20sp"/> 


</FrameLayout>`` 

createlist_fragment

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:padding="10dp" 
     tools:context="com.lavineoluoch.helloworld.swiftpay.app.CreateListFragment"> 
<LinearLayout 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
<EditText 
     android:id="@+id/etListTitle" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="@string/title"/> 
<EditText 
     android:id="@+id/etAddItem" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="@string/items"/> 
<RelativeLayout 
     android:id="@+id/buttons" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 
    <Button 
      android:id="@+id/btnEditItem" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/btnEdit" 
      android:background="@color/colorPrimary" 
      android:textColor="@color/white" 
      /> 
</RelativeLayout> 


</LinearLayout> 

ListFragment.java

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    final View view = inflater.inflate(R.layout.list_fragment, container, false); 
    fab = (FloatingActionButton)view.findViewById(R.id.fab); 

    fab.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      CreateListFragment createListFragment = new CreateListFragment(); 
      android.support.v4.app.FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction(); 
      fragmentTransaction.replace(R.id.listFrame,createListFragment); 
      fragmentTransaction.commit(); 
     } 
    }); 
    return view; 
} 
+0

嘗試fab.hide();點擊那個 – VVB

+0

我已經試過了,沒有工作。但是,謝謝你的建議。下面的答案適用於我。 – Evo

回答

0

在根檢視你的片段插入這行代碼:

android:background="?android:windowBackground" 

當你必須通過後臺看到你的觀點仍然可以看到懷舊由於視圖尚未更新,因此被替換的片段。

上述答案應該可以解決您的問題,但是我建議您重新構建您的方法,而不是在按FAB時啓動包含CreateListFragment的新Activity。這樣做會更容易維護代碼,這也易於閱讀,並且是最佳實踐。這也意味着用戶可以按下它們的後退按鈕並返回到ListActivity/ListFragment,而無需手動處理Fragment反向堆棧。

+0

謝謝你的解決方案和建議。我一定會考慮改變我的方法。不過,我選擇使用片段是因爲我有一個導航抽屜,我希望出現在每個活動中。所以我想我會在主要內容中使用NavigationView和片段的一個活動。有沒有更好的方法可以實現我的目標? – Evo

+0

你仍然可以使用一個'NavigationDrawer'。爲所有使用「NavigationDrawer」的活動製作一個基礎'活動',並將所有抽屜邏輯放在那裏。然後從'CreateListActivity'和'ListActivity'中的新抽屜類擴展。使用這種方法也意味着如果需要取決於從中擴展的「活動」,則可以更改抽屜的內容。 – vguzzi

+0

好的,謝謝。我會嘗試。 – Evo