在运行时切换片段

时间:2012-07-20 04:02:06

标签: java android android-fragments

我发现这篇文章最有帮助/最无益:Switching between Fragments in a single Activity

我不确定我们是否有同样的问题,这就是为什么我要发布我的代码,看看我是否也采用了错误的方式。上面提到的解决方案似乎使用我认为不需要片段的方法来解决问题。还是很新的,我的逻辑错误可能很好。无论如何这是我的问题:

我有一个屏幕出现,它给你两个选择,蓝色药丸或红色药丸(这两个按钮包含在一个片段中)。如果您选择红色药丸,则新片段将替换现有片段,文本为“您留在仙境......”或者如果选择了蓝色药丸,则片段将替换现有片段,文本“故事结束......”。

这只是我现在用来教自己片段的例子。

我有 1个活动 1个布局 3个片段

我的活动:

package com.example.learn.fragments;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;

public class MainActivity extends Activity {

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    /* Add a class to handle fragment */
    public static class SSFFragment extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            View v = inflater.inflate(R.layout.choose_pill_frag, container,
                    false);
            return v;
        }
    }

    public void red(View view) {
        // Change fragment code here?
    }

    public void blue(View view) {
        // Change fragment code here?
    }

}

我的布局:

<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" >

    <fragment
        android:id="@+id/frag"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.example.learn.fragments.MainActivity$SSFFragment" />

</RelativeLayout>

启动片段:

<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" >

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:onClick="blue"
        android:src="@drawable/blue" />

    <ImageButton
        android:id="@+id/imageButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:onClick="red"
        android:src="@drawable/red" />

</RelativeLayout>

Blue Pill Frag:

<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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="The story ends, you wake up in your bed and believe whatever you want to believe."
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

Red Pill Frag:

<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" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="You stay in Wonderland and I show you how deep the rabbit-hole goes."
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

问题

1:我应该以这种方式解决这个问题吗?

2:用于完成此操作的相应代码是什么? FragmentTransaction?

2 个答案:

答案 0 :(得分:1)

  

上面提到的解决方案似乎解决了使用我认为不需要片段的方法的问题。

我相信您发布的链接并不能完全避免碎片。片段可以在xml文件中指定(在这种情况下它们是永久性的)或在代码中使用FragmentTransactions(在这种情况下你可以添加,删除它们)。

public void red(View view) {
    // Change fragment code here?
}

public void blue(View view) {
    // Change fragment code here?
}

我认为您希望此处的onClickListeners链接到您的图片按钮。在他们内部,您将拨打FragmentTransaction添加/删除/交换。

答案 1 :(得分:0)

看看这个。

我似乎错误地发现了碎片。

Replacing Fragments isn't working/Am I executing this the proper way?