2015-08-25 16 views
1

我有一個SimpleAdapter,我正在使用它來顯示雙行列表視圖。我希望這個簡單的適配器上的第15個位置具有不同的格式,但是當我滾動列表視圖時,它會丟失位置和格式更改位置。有沒有解決方案?我嘗試過使用convertView,但似乎無法獲得正確的格式。我的代碼如下。謝謝!SimpleAdapter在滾動時沒有保持位置

 SimpleAdapter adapter = new SimpleAdapter(this, data, android.R.layout.simple_list_item_2, new String[] {"title", "publisher"}, new int[] {android.R.id.text1, android.R.id.text2}){ 
     public View getView(int position, View convertView, ViewGroup parent) { 
      View view = super.getView(position, convertView, parent); 

      if (position == 15) { 
       TextView text1 = (TextView) view.findViewById(android.R.id.text1); 
       text1.setTextColor(Color.WHITE); 
       TextView text2 = (TextView) view.findViewById(android.R.id.text2); 
       text2.setTextColor(Color.WHITE); 
       view.setBackgroundColor(getResources().getColor(R.color.teal_dark)); 
      } 

      return view; 
     } 


    }; 

回答

0

嘗試添加一個else子句重置格式默認格式

if (position == 15) { 
    TextView text1 = (TextView) view.findViewById(android.R.id.text1); 
    text1.setTextColor(Color.WHITE); 
    TextView text2 = (TextView) view.findViewById(android.R.id.text2); 
    text2.setTextColor(Color.WHITE); 
    view.setBackgroundColor(getResources().getColor(R.color.teal_dark)); 
} else { 
    TextView text1 = (TextView) view.findViewById(android.R.id.text1); 
    text1.setTextColor(Color.BLACK); 
    TextView text2 = (TextView) view.findViewById(android.R.id.text2); 
    text2.setTextColor(Color.BLACK); 
    view.setBackgroundColor(getResources().getColor(R.color.teal_light)); 
} 
+0

這個工作!謝謝 - 我只是不確定爲什麼...如果之前達到「位置== 15」,是否會再次達到? – apoc52

+1

實際上,android會在列表視圖中爲您的每一行回收視圖。當您第一次到達第15行時,然後滾動到另一個位置,android將爲其他行重新使用第15行,這就是爲什麼格式似乎跳轉到另一行 –

相關問題