Java 在运行时切换片段
我发现这篇文章非常有用/没有帮助: 我不确定我们是否有同样的问题,这就是为什么我要发布我的代码,看看我是否也用了错误的方法。上面提到的解决方案似乎使用了一种我觉得不必要的方法来解决问题。还是很新的,很可能我的逻辑是错误的。无论如何,我的问题是: 我有一个屏幕出现,它给你两个选择,要么是蓝色药丸,要么是红色药丸(这两个按钮包含在一个片段中)。如果您选择红色药丸,则新片段将用文本“you stay in wonderland…”替换现有片段;如果选择蓝色药丸,则新片段将用文本“故事结束…”替换现有片段 这就是我现在用来自学的例子 我有1个活动和1个布局和3个片段 我的活动:Java 在运行时切换片段,java,android,android-fragments,Java,Android,Android Fragments,我发现这篇文章非常有用/没有帮助: 我不确定我们是否有同样的问题,这就是为什么我要发布我的代码,看看我是否也用了错误的方法。上面提到的解决方案似乎使用了一种我觉得不必要的方法来解决问题。还是很新的,很可能我的逻辑是错误的。无论如何,我的问题是: 我有一个屏幕出现,它给你两个选择,要么是蓝色药丸,要么是红色药丸(这两个按钮包含在一个片段中)。如果您选择红色药丸,则新片段将用文本“you stay in wonderland…”替换现有片段;如果选择蓝色药丸,则新片段将用文本“故事结束…”替换现有片
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>
蓝色药片碎片:
<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>
<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>
红色药片碎片:
<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>
<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:完成此操作的适当代码是什么?零碎交易
上面提到的解决方案似乎使用了一种我觉得不必要的方法来解决问题
我相信你发布的链接不会完全避免碎片。片段可以在xml文件中指定(在这种情况下,它们是永久的),也可以在使用FragmentTransactions的代码中指定(在这种情况下,您可以添加、删除它们)
我想你希望这里的onClickListeners链接到你的图像按钮。在它们内部,您将调用您的添加/删除/交换
上面提到的解决方案似乎使用了一种我觉得不必要的方法来解决问题
我相信你发布的链接不会完全避免碎片。片段可以在xml文件中指定(在这种情况下,它们是永久的),也可以在使用FragmentTransactions的代码中指定(在这种情况下,您可以添加、删除它们)
我想你希望这里的onClickListeners链接到你的图像按钮。在它们里面,您将调用您的添加/删除/交换。看看这个
看来我是走错了路
看看这个
看来我是走错了路
那么您的问题是什么?碎片没有改变?根本没出现?对不起,我不清楚。正在更新问题。那么您的问题是什么?碎片没有改变?根本没出现?对不起,我不清楚。现在更新问题。太棒了,这个小小的方向有很长的路要走。我很感激。你看不到我在处理碎片时破坏了任何主要的“标准操作程序”,是吗?@EGHDK-Nope,这都是常见的东西。我在修改碎片的代码方面仍然存在问题。我应该更新我的代码吗?太棒了,这个小小的方向有很长的路要走。我很感激。你看不到我在处理碎片时破坏了任何主要的“标准操作程序”,是吗?@EGHDK-Nope,这都是常见的东西。我在修改碎片的代码方面仍然存在问题。我应该更新代码吗?