Java 片段不替换父活动';集装箱布局
我在android应用程序中有一个活动,我在框架布局中放置了一个RecyclerView小部件。框架布局用作父容器Java 片段不替换父活动';集装箱布局,java,android,android-layout,android-fragments,Java,Android,Android Layout,Android Fragments,我在android应用程序中有一个活动,我在框架布局中放置了一个RecyclerView小部件。框架布局用作父容器 <RelativeLayout 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_pa
<RelativeLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:paddingBottom="0dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
tools:context="com.example.asad.comppecv2.AdminActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/default_toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
/>
<FrameLayout
android:id="@+id/container_body"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_below="@+id/default_toolbar">
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_below="@id/default_toolbar"
android:id="@+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingBottom="16dp"
android:paddingTop="16dp"
android:scrollbars="vertical" />
</FrameLayout>
</RelativeLayout>
这个活动包含一个导航抽屉,我从中加载不同的片段
我想要的是,当一个片段加载时,它会替换框架布局的内容。下面是负责加载片段的代码
private void initiateFragment(){
if (fragment != null) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.addToBackStack(null);
ft.replace(R.id.container_body, fragment);
ft.commit();
}
}
下面是我的活动的图片,当活动启动时,默认情况下会实例化RecyclerView
这是我想加载并替换回收器视图的片段的图片
但是当我加载这个片段时,它与recycler视图重叠,而不是替换
任何帮助都将不胜感激
更新#2
这是负责处理片段的代码块
public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
result.setSelection(position,false);
clearStack();
switch (position) {
case 1:
fragment = new admin_fragment_data_entry();
break;
case 2:
fragment = new admin_fragment_data_edit();
break;
case 3:
fragment = new admin_fragment_judge_data();
break;
case 4:
fragment = new admin_fragment_schedule();
break;
}
initiateFragment();
return false;
}
private void clearStack(){
int count = getSupportFragmentManager().getBackStackEntryCount();
while(count > 0){
getSupportFragmentManager().popBackStack();
count--;
}
试试这个:
private void initiateFragment(){
if (fragment != null) {
FrameLayout frameLayout = (FrameLayout) findViewById(R.id.container_body);
frameLayout.removeAllViews();
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.addToBackStack(null);
ft.replace(R.id.container_body, fragment);
ft.commit();
}
}
试试这个:
private void initiateFragment(){
if (fragment != null) {
FrameLayout frameLayout = (FrameLayout) findViewById(R.id.container_body);
frameLayout.removeAllViews();
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.addToBackStack(null);
ft.replace(R.id.container_body, fragment);
ft.commit();
}
}
只是添加背景色?如何删除此片段?请参阅更新#2请缩小图像。谢谢您的建议!不过,它是重叠的,所以也许可以为片段设置一些背景?每个“开关”都在使用clearStack()删除碎片,它是否正常工作?你能制造出三个碎片重叠的情况吗?据我所知,
ft.replace(…)代码>实际上正在替换。。。dobra sugestia Marcinjust是否添加背景色?如何删除此片段?请参阅更新#2请缩小图像。谢谢您的建议!不过,它是重叠的,所以也许可以为片段设置一些背景?每个“开关”都在使用clearStack()删除碎片,它是否正常工作?你能制造出三个碎片重叠的情况吗?据我所知,ft.replace(…)代码>实际上正在替换。。。dobra sugestia Marcint这段代码确实避免了重叠,但当我按back返回到主活动时,视图消失了,主活动为空。任何解决方法?当需要显示时(可能与clearStack
方法相关),您可以为您的RecyclerView
和View.VISIBLE
设置可见性(View.GONE)
,而不是frameLayout.RemoveAllView(),但当我按back返回到主活动时,视图消失,主活动为空。任何解决方法?当需要显示时,您可以为您的RecyclerView
和View.VISIBLE设置可见性(View.GONE)
,而不是frameLayout.removeAllViews()
(可能与clearStack
方法相关)