我有几个片段。一些片段在片段的顶部和底部具有相同的按钮,但内容不同。
因此我使用嵌套片段作为顶部和buttom部分。这是我的java类: LogFragment.java 。我为顶部导航插入两个片段,在底部插入页脚。
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>
嵌套NavigationFragment
和FooterFragment
包含一些按钮,图片或文本字段。例如: 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
类访问/操作/填充嵌套片段中元素的某些数据?
例如:
我想通过logInfo
访问嵌套fragment_footer.xml
的ID为LogFragment.java
的textview。
答案 0 :(得分:3)
只需使用[child fragment].getView().findViewById(R.id.[elemnet id])
即可获取嵌套片段中的元素。例如:
@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;
}
顺便说一下,