从一个Fragment中调用另一个Fragment

16

我有一个带有按钮的活动,当我点击它时,我调用了另一个带有按钮的片段。但是当我点击片段上的按钮时,我无法调用第二个片段。这是我的源代码,非常简单:

activity_main.xml:
<LinearLayout 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"
android:orientation="horizontal"
tools:context=".MainActivity" >

<Button 
    android:id="@+id/btn_click"
    android:text="Call Fragment"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:onClick="onClick"
    />
</LinearLayout>

fragment1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f0f0f0"
android:orientation="vertical" >

<TextView 
    android:id="@+id/fragment1"
    android:text="Fragment 1"
    android:textSize="25sp"
    android:gravity="center"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    />

<Button 
    android:id="@+id/btn_frag2"
    android:text="Call Fragment 2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
</LinearLayout>

fragment2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f0f0f0"
android:orientation="vertical" >

<TextView 
    android:id="@+id/fragment2"
    android:text="Fragment 2"
    android:textSize="25sp"
    android:gravity="center_vertical|center_horizontal"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    />
</LinearLayout>

主活动(MainActivity.java)

public class MainActivity extends Activity {

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

public void onClick(View v) {
    Fragment1 fragment1 = new Fragment1();
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(android.R.id.content, fragment1);
    fragmentTransaction.commit();
}

}

Fragment1.java

public class Fragment1 extends Fragment {

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

    public void onClick2(View view) {
        Fragment2 fragment2 = new Fragment2();
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.fragment1, fragment2);
        fragmentTransaction.commit();
    }
}

Fragment2.java

public class Fragment2 extends Fragment {

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

我的代码有什么错误?

7个回答

32

我认为现在片段嵌套已经可用,只需要更新向后兼容的jar包。

现在让我们深入研究问题本身。

public void onClick2(View view) {
    Fragment2 fragment2 = new Fragment2();
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.fragment1, fragment2);
    fragmentTransaction.commit();
}

我认为 R.id.fragment1 属于一个 TextView,而不是一个好的包含子视图的地方,因为它不是一个 ViewGroup。你可以从xml中删除 textView 并用 LinearLayout 替换它,如果还有问题,请告诉我错误信息。

fragment1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f0f0f0"
android:orientation="vertical" >

<LinearLayout 
    android:id="@+id/fragment1"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    />

<Button 
    android:id="@+id/btn_frag2"
    android:text="Call Fragment 2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
</LinearLayout>

评论中的错误已更新

public class Fragment1 extends Fragment implements OnClickListener{

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment1, container, false);
((Button) v.findViewById(R.id.btn_frag2)).setOnClickListener(this);
    return v;
}

public void onClick(View view) {
    Fragment2 fragment2 = new Fragment2();
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction =        fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.container, fragment2);
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();
}

}

它不起作用。我用LinearLayout替换了TextView,第一个片段可以正常打开,但是当我按下该片段上的按钮以打开第二个片段时,应用程序会因错误而停止。 在活动类com.example.fragments.MainActivity中找不到方法onClick2(View)来处理id为“btn_frag2”的android.widget.Button视图上的onClick处理程序。 - Jack Daniel
1
它可以工作,但我将R.id.fragment1替换为android.R.id.content,并且对于后退堆栈添加了fragmentTransaction.addToBackStack(null); - Jack Daniel
@JackDaniel,android.R.id.content是什么?在哪里可以找到上述类的内容? - Shoeb Ahmed Siddique
1
这个解决方案在加入@JackDaniel建议的更改后运行良好。谢谢。 - Ishara Amarasekera

5

如果你想用Fragment2替换整个Fragment1,需要在MainActivity内使用以下代码:

Fragment2 fragment2 = new Fragment2();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(android.R.id.content, fragment2);
fragmentTransaction.commit();

只需将此代码放入 MainActivity 中的一个方法中,然后从 Fragment1 调用该方法即可。


当我旋转手机时,第二个碎片消失了,第一个碎片显示在屏幕上。 - vinicius gati

2
这是解决相同问题的答案。Fragment2.java 是持有fragment2.xml布局的碎片类。
public void onClick(View v) {
                    Fragment  fragment2 = new Fragement2();
                    FragmentManager fragmentManager = getFragmentManager();
                    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                    fragmentTransaction.replace(R.id.frame_container, fragment2);
                    fragmentTransaction.addToBackStack(null);
                    fragmentTransaction.commit();   
                }

0

在MainActivity中

private static android.support.v4.app.FragmentManager fragmentManager;

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

    fragmentManager = getSupportFragmentManager();                                                                 


    }

public void secondFragment() {

    fragmentManager
            .beginTransaction()
            .setCustomAnimations(R.anim.right_enter, R.anim.left_out)
            .replace(R.id.frameContainer, new secondFragment(), "secondFragmentTag").addToBackStack(null)
            .commit();
}

在 FirstFragment 中这样调用 SecondFragment:

new MainActivity().secondFragment();

0
这段代码可以让我从子片段调用父片段的方法。
ParentFragment parent = (ParentFragment) activity.getFragmentManager().findViewById(R.id.contaniner);

parent.callMethod();


0
我会使用监听器。Fragment1 调用监听器(主 activity)。

-2

只需要这样做:getTabAt(你的选项卡的索引)

        ActionBar actionBar = getSupportActionBar();
        actionBar.selectTab(actionBar.getTabAt(0));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接