【发布时间】:2016-04-04 08:50:24
【问题描述】:
New Layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/L1" >
<fragment
android:id="@+id/fragment_place"
android:name="com.javacodegeeks.android.fragmentstest.HomeFrag"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
<RelativeLayout
android:id="@+id/L1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<ImageView
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/home"
android:onClick="selectFrag"
android:layout_alignParentLeft="true"/>
<ImageView
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/search"
android:onClick="selectFrag"
android:layout_toRightOf="@+id/button1"/>
<ImageView
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/map"
android:onClick="selectFrag"
android:layout_toRightOf="@+id/button2"/>
</RelativeLayout>
</RelativeLayout>
我正在学习使用 FragmentManager 在 ImageView 单击上更改片段。当我单击第一个 imageView 时,带有文本“home”的主要片段布局显示良好。但是当点击其他ImageViews改变fragment页面时,它会更改为新的fragment页面,但不会删除之前的页面。结果,上一页与新页混在一起。 It look like this. 截图显示主页,文本为“home”,即上一页,新页面,文本为“search”。点击新的ImageView时如何删除上一页?
MainActivity
public class MainActivity extends Activity {
private static final int RESULT_SETTINGS = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demo);
}
public void selectFrag(View view) {
Fragment fr = null;
if(view == findViewById(R.id.button1)) {
fr = new HomeFrag();
}else if(view == findViewById(R.id.button2)){
fr = new SearchFrag();
}else if(view == findViewById(R.id.button3)){
fr = new MapFrag();
}
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_place, fr);
fragmentTransaction.commit();
}
}
【问题讨论】:
-
在框架布局中使用片段阅读此answer
标签: android android-fragments fragmentmanager