无法使用findFragmentById访问我的片段

时间:2013-11-28 18:15:52

标签: java android xml android-layout android-fragments

我试图在片段之间传递数据,但我无法访问我的片段以从中获取元素。 我有多个片段显示在main.xml的FrameLayout中,其id为'content'。 它们使用Java控制。 片段布局位于单独的xml布局文件中。 如何使用findFragmentById方法访问这些片段? 或者你可以推荐别的东西。

Main.java

public class Main extends FragmentActivity implements Communicator {

    // For fragments
    FragmentManager fm;
    FragmentTransaction ft;
    Fragment myFragment;
    Button about, list, settings;

    // Classes
    Methods method = new Methods();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Preparing fragments and UI
        list = (Button) findViewById(R.id.btnList);
        about = (Button) findViewById(R.id.btnAbout);
        settings = (Button) findViewById(R.id.btnSettings);
        method.backgroundImageDefault(list);
        method.backgroundImageDefault(about);
        method.backgroundImageDefault(settings);

        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();

        if (savedInstanceState == null) {
            Welcome myFragment = new Welcome();
            ft.add(R.id.content, myFragment);
            ft.commit();
        }
        list.setOnClickListener(btnOnClickListener);
        about.setOnClickListener(btnOnClickListener);
        settings.setOnClickListener(btnOnClickListener);

    }

    // On Click listener for fragments
    Button.OnClickListener btnOnClickListener = new Button.OnClickListener() {
        @Override
        public void onClick(View v) {
            Fragment newFragment;

            if (v == list) {
                newFragment = new List();
                method.backgroundImageClicked(list, about, settings);
            } else if (v == about) {
                newFragment = new About();
                method.backgroundImageClicked(about, list, settings);
            } else if (v == settings) {
                newFragment = new Settings();
                method.backgroundImageClicked(settings, about, list);
            } else {
                newFragment = new Welcome();
            }
            FragmentManager fm1 = getSupportFragmentManager();
            FragmentTransaction ft1 = fm1.beginTransaction();
            ft1.replace(R.id.content, newFragment).addToBackStack(null)
                    .commit();

        }
    };

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void respond(String data) 
        About aboutFrag = fm.findFragmentById(R.id.layoutAbout);
        aboutFrag.changeData(data);     
    }

}

main.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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Main" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="37.5sp"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btnList"
            android:layout_width="0sp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/list_button" />

        <Button
            android:id="@+id/btnSettings"
            android:layout_width="0sp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/action_settings" />

        <Button
            android:id="@+id/btnAbout"
            android:layout_width="0sp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/about_button" />
    </LinearLayout>

    <FrameLayout
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_marginTop="37.5sp" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:3)

  

findFragmentById(int id)

     

查找由给定id标识的片段   在XML中或在事务中添加时作为容器ID膨胀。

您正在动态添加片段,因此您应该使用片段容器的id来查找片段,即

About aboutFrag = fm.findFragmentById(R.id.content);

我的建议是通过使用标签找到片段,在将每个片段添加到片段容器时设置相应的标签。例如,

Welcome myFragment = new Welcome();
ft.add(R.id.content, myFragment,"welcome tag");
ft.commit();

FragmentTransaction ft1 = fm1.beginTransaction();
ft1.replace(R.id.content, aboutFrag ,"about tag").addToBackStack(null).commit();

使用findFragmentByTag(String tag)方法找到片段。

About aboutFrag = fm.findFragmentByTag("about tag");

Welcome myFragment = fm.findFragmentByTag("welcome tag");

我希望这对你有所帮助。