2012-07-30 14 views
0

我有一個使用FrameLayout與屏幕分割成左側導航和右側的內容面板主要活動。左側面板用於導航,將不同的佈局加載到右側面板(稱爲contentLayout),該面板可以粘貼多個佈局。我在onItemClickListener s的左側導航面板中有ListView。在他們分開onItemClick方法,我用contentLayout.removeAllViews()然後誇大他們的佈局,像這樣:一個ListView控件,第二佈局不更新

private final OnItemClickListener redListener = new OnItemClickListener() { 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
     contentLayout.removeAllViews(); 
     if (myFirstClassLayout == null) { 
      myFirstClassLayout = FirstClassLayout.getLayout(
           layoutInflater, 
           MyActivity.this, 
           resources, 
           new ArrayList<String>(), 
           new ArrayList<Desc>() 
          ); 
     } 
     CommonClass.updateListeners(); 
     if (FirstClassLayout != null) contentLayout.addView(myFirstClassLayout); 
    } 
}; 

private final OnItemClickListener blueListener = new OnItemClickListener() { 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
     contentLayout.removeAllViews(); 
     if (mySecondClassLayout== null) { 
      mySecondClassLayout = SecondClassLayout.getLayout(
           layoutInflater, 
           MyActivity.this, 
           resources, 
           new ArrayList<String>(), 
           textList, 
           new ArrayList<Desc>(), 
           hash 
          ); 
     } 
     CommonClass.updateListeners(); 
     if (SecondClassLayout != null) contentLayout.addView(mySecondClassLayout); 
    } 
}; 

在這兩個FirstClassLayoutSecondClassLayout,我吹一個ThirdClassLayout包含動態ListView(姑且稱之爲commonListView),使得使用的一個ArrayAdapter<String>ListView是一個listChoiceIndicatorSingle樣式視圖,這意味着它在每個列表項右側顯示一個類似單選按鈕的圖標。

當應用程序啓動時,正確的內容面板爲空。如果我觸發了redListenerblueListener,它將在右側的內容面板中顯示commonListView,但未選擇任何列表項目。如果我先觸發紅色,然後觸發藍色,然後在commonListView中進行選擇,則更改會反射回FirstClassLayout,這太棒了!然而,當我繼續通過選擇commonListView從FirstClassLayout別的東西,然後觸發blueListenerSecondClassLayout,在SecondClassLayoutcommonListView並不反映我的選擇。相反,當我最後一次使用SecondClassLayout時,它卡在我選擇的項目上。

出現這種情況,如果我執行相反的順序測試,所以根據我上次觸發該聽衆,去年聽衆和佈局將有超過兩種佈局適當的UI synching控制。我首先觸發的那個似乎只能控制自己。我需要兩個聽衆能夠在任何給定時間在兩個佈局上顯示完全相同的ListView選定項目。

commonListView從去年佈局先佈局synching通過CommonClass.updateListeners()實現其作用:

adapter.clear(); 
for (String title: statusSet) adapter.add(title); 
adapter.notifyDataSetChanged(); 
commonListView.setItemChecked(positionOfItemChecked, true); 
Log.i(TAG, "position checked = " + positionOfItemChecked); 
adapter.notifyDataSetChanged(); 

由於這一工程從去年佈局觸發首先,我想這必須與一個UI刷新問題,但我到處搜尋了一個很好的解決方案並嘗試了所有建議。 0123'語句在LogCat中爲第一個或最後一個佈局觸發正確輸出,所以我知道它正確地打上了代碼。我只是不明白爲什麼第一個無法控制最後一個。我已經試過:

commonListView.invalidate(); 
((BaseAdapter) commonListView.getAdapter()).notifyDataSetChanged(); 

AsyncTask和其他幾個人,但似乎沒有什麼上刷新的第一個佈局,使其第二匹配工作。有沒有人遇到過這樣的問題?有誰知道如何解決或解決這個問題?

回答

0

我想清楚是什麼導致第二人佔據第一。我繼承了很多這樣的代碼,所以我不熟悉所有的部分。涉及其他幾件。我終於找到了原始作者使用實現自定義偵聽器的自定義AdapterWrapper的代碼部分。 ThirdClassLayout只通過檢查靜態ArrayAdapter是否爲null來創建一個新的ArrayAdapter和一個新的AdapterWrapper。刪除此檢查允許兩個適配器對象都爲這兩個佈局創建新的。隨着他們自己創建的監聽器,兩個佈局現在都會響應。

0

看看contentLayout.requestLayout()將意見解決你的問題了。

+0

我剛剛在contentLayout.addView裏面加了同樣的if塊。在if區塊外面試過它。第二個佈局的問題仍然不清晰。 – jdynamite 2012-07-31 14:09:59

相關問題