2013-01-22 27 views
1

我有一個ListView與一行項目,其中每行包含SeekBarTextView。每當我移動SeekBar的任何一個時,我都需要在ListView中更新所有TextView的實時更新,而不會丟失對SeekBar的關注。正在更新ListView行,而不會丟失SeekBar重點

我試圖

  • 呼叫notifyDataSetChanged()ListView,但 SeekBar失去焦點。

  • 遍歷ListView用下面的代碼:

for (int i = 0; i < listView.getChildCount(); i++) 
{ 
TextView tv = (TextView) listView.getChildAt(i).findViewById(R.id.textView1); 
String value = getData(); 
tv.setText(value); 
} 

然而,上面的代碼不給持久更新到ListView,這是一個問題,如果用戶滾動。

任何建議如何處理這個問題?

回答

1

每當我移動任何的搜索欄的我需要所有的TextView的 在ListView現場更新,不會對搜索欄失去焦點。

你想要做的就是更新適配器的數據列表,而無需調用notifyDataSetChanged(),然後同時更新從當前可見行的TextViews什麼。

//... 
@Override 
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 
    // found is a reference to the ListView  
    int firstVisible = found.getFirstVisiblePosition(); 
    // first update the mData which backs the adapter    
    for (int i = 0; i < mData.size(); i++) { 
      // update update update 
    } 
    // update the visible rows 
    for (int j = 0; j < found.getChildCount(); j++) { 
      final View row = found.getChildAt(j); 
      // get the position from the mData by offseting j with the firstVisible position 
     ((TextView) row.findViewById(R.id.theIdOfTheTextView)).setText(mData.get(firstVisible + j)); 
    } 
} 
//... 

這應該爲您提供平穩的更新。

+0

謝謝!那正是我需要的。 –

+0

你讓我意識到我只是忘記更新支持適配器的數據。 –