您好(提前致谢)
我有一个具有类似 Google Play 商店布局的应用程序(使用带有 5 个子 fragment 的 PagerTabStrip)。在其中一些 fragment 中,我需要显示一些关于上次数据更新时间的信息。
我立即想到创建一个 fragment (LastUpdatedFragment),然后将其添加(嵌套)到我需要的 fragment 中。起初,由于每个屏幕的最后更新日期应该是相同的,所以一切正常(我只是将 fragment 添加到我需要的父 fragment 的 xml 中,并在 onCreateView 中将日期放在 TextView 中),但经过一些更改后,我现在需要为这些 LastUpdatedFragment 实例中的每一个实例发送一个特定日期。
我想到了一种方法 - 为我的 fragment 创建新的自定义属性,然后我会设置我想要的日期。阅读一些内容后,我偶然发现了一种更简单的方法(Fragments within Fragments - 使用 FragmentManager 动态添加 fragment )- 我只需要父 fragment 来处理输入参数并将其传递给子 fragment 。
问题是,虽然我没有收到任何错误,也没有收到任何子 fragment ,但它只是不显示。如果您能指导我正确的方向,我将不胜感激。
ScoreBoardFragment.java -> 父 fragment
@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 -> 子 fragment
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>
最佳答案
尝试使用 FrameLayout 而不是 LinearLayout 作为占位符。
关于Android 在 Fragment 中添加 Fragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27803545/