0

我有一個簡單的android應用程序,它是動態創建列表並添加到FragmentManager。列表類是指包含合併佈局的佈局項目。整個應用程序正在使用SherlockFragmentActivity。如何在Android現有的應用程序添加一個簡單的頁面

現在我想添加一個永久頁腳到現有的FragmentManager而不會影響當前的功能。

這個頁腳應該可用的所有頁面。

任何人都可以請幫我這麼做嗎?

這裏是在Java代碼:

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    FragmentManager fm = getSupportFragmentManager(); 
    // Create the list fragment and add it to sole content. 

    if (fm.findFragmentById(android.R.id.content) == null) { 
     ClockListFragment list = new ClockListFragment(); 
     fm.beginTransaction().add(android.R.id.content, list).commit(); 

     //adding the list to the content 
    } 
} 

這裏ClockListFragment是被添加的內容類:

public static class ClockListFragment extends SherlockListFragment implements 
LoaderManager.LoaderCallbacks<Cursor>, PauseSource { 

    variablesdeclarations....... 

    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
     setEmptyText(getText(R.string.no_clock_defined)); 
     setHasOptionsMenu(true); 

     mAdapter = new ClockCursorAdapter(getActivity(), R.layout.world_clock_item, null, this); 
     setListAdapter(mAdapter); 

     setListShown(false); 

     ListView listView = getListView(); 
     setupCabOld(listView); 
     registerPreferenceChanged(); 

     if (savedInstanceState != null) { 
      // Restore contextual action bar state 
      CharSequence cab = savedInstanceState.getCharSequence(CAB); 
      if (cab != null) { 
       mMode = getSherlockActivity().startActionMode(new ModeCallback()); 
       mMode.setTitle(cab); 
       mMode.invalidate(); 
      } 
     } 

     getLoaderManager().initLoader(0, null, this); 
     Clocks.updateOrder(getActivity()); 
     updateWeather(false); 
    } 

    ........ other codes 
} 
+0

您是否嘗試過用'addFooterView()的['ListView'類]的'方法(http://developer.android.com/reference/android/widget/ListView。 HTML)? –

+0

我有兩個或兩個以上的活動是項目。上面的代碼是項目的開始活動。我們在哪裏必須使用 –

回答

0

我有我的活動(S)使用基地佈局XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

     <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:id="@+id/content_pane" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_above="@+id/footer_layout"> 

    </FrameLayout> 

    <include layout="@layout/footer"/> 
</RelativeLayout> 

然後你可以添加/替換你的片段到@ + id/content_pane(framlayout)。當然你需要一個頁腳佈局。

要在您的例子中使用:

  1. 添加的setContentView(R.layout.base);到你的活動onCreate-method。
  2. 變化fm.beginTransaction().add(android.R.id.content, list).commit();fm.beginTransaction().add(R.id.content_pane, list).commit();
+0

感謝您的回答。我不確定該項目是否具有基礎佈局。該項目已預定義佈局。 <?xml version =「1.0」encoding =「utf-8」?> <包括佈局= 「@佈局/ clock_item_merge」/> 如何在這裏使用。 –

+0

添加代碼setContentView(R.layout.base_layout)後; \t ClockListFragment list = new ClockListFragment(); \t fm.beginTransaction()。add(R.id.content_pane,list).commit();但我的主要時鐘沒有顯示。我的頁腳正在顯示。 –

相關問題