3

我有一個自定義佈局的ListFragment。我想要做的是通過使用ViewFlipper動畫在相同的佈局中切換我的ListView。這實際上工作,但因爲我使用ArrayAdapter來填充兩個ListViews,所以setListAdapter()似乎並沒有刷新我的第二個ListView,即使我在調用showNext()後也使用它。我如何告訴我的ListFragment我已經改變了視圖?它甚至有可能嗎?如何在使用ViewFlipper的showNext()後更改ListFragment的ListView?

這是我的自定義佈局,由於我也使用自定義ListView的,我必須使用android:id =「@ id/android:list」來列出這兩個列表。

<LinearLayout> 
     <TextView/> 

     <ListView 
      android:id="@id/android:list" 
      android:layout_width="match_parent" 
      android:layout_height="0dip" 
      android:layout_weight="1" 
      android:drawSelectorOnTop="false"/>   
    </LinearLayout> 

    <LinearLayout>  
     <TextView/> 

     <ListView 
      android:id="@id/android:list" 
      android:layout_width="match_parent" 
      android:layout_height="0dip" 
      android:layout_weight="1" 
      android:drawSelectorOnTop="false"/>   
    </LinearLayout> 

</ViewFlipper> 

這是我的代碼部分:

headerFlipper = (ViewFlipper)getActivity().findViewById(R.id.viewFlipper); 
headerFlipper.showNext(); 

// at this point a simple getListView() returns the same first ListView object. 

// this changes the first ListView (not visible anymore) 
// how to change the second one? 
setListAdapter(new ArrayAdapter<DummyContent.DummyMessage>(getActivity(), 
     android.R.layout.simple_list_item_activated_1, 
     android.R.id.text1, DummyContent.MESSAGES)); 

有什麼建議? 在此先感謝。

回答

2

您的viewflipper中有兩個具有相同ID的項目,因此該活動僅使用一個。要這樣做,您需要將當前的ListActivity更改爲FragmentActivity,然後創建兩個單獨的ListFragment活動來處理每個列表。

請記住,這些類名中的某些可能會根據您使用的支持庫而有所不同,但總體而言,它們之間幾乎沒有差異。

對於佈局,只需將片段的兩個LinearLayouts放置在它們自己的xml文件中,並將它們包含在viewFlipper xml中。

viewflipper.xml

<?xml version="1.0" encoding="utf-8"?> 
<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/viewFlipper" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <include 
     android:id="@+id/fragListOne" 
     layout="@layout/listfragmentone" /> 

    <include 
     android:id="@+id/fragListTwo" 
     layout="@layout/listfragmenttwo" /> 

</ViewFlipper> 

那麼你可以使用像每一個片段:

listfragmentone & listfragmenttwo

<?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:paddingLeft="8dp" 
    android:paddingRight="8dp"> 

    <ListView android:id="@+id/android:list" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="#00FF00" 
      android:layout_weight="1" 
      android:drawSelectorOnTop="false"/> 

    <TextView android:id="@id/android:empty" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="#FF0000" 
      android:text="No data"/> 
</LinearLayout> 
明智

代碼,你將有座標數據的主要活動和一切,以及處理每個listview separ的兩個listview片段ately。主要活動還需要跟蹤,其中一個是可見的,等

主要業務 - 擴展FragmentActivity

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

    setContentView(R.id.viewFlipper); 

    // use fragment manager to see which fragments are instantiated 
    // and available, then reuse any available in case of 
    // orientation change 

    FragmentManager.enableDebugLogging(true) 
    FragmentManager fragManager = getSupportFragmentManager(); 
    ListFragmentOne fragOne = fragManager.findFragmentById("frag one tag"); 

    // if fragOne is null add a new instance via an add transaction 
    // etc. 
} 

片段活動,延長ListFragment,應該是類似於正常列表活動除了你」將使用onCreateView而不是onCreate

相關問題