Android在Fragment内添加Fragment

4

你好(在此提前感谢您),

我有一个应用程序,其布局类似于Google Play商店(使用PagerTabStrip和5个子片段)。在其中一些片段中,我需要显示有关上次更新数据的一些小信息。

我立即想到创建一个片段(LastUpdatedFragment),然后将其添加(嵌套)到我需要的片段中。最初,由于最后更新日期应该对每个屏幕都是相同的,事情正在起作用(我只需将该片段添加到我需要的父片段的xml中,并在onCreateView中将日期放入TextView中),但是在进行了一些更改后,我现在需要为这些LastUpdatedFragment实例发送特定日期。

我想到了一种方法-为我的Fragment创建新的自定义属性,然后我将设置所需的日期。经过一些阅读,我发现了一种更简单的方法(Fragments within Fragments -使用FragmentManager动态添加片段)-我只需要父Fragment处理输入参数并将其传递给子片段。

问题在于,尽管我没有收到任何错误,但我也没有收到任何子片段,它就是不显示。如果您能指导我正确的方向,我将不胜感激。

ScoreBoardFragment.java -> 父片段

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        rootView = inflater.inflate(R.layout.fragment_scoreboard, container,
                false);

        for (int i = 0; i < tvCount; i++) {
            tvName = "f_scoreboard_header_tv" + String.valueOf(i);
            resID = getResources().getIdentifier(tvName, "id",
                    _activity.getPackageName());
            header = (TextView) rootView.findViewById(resID);
            header.setTypeface(MyGlobalConfig.getInstance().getHeadersFont());
        }

        FragmentManager childFragMan = getChildFragmentManager();
        FragmentTransaction childFragTrans = childFragMan.beginTransaction();
        LastUpdatedFragment fragB = new LastUpdatedFragment();
        childFragTrans.add(R.id.FRAGMENT_PLACEHOLDER, fragB);
        childFragTrans.addToBackStack("B");
        childFragTrans.commit();        


        return rootView;
    }

fragment_scoreboard.xml(简化版,但显示其他所有内容)

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/FRAGMENT_PLACEHOLDER"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <LinearLayout
            style="@style/ListView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/f_scoreboard_header_tv0"
                style="@style/ListViewRowHeader"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="#" />

        </LinearLayout>

        <ListView
            android:id="@+id/f_scoreboard_lvbody"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerHorizontal="true" >
        </ListView>

    </LinearLayout>

LastUpdatedFragment.java -> 子片段

(该文涉及IT技术相关内容)
        public class LastUpdatedFragment extends Fragment{
        private View rootView;
        private TextView tv;
        private Context ctx;

        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            Thread.setDefaultUncaughtExceptionHandler(new ExceptionHandler(
                    getActivity()));
            this.ctx = getActivity().getApplicationContext();

        }

        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {

            rootView = inflater.inflate(R.layout.fragment_date, container,
                    false);
            tv = (TextView) rootView.findViewById(R.id.f_date_tv);
            tv.setTypeface(MyGlobalConfig.getInstance().getBodyFont());
            tv.setText(getString(R.string.lastupdated) + ": " + Util.getLastUpdated(ctx));
            return rootView;
        }
    }

fragment_date.xml

    <?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/f_date_tv"
            style="@style/LastUpdatedTV"
            android:layout_width="match_parent"
            android:text="Data de última actualização: 27/12/2014 21:30" />
    </LinearLayout>

FRAGMENT_PLACEHOLDER的声明在哪里? - Blackbelt
在fragment_scoreboard.xml文件中 -> 这是第一个LinearLayout的id。 - Bernardo
1
尝试使用FrameLayout作为占位符,而不是LinearLayout。 - Krish
你把ScoreBoardFragment添加到哪里了? - Blackbelt
@Krish,就这样吧!你写出你的答案,我会接受它的;)。谢谢你们两个。 - Bernardo
1个回答

4

如果要使用占位符,建议使用FrameLayout而不是LinearLayout。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接