【问题标题】:Fragment not replacing parent activity's container layout片段不替换父活动的容器布局
【发布时间】:2016-05-10 16:03:33
【问题描述】:

我在我的 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_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>

这是我在我的活动中实例化 RecyclerView 的代码。

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
recyclerAdapter = new RecyclerAdapter(Participant_Data_List,getApplicationContext());
recyclerView.setAdapter(recyclerAdapter);
final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(layoutManager);

这个活动包含一个导航抽屉,我从中加载不同的片段。

我想要的是,当一个片段加载时,它会替换我的框架布局的内容。这是负责加载片段的代码。

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 在活动加载时默认实例化。

这是我要加载和替换回收站视图的片段图片。

但是当我加载这个片段时,它不是替换,而是在回收器视图上重叠。

任何帮助将不胜感激!

更新 #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--;
}

【问题讨论】:

  • 只是添加背景颜色?你如何删除这个片段?
  • 请参阅更新#2
  • 请按比例缩小图像。
  • 感谢您的建议!
  • 仍然是重叠的,所以可以为片段设置一些背景?每个“开关”都在使用clearStack() 删除片段,它有效吗?你能产生三个片段重叠的情况吗?据我所知ft.replace(...); 实际上正在取代... dobra sugestia Marcin

标签: java android android-layout android-fragments


【解决方案1】:

试试这个:

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();
    }
}

【讨论】:

  • 这段代码确实避免了重叠,但是当我按返回键返回主活动时,视图消失了,主活动为空白。有什么解决方法吗?
  • 当需要显示时,您可以用setVisibility(View.GONE) 代替frameLayout.removeAllViews() 代替RecyclerViewView.VISIBLE(可能与clearStack 方法相关)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-05-30
  • 1970-01-01
  • 2017-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多