0

我會盡量保持它的簡短和重點。使用FragmentTabsPager兼容性庫示例時,我會在選項卡內替換哪些內容?

觀察下面的截圖: enter image description here

如何實現的方式是,當我按下這個ListView的一個項目,該片段被替換爲與相應的播放器數據的新片段?我應該替換FrameLayout tabcontent的內容嗎?如果是這樣,我如何確保ViewPager仍然保持功能?

「基本活動」 的XML:

<TabHost 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@android:id/tabhost" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<LinearLayout 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TabWidget 
     android:id="@android:id/tabs" 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0"/> 

    <FrameLayout 
     android:id="@android:id/tabcontent" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_weight="0"/> 

    <android.support.v4.view.ViewPager 
     android:id="@+id/pager" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1"/> 

</LinearLayout> 
</TabHost> 

這是onActivityCreated:

@Override 
    public void onActivityCreated(Bundle savedInstance) 
    { 
     super.onActivityCreated(savedInstance); 
     setHasOptionsMenu(true); 


     listView = (ListView)getActivity().findViewById(R.id.lv_federations); 
     listView.setVisibility(View.GONE); 
     if (savedInstance == null) 
     { 
      new PullFederationDataTask().execute(); 

     }  

     listView.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> arg0, View arg1, 
        int position, long arg3) 
      { 
       // Replace Fragment 
      } 
     }); 
    } 

我該怎麼辦?

回答

0

我不確定在代碼中更換onItemClickListener中的片段,因爲我正在努力解決同樣的問題,還沒有找到合適的解決方案。

如果實現通過擴展ListFragment您的播放器列表中,您可以通過使用fragment transactions顯示播放器的詳細信息的新片段替換當前片段(或者你可以開始一個新的活動,但我將討論片段交易在這個答案中)。

要更換片段,您可以創建在onListItemClick方法來獲取點擊的列表項的ID作爲參數,片斷的構造一個新的片段:

@Override 
public void onListItemClick (ListView l, View v, int position, long id) { 

    // Here I assume that the fragment containing the player details 
    // is called PlayerDetailFragment. 
    // The id of the item that was clicked is passed to the fragment's constructor. 
    Fragment newFragment = new PlayerDetailFragment(id); 
    FragmentTransaction ft = getFragmentManager().beginTransaction(); 

    // Replace whatever is in the fragment_container view with this fragment, 
    // and add the transaction to the back stack. 
    // android.R.id.content refers to the id of the root ViewGroup. 
    ft.replace(android.R.id.content, newFragment); 
    ft.addToBackStack(null); 

    // Commit the transaction 
    ft.commit();  
} 

可以使用ListAdapter的getItemId()方法請參考玩家的ID。使用ListView.setAdapter()方法使用您自己的ListAdapter作爲ListViewListAdapter負責維護支持此列表的數據並生成一個視圖來表示該數據集中的項目。 This教程介紹瞭如何編寫自己的ArrayAdapter以獲得ListView

在您的ListAdapter中,您覆蓋getItemId()方法並返回播放器的ID。然後PlayerDetailFragment可以使用id從數據庫中檢索數據。

我沒有使用ViewPagers的經驗,所以我無法幫助。

相關問題