2012-02-06 92 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,所以我想我现在去睡觉了。今天足够的工作(昨日:)

感谢所有支持