我目前正在嘗試編程Android Launcher
與Fragments
但我在Fragments
Views
有問題。 我有一個Dock-Fragmen
t與Dock-Controller
,它允許用戶更改片段,如應用程序菜單,設置片段等。Dock
顯示在按鈕上,Fragments
(應用程序菜單,設置片段)應顯示在上面Dock
。 問題是,應用程序菜單沒有顯示在其相關Fragment
,而是在碼頭圖標後面的Dock Fragment
,...所以我想,應用程序菜單片段在其onCreateView()
-方法中得到錯誤的視圖,但我不不明白爲什麼。片段似乎膨脹錯誤視圖
這是MainActivity
的代碼,從FragmentActivity
延伸。我將片段添加到經理。
private void addDockToManager() {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(dbConnection.getLayout(DOCK_TAG), dockController.getFragment(), DOCK_TAG);
ft.commit();
}
private void addPluginsToManager() {
FragmentManager fm = null;
FragmentTransaction ft = null;
for(String key : controllerMap.keySet()) {
fm = getSupportFragmentManager();
ft = fm.beginTransaction();
FrameController controller = null;
if ((controller = controllerMap.get(key)) != null) {
ft.add(dbConnection.getLayout(key), controller.getFragment(), key);
if (key.equals(standardFrame))
ft.addToBackStack(key);
}
ft.commit();
fm.executePendingTransactions();
}
fm = getSupportFragmentManager();
ft = fm.beginTransaction();
for(String key : controllerMap.keySet()) {
if (controllerMap.get(key) != null && !key.equals(standardFrame)) {
ft.hide(fm.findFragmentByTag(key));
}
}
ft.commit();
}
的佈局目前在dbConnection
硬編碼:
public int getLayout(String name) {
int layout = -1;
switch(name) {
case "app_menu" : layout = R.id.fl_app_menu;
case "settings" : layout = R.id.fl_settings;
case "dock" : layout = R.id.fl_dock;
}
return layout;
}
的MainActivity's
XML看起來像:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rl_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.activity.MainActivity"
tools:ignore="MergeRootFrame" >
<FrameLayout
android:id="@+id/fl_settings"
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_above="@+id/fl_dock"
android:background="#00ffffff" >
</FrameLayout>
<FrameLayout
android:id="@+id/fl_app_menu"
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_above="@+id/fl_dock"
android:background="#00ffffff" >
</FrameLayout>
<FrameLayout
android:id="@+id/fl_dock"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" >
</FrameLayout>
</RelativeLayout>
應用菜單中的XML是gridview
,看起來像即:
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gv_apps"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="6"
android:gravity="center"
android:columnWidth="50dp"
android:stretchMode="columnWidth" >
</GridView>
的App Fragment
看起來像這樣:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup group,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.external_apps, group, false);
layout = (GridView) view.findViewById(R.id.gv_apps);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
GridViewAdapter gridViewAdapter = new GridViewAdapter((AppMenuController) myController, apps);
((GridView) layout).setAdapter(gridViewAdapter);
}
而且GridViewAdapter
的getView
方法:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(controller.getMainActivity().getApplicationContext());
imageView.setImageDrawable(buttons.get(position).getIcon());
imageView.setLayoutParams(new GridView.LayoutParams(65, 65));
return imageView;
}
我希望我提到的是足以解決問題。我正在網上搜索幾個小時,但我找不到解決方案。
謝謝! 3年研究和切換案例的問題,... – user3762869
哈哈,沒問題:P它也發生在我身上,這就是爲什麼我寧願使用if-elseif-else結構,即使人們說開關是「漂亮」: d – EpicPandaForce