访问嵌套片段的文本字段

Access textfield of nested fragment

我有几个片段。部分片段顶部和底部的按钮相同,但内容不同。

因此我在顶部和底部使用嵌套片段。这是我的javaclass: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>

嵌套的 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 class access/manipulate/fill 我的嵌套片段中元素的某些数据?


例如:

我想通过 LogFragment.java.

的嵌套 fragment_footer.xml 访问 ID 为 logInfo 的文本视图

只需使用[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;
}

顺便说一句,

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