【问题标题】:Invoking a fragment from another fragment and then replacing its layout从另一个片段调用一个片段,然后替换它的布局
【发布时间】:2018-05-13 15:25:11
【问题描述】:

我对 android 开发相当陌生,并且在调用/替换片段时遇到问题。我的问题与这个问题非常相似:Android - fragment .replace() doesn't replace content - puts it on top。然而,对此的“解决方案”是,必须在代码中而不是在 xml 文件中创建片段。

我现在遇到了同样的问题,但是我的片段是在代码中动态创建的(MainActivity.java):

public void selectDrawerItem(MenuItem menuItem) {

    Fragment fragment = null;
    Class fragmentClass;
    switch(menuItem.getItemId()) {
        case R.id.nav_first_fragment:
            fragmentClass = FirstFragment.class;
            break;
        case R.id.nav_second_fragment:
            fragmentClass = SecondFragment.class;
            break;
        case R.id.nav_third_fragment:
            fragmentClass = ThirdFragment.class;
            break;
        default:
            fragmentClass = FirstFragment.class;
    }

    try {
        fragment = (Fragment) fragmentClass.newInstance();
    } catch (InstantiationException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e){
        e.printStackTrace();
    }

    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.flContent, fragment).addToBackStack(null).commit();

    menuItem.setChecked(true);
    setTitle(menuItem.getTitle());

    mDrawer.closeDrawers();
}

现在,当我尝试让该 Fragment 调用不同的 Fragment 并将其自己的内容替换为新的 Fragment 时,它只是将其内容添加到现有的内容下方而不是替换它。

我的 FirstFragment.java:

public class FirstFragment extends Fragment {

private View v;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    v = inflater.inflate(R.layout.content_first,container, false);
    configureImageButton();
    return v;
}

private void configureImageButton() {
    // TODO Auto-generated method stub
    ImageButton btn = (ImageButton) v.findViewById(R.id.imageButton1);

    btn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Toast.makeText(getActivity(), "You Clicked the button!", Toast.LENGTH_LONG).show();

            Fragment f1 = null;
            Class c;
            c = FirstFirstFragment.class;

            try {
                f1 = (Fragment) c.newInstance();
            } catch (IllegalAccessException e){
                e.printStackTrace();
            } catch (java.lang.InstantiationException e){
                e.printStackTrace();            }


            FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.tableLayout1, f1);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();

        }
    });

}

还有我的 FirstFirstFragment.java:

public class FirstFirstFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.content_first_first, container, false);
    return view;
}

我的activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<!-- This DrawerLayout has two children at the root  -->
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- This LinearLayout represents the contents of the screen  -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <!-- The ActionBar displayed at the top -->
        <include
            layout="@layout/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <!-- The main content view where fragments are loaded -->
        <FrameLayout
            android:id="@+id/flContent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <Switch
                android:id="@+id/switch1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
        </FrameLayout>
    </LinearLayout>

    <!-- The navigation drawer that comes from the left -->
    <!-- Note that `android:layout_gravity` needs to be set to 'start' -->
    <android.support.design.widget.NavigationView
        android:id="@+id/nvView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@android:color/white"
        app:menu="@menu/drawer_view"
        app:headerLayout="@layout/nav_header"/>
</android.support.v4.widget.DrawerLayout>

我在开头链接的文章的答案中读到有人遇到了这个确切的问题,并通过使用相同的 FragmentManager 解决了它。和我一样,他之前在 Activity 中使用了 getSupportFragmentManager(),在 Fragment 中使用了 getFragmentManager()。

但是 Fragment 不知道 getSupportFragmentManager(),那么它应该如何工作?

我尝试了我所知道的一切来解决这个问题,但没有任何效果。然后我读了这篇

据我所知,您不能将片段添加到片段中。在设计文档中写道:“片段必须始终嵌入在活动中,并且片段的生命周期直接受到宿主活动生命周期的影响。”因此,您的设计目前似乎无法开发。

关于 stackoverflow 的一个类似问题,现在我想知道:是否有可能让一个片段调用另一个片段,然后替换第一个片段?(而不仅仅是在下面添加它)

我真的希望这个问题是合理的,而不是一些愚蠢的初学者的错误,因为我搜索了整个互联网并没有找到正确的答案。

提前感谢您的友好回答:)。

【问题讨论】:

  • getActivity().getSupportFragmentManager()
  • 感谢您的快速回答,但我确实已经尝试过了,即使我能够从我的 Fragment 调用 getSupportFragmentManager() 方法,它仍然将其内容放在已经存在的下面而不是替换它....
  • 发布您的活动布局 xml 文件
  • 什么是fragmentTransaction.replace(R.id.tableLayout1, f1); 你应该使用fragmentTransaction.replace(R.id.flContent, f1);
  • 啊,是的,非常感谢!!它现在替换了第一个片段!但是来自activity_main的切换仍然可见......关于如何替换它的任何想法?

标签: java android android-fragments fragmentmanager


【解决方案1】:

在片段使用中获取fragmentManger

getActivity().getSupportFragmentManager()

As mentioned你应该使用R.id.flContent作为你的框架布局宿主所以使用

fragmentTransaction.replace(R.id.flContent, f1);

而不是

fragmentTransaction.replace(R.id.tableLayout1, f1); 

【讨论】:

  • 很高兴能帮上忙,编码愉快!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-18
相关资源
最近更新 更多