【发布时间】:2012-03-26 12:36:46
【问题描述】:
我正在尝试构建一个基于ListFragment 的基本应用程序,该应用程序根据用户输入从一个ListFragment 转换到另一个。
我使用 Android 系统为 ListFragment 扩展的默认 ListView,因此我不会覆盖 onCreateView()。
要设置ListFragment 周围的边距,我添加了GlobalLayoutListener。
现在,当我启动应用程序时,包含默认片段的第一个屏幕正确显示,并且边距设置正确。
但是,当我单击主布局中的图像时,它会调用到第二个片段的转换,该片段具有与第一个片段相同的onActivityCreated() 方法和GlobalLayoutListener,我得到了可怕的Content View not created yet 错误当我尝试访问第二个片段中的ListView 时的OnGlobalLayout() 方法。
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(getActivity(),
R.layout.listcommon, R.id.label, MAIN_TITLES));
ListView lv = getListView();
lv.setDivider(null);
lv.setDividerHeight(0);
lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
lv.setItemChecked(0, true);
lv.setSelection(0);
ViewTreeObserver observer = getListView().getViewTreeObserver();
observer.addOnGlobalLayoutListener(fragmentLayoutListener);
}
ViewTreeObserver.OnGlobalLayoutListener fragmentLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener(){
public void onGlobalLayout() {
//Crashes here for second fragment
ListView listView = getListView();
FrameLayout.LayoutParams params = (LayoutParams) listView.getLayoutParams();
params.setMargins(10, 10, 10, 10);
}
};
这是主要布局:(默认片段通过FragmentTransaction.add() 添加到第一个FrameLayout)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:background="#00000000">
<FrameLayout
android:id="@+id/TitlesContainer"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="40"
android:layout_margin="10dip"
android:background="@drawable/titlesbg"
>
</FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="60"
android:background="#00000000"
>
<ImageView
android:id="@+id/imageView_clickWheel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/clickImage" />
</FrameLayout>
</LinearLayout>
对我应该做些什么来避免遇到这个错误有什么想法吗?
我是否应该覆盖 onCreateView() 并扩充自定义列表视图(但我没有这种需要)?
编辑:
以下是我如何将默认片段添加到主要活动的onCreate():
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
TitlesFragment fragment = (TitlesFragment) fragmentManager.findFragmentByTag(MAIN_SCREEN_TAG);
if (fragment == null){
fragment = TitlesFragment.newInstance(SCREEN_MAIN);
fragmentTransaction.add(R.id.TitlesContainer, fragment, MAIN_SCREEN_TAG);
} else {
fragmentTransaction.add(R.id.TitlesContainer, fragment);
}
fragmentTransaction.commit();
要执行转换,我会这样做:
fragment = (TitlesFragment) fragmentManager.findFragmentByTag(SECOND_FRAGMENT_TAG);
if (fragment == null){
fragment = TitlesFragment.newInstance(SECOND_FRAGMENT);
fragmentTransaction.replace(R.id.TitlesContainer, fragment, SECOND_FRAGMENT_TAG);
} else {
fragmentTransaction.replace(R.id.TitlesContainer, fragment);
}
fragmentTransaction.commit();
【问题讨论】:
-
你能显示你在布局中添加片段的代码吗?我认为问题可能就在那里。
-
我添加了显示片段添加/替换的代码。你能解释一下为什么这里默认的膨胀视图(我想为 ListFragment 的 android.R.layout.list_content)是不够的吗?另外我有点疑惑,getListView() 怎么不会在 onActivityCreated() 中让应用崩溃?
-
默认视图应该没问题,对不起,如果我的回答让您感到困惑,因为它使用了自定义布局。有关您上一个问题的进一步解释,请参阅下面我修改后的答案。
-
很抱歉,我没有看到对以下答案的修改。 :)
-
没有时间尝试为您寻找更好的答案。如果你到那时还没有整理好,我明天会尝试回到它。不过,在 onCreateView 中扩展您的列表布局,我认为您会没事的。
标签: android android-listfragment