2012-02-06 41 views
0

我目前正在研究一個ListView,其中包含大約80到100個項目(TextViews)。我不認爲這是太多的內容,但當我滾動(用手指)ListView是浮動或laging。但是,當我使用「快速滾動按鈕」 - ListView右側的那個東西時 - 滾動看起來非常一致和平滑。爲什麼ListView不一致滾動?

有沒有人有同樣的問題?我在我的HTC Sensation上測試了ListView

這裏是我的ListView代碼:

<ListView 
    android:id="@+id/list_view" 
    android:layout_height="match_parent" 
    android:layout_width="match_parent" 
    android:scrollingCache="true"> 

</ListView> 

和Java代碼:

adptr = new ArrayAdapter<String>(iF, R.layout.list_item, showing) { 

     @Override 
     public View getView(int position, View convertView, ViewGroup grp) { 

      LinearLayout lin = new LinearLayout(this.getContext()); 
      lin.setOrientation(LinearLayout.HORIZONTAL); 

      // Icon 
      ImageView v = new ImageView(this.getContext()); 
      // v.setBackgroundDrawable(iF.getResources().getDrawable(R.drawable.cube_icon)); 

      // Text 
      TextView txt = new TextView(this.getContext()); 
      txt.setTextSize(Float.valueOf(prefs.getString("pref_txtSize", "12"))); 
      txt.setPadding(10, 10, 10, 10); 
      txt.setText(this.getItem(position)); 
      txt.setTextColor(getLineColor(position)); 

      // Shortcut 
      LinearLayout shortLin = new LinearLayout(this.getContext()); 
      shortLin.setGravity(Gravity.RIGHT); 

      LayoutParams par = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 
      shortLin.setLayoutParams(par); 

      TextView s = new TextView(this.getContext()); 
      s.setTextSize(Float.valueOf(prefs.getString("pref_txtSize", "12"))); 
      s.setWidth(iF.getResources().getDimensionPixelSize(R.dimen.shortcutWidth)); 
      s.setPadding(10, 10, 10, 10); 

      s.setText(getShortcut(position)); 

      shortLin.addView(s); 

      // Return 
      txt.invalidate(); 
      v.invalidate(); 
      s.invalidate(); 

      lin.addView(v); 
      lin.addView(txt); 
      lin.addView(shortLin); 

      return lin; 
     } 
    }; 

正如你所看到的,我做了一個自定義的ListViewArrayAdapter將以不同的方法添加(此處未顯示)。

在此先感謝

阿德里安

+0

不知道,如果你要在你的getView() – 2012-02-06 16:30:31

+0

是無效的textviews所有的時間。這可能不是杞人憂天。我真的不知道它做了什麼。發現這在一些谷歌研究:) – aeduG 2012-02-06 16:43:50

回答

4

你還記得大約緩存?

public View getView(int position, View convertView, ViewGroup parent) { 

    View view = convertView; 
    ViewHolder holder; 

    if (view == null) { 

     LayoutInflater inflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     view = inflater 
       .inflate(R.layout.list_view_home_item, parent, false); 

     holder = new ViewHolder(); 
     holder.title = (TextView) view 
       .findViewById(R.id.textView); 

     holder.title.setText("blah"); 
     view.setTag(holder);    
    } else {   
     holder = (ViewHolder) view.getTag(); 
    } 

    return view; 

static class ViewHolder { 
    TextView title; 
} 

+0

謝謝你的答案。我從來沒有聽說過關於LayoutInflater。我將去android的開發頁面,並閱讀更多關於這一點。 – aeduG 2012-02-06 16:39:02

+0

要添加到此答案,ListView上的Google IO事件的一個很好的截屏會是一件好事。它幫助我http://www.youtube.com/watch?v=wDBM6wVEO70 – hooked82 2012-02-06 16:44:18

+0

請記住檢查本網站上的好答案! ;-) 昨天我得到了同樣的問題 – 2012-02-06 16:46:36

2

您每次拉取視圖時都會動態地爲列表創建每個View元素。這可能是迄今爲止效率最低的機制:)。

作爲第一步,看看您是否可以用XML佈局視圖。使事情更容易管理。

即使您不這樣做,請使用convertView參數。它存在的唯一原因是如果可能的話,讓你不必重新分配視圖。如果convertView非空,則它具有在之前調用getView()時創建的所有視圖。您所要做的就是在每個視圖中填寫適當的信息(基本上,您的情況是setText()調用)。如果convertView爲空,請創建您的視圖。另外,不要invalidate()。至少,沒有XML佈局,這是一個重寫版本,應該快一點。

@Override 
public View getView(int position, View convertView, ViewGroup grp) { 
    if(convertView == null) { 
     LinearLayout lin = new LinearLayout(this.getContext()); 
     lin.setOrientation(LinearLayout.HORIZONTAL); 

     // Icon 
     ImageView v = new ImageView(this.getContext()); 
     // v.setBackgroundDrawable(iF.getResources().getDrawable(R.drawable.cube_icon)); 

     // Text 
     TextView txt = new TextView(this.getContext()); 
     txt.setId(1); 
     txt.setTextSize(Float.valueOf(prefs.getString("pref_txtSize", "12"))); 
     txt.setPadding(10, 10, 10, 10); 
     txt.setTextColor(getLineColor(position)); 

     // Shortcut 
     LinearLayout shortLin = new LinearLayout(this.getContext()); 
     shortLin.setGravity(Gravity.RIGHT); 

     LayoutParams par = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 
     shortLin.setLayoutParams(par); 

     TextView s = new TextView(this.getContext()); 
     s.setId(2); 
     s.setTextSize(Float.valueOf(prefs.getString("pref_txtSize", "12"))); 
     s.setWidth(iF.getResources().getDimensionPixelSize(R.dimen.shortcutWidth)); 
     s.setPadding(10, 10, 10, 10); 

     shortLin.addView(s); 
     lin.addView(v); 
     lin.addView(txt); 
     lin.addView(shortLin); 
    } 

    TextView txt = (TextView)convertView.findViewById(1); 
    txt.setText(this.getItem(position)); 
    TextView txt = (TextView)convertView.findViewById(2); 
    txt.setText(getShortcut(position)); 


    return lin; 
} 

再次,不是最好的方法或最佳實踐,但這應該工作。

+0

非常感謝。我會盡快測試這些代碼。但是在這個代碼中,沒有像Damian推薦的那樣膨脹。但是,感謝您的支持 – aeduG 2012-02-06 17:00:24

0

最後,我想出了這個解決方案:

@Override 
     public View getView(int position, View convertView, ViewGroup parent) { 

      View view = convertView; 
      ViewHolder holder; 

      if (view == null) { 

       LayoutInflater inflater = (LayoutInflater) iF.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       view = inflater.inflate(R.layout.list_item, parent, false); 

       holder = new ViewHolder(); 
       holder.title = (TextView) view.findViewById(R.id.item_text); 
       holder.shortcut = (TextView) view.findViewById(R.id.item_short); 

       holder.title.setId(1); 
       holder.shortcut.setId(2); 

       holder.title.setText(getItem((position))); 
       holder.shortcut.setText(getShortcut(position)); 

       holder.title.setTextSize(Float.valueOf(prefs.getString("pref_txtSize", "12"))); 
       holder.shortcut.setTextSize(Float.valueOf(prefs.getString("prefs_txtSize", "12"))); 

       view.setTag(holder); 
      } else { 
       holder = (ViewHolder) view.getTag(); 

       TextView title = (TextView) convertView.findViewById(1); 
       title.setText(getItem(position)); 

       TextView shortcut = (TextView) convertView.findViewById(2); 
       shortcut.setText(getShortcut(position)); 
      } 

      return view; 
     } 

我現在它的工作真的很開心。現在是00:33,所以我想我現在去睡覺了。今天足夠的工作(昨日:)

感謝所有支持