简体   繁体   English

在运行时切换片段

[英]Switching fragments during runtime

I found this article most helpful/unhelpful: Switching between Fragments in a single Activity 我发现本文最有帮助/无益: 在单个活动中的片段之间切换

I'm not sure if we have the same problem, which is why I'm posting my code anyway to see if I'm also going about this the wrong way. 我不确定我们是否有同样的问题,这就是为什么无论如何我都会发布代码以查看我是否也以错误的方式进行操作。 The above mentioned solution seems to solve the problem using a method that I feel makes fragments unnecessary. 上述解决方案似乎使用一种我认为不需要片段的方法来解决问题。 Still very new, it could be very well that my logic is wrong. 还是很新,我的逻辑错是很好。 Anyway here is my problem: 无论如何,这是我的问题:

I have a screen that comes up and it gives you two choices, either blue pill or red pill (these two buttons are contained in a fragment). 我有一个屏幕出现,它为您提供两种选择,蓝色药丸或红色药丸(这两个按钮包含在一个片段中)。 If you select the red pill a new fragment replaces the existing fragment with the text "You stay in wonderland..." or if the blue pill is selected a fragment replaces the existing fragment with the text "The story ends...". 如果选择红色药片,则新片段将用文本“您留在仙境...”替换现有片段,或者如果选择蓝色药片,则片段将用文本“故事结束...”替换现有片段。

This is just the example I'm using for now to teach myself fragments. 这只是我现在用来教自己片段的示例。

I have 1 activity and 1 layout and 3 fragments . 我有1个活动1个布局以及3个片段

My activity: 我的活动:

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?
    }

}

My layout: 我的布局:

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

Starting Fragment: 起始片段:

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

Issue 问题

1: Should I be going about this in this way? 1:我应该这样吗?

2: What would be the appropriate code to use to complete this? 2:用于完成此操作的适当代码是什么? FragmentTransaction? FragmentTransaction?

The above mentioned solution seems to solve the problem using a method that I feel makes fragments unnecessary. 上述解决方案似乎使用一种我认为不需要片段的方法来解决问题。

I believe the link you posted doesn't avoid fragments entirely. 我相信您发布的链接不会完全避免碎片。 Fragments can be specified in xml files (in which case they are permanent) or in code using FragmentTransactions (in which case you can add, remove them). 片段可以在xml文件中指定(在这种情况下它们是永久的),也可以在代码中使用FragmentTransactions指定(在这种情况下可以添加,删除它们)。

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

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

I think you want onClickListeners here linked to your image buttons. 我认为您希望此处的onClickListeners链接到您的图像按钮。 Inside them you will call your FragmentTransaction add/remove/swaps. 在它们内部,您将调用FragmentTransaction添加/删除/交换。

Take a look at this. 看看这个。

I was going about fragments incorrectly it seems. 我似乎正在错误地处理碎片。

Replacing Fragments isn't working/Am I executing this the proper way? 替换碎片无法正常工作/我是否以正确的方式执行此操作?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM