碎片交易
Fragment Transaction
我有一个带有浮动操作按钮的 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>``
创建list_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;
}
在片段的根视图中插入这行代码:
android:background="?android:windowBackground"
因为你有一个透视背景,你的视图仍然可以看到被替换的片段的记忆,因为视图还没有更新。
上面的答案应该可以解决您的问题,但是我建议重新构建您的方法,而不是在按下 FAB 时启动一个包含 CreateListFragment 的新 Activity。这样做会使代码更易于维护,也更易于阅读,并且是最佳实践。这也意味着用户可以按下后退按钮并导航回 ListActivity/ListFragment,而无需您手动处理 Fragment 后退堆栈。
我有一个带有浮动操作按钮的 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>``
创建list_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;
}
在片段的根视图中插入这行代码:
android:background="?android:windowBackground"
因为你有一个透视背景,你的视图仍然可以看到被替换的片段的记忆,因为视图还没有更新。
上面的答案应该可以解决您的问题,但是我建议重新构建您的方法,而不是在按下 FAB 时启动一个包含 CreateListFragment 的新 Activity。这样做会使代码更易于维护,也更易于阅读,并且是最佳实践。这也意味着用户可以按下后退按钮并导航回 ListActivity/ListFragment,而无需您手动处理 Fragment 后退堆栈。