【问题标题】:Nested fragments - Screen of Frag2 stays empty嵌套片段 - Frag2 的屏幕保持为空
【发布时间】:2017-09-06 15:04:16
【问题描述】:

我正在遭受嵌套片段的困扰。我有一个调用片段 1 的主要活动,该片段又通过按钮调用片段。片段 frag2 已很好地实例化,但屏幕为空白。 我的代码有什么明显的地方吗?

主活动

import android.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FragmentManager fragmentManager;
        Frag1 f1 = new Frag1();
        fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.content_frame,
                f1).commit();
    }
}

主要活动 xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.narb.nestedfragments.MainActivity">


    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</RelativeLayout>

片段 1 及其 xml 布局:

import android.app.FragmentTransaction;
import android.content.Context;
import android.os.Bundle;
import android.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;


/**
 * A simple {@link Fragment} subclass.
 */
public class Frag1 extends android.app.Fragment {
    Context context;
    private Button back1;
    RelativeLayout rl1;

    public Frag1() {
        // Required empty public constructor
    }

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

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        context = getActivity().getApplicationContext();

        initFindView();

        back1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Log.i("frag1","createdview");
                //getActivity().getFragmentManager().popBackStackImmediate();
                rl1.setVisibility(View.INVISIBLE);
                FragmentTransaction ft = getFragmentManager().beginTransaction();
                Frag2 f2 = new Frag2();
                ft.replace(R.id.fl1, f2);
                ft.addToBackStack(null);
                ft.commit();
            }
        });

    }

    private void initFindView(){
        back1 = (Button) getActivity().findViewById(R.id.btn1);
        rl1 = (RelativeLayout) getActivity().findViewById(R.id.rl1);
    }
}

在调用 frag2 之前我需要使布局 frag 1 不可见是否正常?

最后是我的片段 2 及其布局。我看到了 frag 2 的日志,但屏幕是空的:

import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;


public class Frag2 extends Fragment {
    Context context;
    private Button btn2;

    public Frag2() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootview;
        rootview = inflater.inflate(R.layout.fragment_frag2, container, false);
        Log.i("frag2","createdview");
        return rootview;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        context = getActivity().getApplicationContext();
        Log.i("frag2","onactivitycreated");

        btn2 = (Button) getActivity().findViewById(R.id.btn2);
        btn2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Log.i("frag2","onactivitycreated");
            }
        });

    }
}

<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"
    tools:context="com.narb.nestedfragments.Frag2">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Test 2" />

    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="60dp"
        android:text="back"
        />
</RelativeLayout>

【问题讨论】:

  • 在调用 frag2 之前我需要使布局 frag 1 不可见是否正常? 不...嵌套?那你为什么不使用getChildFragmentManager
  • fragment2 - textview 高度是 match_parent 更改为 wrap_content
  • fragment_frag1 xml 未附加..

标签: android android-fragments android-nested-fragment


【解决方案1】:

如果您的片段 1 有另一个 FrameLayout 替换片段 2,则在片段 1 中使用 getChildFragmentManager() 而不是 getFragmentManager(),

否则,您在片段 1 内的 replace() 方法 R.id.fl1 中传递了错误的容器 ID。你应该在那里传递R.id.content_frame

【讨论】:

  • 知道了。我想做的正是你所描述的。现在可以了。最重要的是,我添加了 popBackStack 也可以。出于我的好奇心,您将在什么情况下使用 getChildFragmentManager?我开始扯头发。这个片段的东西不容易掌握。非常感谢!
  • 例如,如果您的片段有 ViewPager,其中每个页面都是片段,那么您需要使用 getChildFragmentManager(),因为它们是片段中的片段。
【解决方案2】:

您尚未发布 fragment1 XML 代码。假设RelativeLayout (rl1) 是您的父布局,并且您在rl1 中有FrameLayout (fl1)。您正在使父布局不可见。所以fragment2是不可见的。

如果您不想在显示时显示片段1,则不需要嵌套片段。

你应该这样调用来加载第二个片段。

getFragmentManager().beginTransaction()
                .replace(R.id.content_frame ,new Frag2());
                .addToBackStack(null);
                .commit();

【讨论】:

    【解决方案3】:

    在 Frag1 类中使用 activity_main 的 content_frame id

    back1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Log.i("frag1", "createdview");
                //getActivity().getFragmentManager().popBackStackImmediate();
                rl1.setVisibility(View.INVISIBLE);
                FragmentTransaction ft = getFragmentManager().beginTransaction();
                Frag2 f2 = new Frag2();
                ft.replace(R.id.content_frame, f2);
                ft.addToBackStack(null);
                ft.commit();
            }
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-11
      • 1970-01-01
      • 1970-01-01
      • 2013-01-28
      相关资源
      最近更新 更多