ユーザー入力に基づいて、あるアプリケーションから別のListFragment
アプリケーションに移行する基本的なアプリケーションを構築しようとしています。ListFragment
ListView
私はAndroidシステムが膨らむデフォルトを利用しているListFragment
ので、オーバーライドしませんonCreateView()
。
の周囲のマージンを設定するためにListFragment
、を追加しGlobalLayoutListener
ます。
これで、アプリケーションを起動すると、デフォルトのフラグメントを含む最初の画面が正しく表示され、マージンが正しく設定されています。
しかし、メインレイアウトの画像をクリックするとすぐに、最初のフラグメントと同じonActivityCreated()
メソッドを持つ2番目のフラグメントへの遷移が呼び出され、2番目のフラグメントのにアクセスしようとすると、メソッドでGlobalLayoutListener
恐ろしいContent View not created yet
エラーが発生します。 。OnGlobalLayout()
ListView
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();