簡體   English   中英

在運行時切換片段

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM