2015-10-29 36 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