gpt4 book ai didi

java - 无法使用 findFragmentById 访问我的 fragment

转载 作者:行者123 更新时间:2023-11-29 05:33:36 30 4
gpt4 key购买 nike

我试图在 fragment 之间传递数据,但我无法访问我的 fragment 以从中获取元素。我有多个 fragment 显示在 main.xml 的 FrameLayout 中,其 id 是“内容”。它们使用 Java 进行控制。 fragment 布局位于单独的 xml 布局文件中。如何使用 findFragmentById 方法访问这些 fragment ?或者您可以推荐其他东西。

主.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);
}

}

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

最佳答案

findFragmentById(int id)

Finds a fragment that was identified by the given id either when inflated from XML or as the container ID when added in a transaction.

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

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

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

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) 方法查找 fragment 。

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

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

希望这对你有所帮助。

关于java - 无法使用 findFragmentById 访问我的 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20272554/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com