替换碎片不起作用/我是否以正确的方式执行此操作?

时间:2012-07-23 21:32:29

标签: java android android-fragments

花了一些时间把我的脑袋包裹在碎片上,但这应该是我关于碎片的最后一个问题,因为我认为我只是让它们失效了。我知道这是一堆乱七八糟的代码。但是我很感激帮助,以确保我没有违反任何碎片的基本规则。

我将发布我的所有代码,看看是否有人可以“查看它”,看看我是否犯了重大错误,或者我是否应该采用更简单的方法。最后,正如标题中所述,我的片段没有被替换......它被添加到顶部。

文件树:

enter image description here

MainActivity.java:

package com.example.learn.fragments;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;

public class MainActivity extends FragmentActivity{

    @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) {


        // Create new fragment and transaction
        ExampleFragments newFragment = new ExampleFragments();
        android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        // Replace whatever is in the fragment_container view with this fragment,
        // and add the transaction to the back stack
        transaction.replace(R.id.frag, newFragment);
        transaction.addToBackStack(null);

        // Commit the transaction
        transaction.commit();
    }

    public void blue(View view) {
        //Figure out code for "red" first
    }

}

ExampleFragments.java:

package com.example.learn.fragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class ExampleFragments extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.red_pill_frag, container, false);
    }
}

ActivityMain.xml:

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

choose_pill_frag.xml

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

red_pill_frag.xml

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

应用程序应显示两个按钮。这两个按钮存在于一个片段中,然后如果您点击一个按钮,片段将被替换为显示正确文本的新片段。截至目前,它应该替换,但它似乎只是将它添加到顶部。

5 个答案:

答案 0 :(得分:25)

而不是<fragment>在布局xml中使用<FrameLayout>进行活动。

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container_id"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

然后在FragmentActivity onCreate中添加初始片段(在您的情况下为SSFFragment):

    FragmentA fragmentA = new FragmentA();
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.add(R.id.container_id, fragmentA);
    transaction.commit();

从内部片段中,您可以替换容器中的片段。

class FragmentA extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        Button button = new Button(getActivity());
        button.setText("Replace");
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                FragmentB fragmentB = new FragmentB();
                transaction.replace(R.id.container_id, fragmentB);
                transaction.commit();
            }
        });
        return button;
    }
}

答案 1 :(得分:4)

以下是您真实问题的答案...因为这是your original post产生的第二个问题,我修改了解决方案,以另一种方式获取该碎片:

Fragment details = (Fragment)getSupportFragmentManager().findFragmentById(R.id.details);
details = new ExamplesFragment();
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.details, details);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();

此外, android.support.v4.app 部分是没有必要的,坦率地说,通过在整个过程中添加和删除它,可能导致“走错路”类型的工作时间。代码(只要你正在使用:)

import android.support.v4.app.FragmentTransaction;

在我的示例中,您不需要导入FragmentManager的支持。但是,如果您遇到错误,请确保已将库本身导入“libs”文件夹。

此解决方案将解决重叠片段问题,并希望节省人们数小时修补更换碎片。

答案 2 :(得分:2)

我正面临着同样的问题,我只是用主线布局替换片段并用线性布局来猜测它的工作原理......它奇怪的不知道它是如何工作的。我正在使用actionbar在片段之间切换  替换我的代码是:

 protected class MyTabsListener1 implements ActionBar.TabListener{
        private Fragment frag ;
        public MyTabsListener1 ( Fragment frag){
            this.frag = frag;
        }

        @Override
        public void onTabReselected(Tab tab, FragmentTransaction ft) {
            //  TODO Auto-generated method stub

        }

        @Override
        public void onTabSelected(Tab tab, FragmentTransaction ft) {

                switch (tab.getPosition()){
                case 0:

                        ft.replace(R.id.replace, homeFrag);
                    break;

                case 1:
                    ft.replace(R.id.replace, createFrag);
                        break;

                case 2:
                    ft.replace(R.id.replace, ioListFrag);
                    break;
                case 3:

                    ft.replace(R.id.replace, settingFrag);

                    break;      


                default: 


                    break;

                }

            }

我的主要布局是:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <LinearLayout
        android:id="@+id/replace"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" >
    </LinearLayout>

</LinearLayout>

答案 3 :(得分:1)

简而言之,如果您使用片段标记在XML中定义片段,则无法替换片段。相反,正如@pawelzieba建议在你的布局中添加Frame标签,找到它并在那里添加,删除,替换片段。希望它有所帮助。干杯

答案 4 :(得分:0)

使用片段的主要好处是能够让它们占据屏幕的一部分而不是整个屏幕,这就是活动所做的。

如果您只是为一个小屏幕制作应用程序,其功能类似于活动,但是在片段中编码,则只需为每个片段单独FragmentActivity

将此作为FragmentActivity的onCreate:

public void onCreate(Bundle savedInstanceState) {
    setContentView(R.layout.emptylayout);

    FragmentManager fragmentManager = getSupportFragmentManager(); //Or getFragmentManager() if you're not using the support library
    FragmentTransaction fragmentTransaction = fragmentManager
            .beginTransaction();
    YourFragment fragment = new YourFragment();
    fragmentTransaction.add(R.id.emptyview, fragment);
    fragmentTransaction.commit();
}

其中layout / emptylayout是带有FrameLayout的XML布局文件。 id / emptyview就是FrameLayout。

如果要为片段的实际布局使用XML,请为实际片段创建单独的XML布局,并在片段的“onCreateView”中对其进行充气:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.files, container, false);
            // Do stuff here
    return view;
}

然后只需使用startActivity(new Intent(getActivity(), YourFragmentActivity.class))从片段中启动FragmentActivity。

这似乎是多余的,是的,但如果你以后要针对更大的屏幕(如果没有,你为什么要打扰片段呢?),那么从长远来看它会更容易。

相关问题