介紹 我想根據用戶的選擇從導航抽屜有不同的片段。對於導航抽屜中的每個項目都應該調用一個不同的站點。我想通過片段實現這一點。 主要片段將獲得通過執行以下操作在OnCreate法添加布局到一個片段programmaticcaly
if (savedInstanceState == null) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
Fragment fragment = MainFragment.passList(hashMap);
ft.replace(R.id.content_frame, new MainFragment());
ft.commit();
}
正如你可以看到我傳遞數據(一個HashMap),其完美的作品片段的方法首先顯示。數據然後被放入字符串數組中。這可以工作,數據也可以在片段的onCreate方法中使用。
這是主要的活動
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/darker_gray"
android:dividerHeight="0.1dp" />
</android.support.v4.widget.DrawerLayout>
的XML和這是主要的片段
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainRelativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/activatedBackgroundIndicator"
android:gravity="center_vertical"
android:paddingRight="10dp"
>
<ScrollView
android:id="@+id/mainScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.03" >
<LinearLayout
android:id="@+id/mainListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
</RelativeLayout>
我想現在實現的是在滾動視圖中顯示的數據的XML。但我有這個問題,我該如何填充它,以及如何初始化它?
通常我會去,只是他們創造這樣的 - 因爲findViewById不片段工作(有我自己寫的方法)
ScrollView sv = new ScrollView(getActivity());
LinearLayout ll = new LinearLayout (getActivity());
sv.addView(ll);
ll.addView(SAMPLETEXTVIEW);
View mainView = inflater
.inflate(R.layout.main_fragment, container, false);
return mainView;
但只是給我留下了一個空白屏幕並沒有添加實際的文字。我怎樣才能做到這一點?數據在那裏 - 我知道,因爲它被打印出來與System.out.println - 我只需要一種方式來顯示它。 Thx提前!
編輯
當我那樣做
View rootView = inflater.inflate(R.layout.main_fragment, container, false);
ScrollView sv = (ScrollView) rootView.findViewById(R.id.mainScrollView);
LinearLayout ll = (LinearLayout) rootView.findViewById(R.id.mainListView);
sv.addView(ll);
我得到的只是一個IllegalStateException是滾動型只能承載一個直接的孩子......但我不明白這一點。線性佈局是唯一直接的孩子。爲什麼會有問題?
下面是完整的onCreateView
- 方法
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.main_fragment, container, false);
ScrollView sv = (ScrollView) rootView.findViewById(R.id.mainScrollView);
LinearLayout ll = (LinearLayout) rootView.findViewById(R.id.mainListView);
sv.addView(ll);
source = new TextView[hashMapSize];
for (int i = 0; i < hashMapSize; i++) {
source[i] = new TextView(getActivity());
source [i].setText("Source: " + sourceArr [i]);
ll.addView(source[i]
}
return rootView;
}
哦,我的上帝,這麼簡單....謝謝! – Musterknabe
有時候只需要一雙新鮮的眼睛:) – linakis