gpt4 book ai didi

java - 访问嵌套 fragment 的文本字段

转载 作者:搜寻专家 更新时间:2023-11-01 08:43:39 25 4
gpt4 key购买 nike

我有几个 fragment 。有些 fragment 在 fragment 的顶部和底部具有相同的按钮,但内容不同。

因此我在顶部和底部使用嵌套 fragment 。这是我的 java 类:LogFragment.java。我为顶部导航和底部的页脚插入了两个 fragment 。

public class LogFragment extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootview = inflater.inflate(R.layout.fragment_log, container, false);

Fragment navigationFrag = new NavigationFrag();
FragmentTransaction trans1 = getChildFragmentManager().beginTransaction();
trans1.add(R.id.navigationFrag, navigationFrag).commit();

Fragment footerFragment = new FooterFragment();
FragmentTransaction trans2 = getChildFragmentManager().beginTransaction();
trans2.add(R.id.footerFragment, footerFragment).commit();

return rootview;
}
}

这是布局文件:fragment_log.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<FrameLayout android:id="@+id/NavigationFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"/>

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop"
android:layout_below="@+id/button1"/>

</RelativeLayout>

<FrameLayout android:id="@+id/FooterFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>

嵌套的 NavigationFragmentFooterFragment 包含一些按钮、图像或文本字段。例如:fragment_footer.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/logInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LOG" />

</RelativeLayout>

我的问题:

如何通过 LogFragment.java 类访问/操作/填充嵌套 fragment 中元素的一些数据?


例如:

我想通过 LogFragment.java 访问嵌套 fragment_footer.xml 的 id logInfo 的 TextView 。

最佳答案

只需使用 [child fragment].getView().findViewById(R.id.[elemnet id]) 获取嵌套 fragment 中的元素。例如:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootview = inflater.inflate(R.layout.fragment_log, container, false);

Fragment navigationFrag = new NavigationFrag();
FragmentTransaction trans1 = getChildFragmentManager().beginTransaction();
trans1.add(R.id.navigationFrag, navigationFrag).commit();

final Fragment footerFragment = new FooterFragment();
FragmentTransaction trans2 = getChildFragmentManager().beginTransaction();
trans2.add(R.id.footerFragment, footerFragment).commit();

Button btn1 = (Button)rootview.findViewById(R.id.button1);
btn1.setOnClickListener( new OnClickListener(){
@Override
public void onClick(View v) {
TextView txv = (TextView)footerFragment.getView().findViewById(R.id.logInfo);
txv.setText("Updated log of Footer");
}
});

return rootview;
}

顺便说一下,

  1. 只有在 onCreateView() 之后才能访问子 fragment ,
  2. 因此您需要将子 fragment 声明为父 fragment 的最终数据或成员数据。

关于java - 访问嵌套 fragment 的文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30434789/

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