[英]Switching fragments during runtime
我发现本文最有帮助/无益: 在单个活动中的片段之间切换
我不确定我们是否有同样的问题,这就是为什么无论如何我都会发布代码以查看我是否也以错误的方式进行操作。 上述解决方案似乎使用一种我认为不需要片段的方法来解决问题。 还是很新,我的逻辑错是很好。 无论如何,这是我的问题:
我有一个屏幕出现,它为您提供两种选择,蓝色药丸或红色药丸(这两个按钮包含在一个片段中)。 如果选择红色药片,则新片段将用文本“您留在仙境...”替换现有片段,或者如果选择蓝色药片,则片段将用文本“故事结束...”替换现有片段。
这只是我现在用来教自己片段的示例。
我有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>
蓝色药丸碎片:
<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:用于完成此操作的适当代码是什么? FragmentTransaction?
上述解决方案似乎使用一种我认为不需要片段的方法来解决问题。
我相信您发布的链接不会完全避免碎片。 片段可以在xml文件中指定(在这种情况下它们是永久的),也可以在代码中使用FragmentTransactions指定(在这种情况下可以添加,删除它们)。
public void red(View view) {
// Change fragment code here?
}
public void blue(View view) {
// Change fragment code here?
}
我认为您希望此处的onClickListeners链接到您的图像按钮。 在它们内部,您将调用FragmentTransaction添加/删除/交换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.