Listview慢滚动

时间:2015-02-10 19:30:20

标签: java android listview scroll android-viewholder

我正在使用ListView,它的每个项目都包含一些控件。当我向下滚动这个Listview时,它很慢。我知道ViewHolder模式用于解决这个问题,我实现了这种模式。但它并没有解决我的问题。

我的xml listview项目是

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

   <RelativeLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"        
        android:orientation="horizontal" >

       <LinearLayout 
            android:layout_width="match_parent"
            android:layout_height="match_parent"            
            android:orientation="vertical" >            
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="5dp"    
                android:id="@+id/artistText" />
            <LinearLayout 
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginLeft="5dp"    
                android:orientation="horizontal" >          
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="10dp"   
                    android:id="@+id/titleText" />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="10dp"   
                    android:id="@+id/durationText" />    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" 
                    android:text="@string/synchedSongCaption"       
                    android:id="@+id/synchedText" />
            </LinearLayout>         
        </LinearLayout>  

        <CheckBox
            android:layout_alignParentRight="true"
            android:layout_marginRight="60dp"
            android:id="@+id/toDownloadSong"
            android:layout_width="wrap_content"         
            android:layout_height="wrap_content" /> 
        <Button 
            android:layout_alignParentRight="true"
            android:id="@+id/contextMenuButton"        
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </RelativeLayout>

    <LinearLayout        
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <Button 
            android:id="@+id/cancelButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/cancelCaption"
             />
        <Button 
            android:id="@+id/pauseButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/pauseCaption" />
        <Button 
            android:id="@+id/resumeButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"            
            android:text="@string/resumeCaption" />  

     </LinearLayout>
</LinearLayout>

我的代码是

public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    ViewHolder holder;
    if (view == null) {
        view = lInflater.inflate(R.layout.internet_audio_view, parent, false);  
        holder = getViewHolder(view);
        view.setTag(holder);
    } else {
        holder = (ViewHolder)view.getTag();
    }
    holder.position = position;             
    Audio song = getModel(position);

    view.setOnClickListener(songItemClickListener);

    holder.titleText.setText(StringHelper.reduceText(song.title));
    holder.artistText.setText(StringHelper.reduceText(song.artist));
    holder.durationText.setText(FormatHelper.secondsToTimeStringFormat(song.duration));   


    return view;
  }

private ViewHolder getViewHolder(View view) {
    ViewHolder holder = new ViewHolder();       
    holder.artistText = (TextView) view.findViewById(R.id.artistText);
    holder.titleText = (TextView) view.findViewById(R.id.titleText);
    holder.durationText = (TextView) view.findViewById(R.id.durationText);
    holder.synchedText = (TextView) view.findViewById(R.id.synchedText);
    holder.toDownloadSong = (CheckBox) view.findViewById(R.id.toDownloadSong);
    holder.contextMenuButton = (Button) view.findViewById(R.id.contextMenuButton);
    holder.cancelButton = (Button) view.findViewById(R.id.cancelButton);
    holder.pauseButton = (Button) view.findViewById(R.id.pauseButton);
    holder.resumeButton = (Button) view.findViewById(R.id.resumeButton);
    return holder;
}

ViewHolder是

private class ViewHolder {
    public int position;
    public TextView titleText;
    public TextView artistText;
    public TextView durationText;
    public TextView synchedText;
    public CheckBox toDownloadSong;
    public Button cancelButton;
    public Button pauseButton;
    public Button resumeButton;
    public Button contextMenuButton;
}

但它不能正常工作 - 我再次滚动时遇到低性能。我哪里错了?

UPD

public static String secondsToTimeStringFormat(int seconds) {
     int minutes = seconds / SECONDS_IN_MINUTE;
     int secs = seconds % SECONDS_IN_MINUTE;
     return String.format("%d:%02d", minutes, secs);
 }

UPD2

  @Override       
  @SuppressWarnings("unchecked")
  protected T getModel(int position) {
      return ((T) getItem(position));
  }

  public Object getItem(int position) {
      return data.get(position);
  }

  protected ArrayList<T> data;   // it contains about 700-800 items. T is Audio defined below

public class Audio {    
  public String  id;   
  public String title;
  public String artist;
  public int duration;
  public String url;
  public String fileName;    
  public String OwnerId;    
  public int length;
}

1 个答案:

答案 0 :(得分:0)

一个好的做法 - 尤其是在列表中使用视图时 - 您应该始终尝试尽可能宽地展平视图,以避免在视图中出现多个ViewGroup。这将使机器有更多时间来绘制视图。

在您的情况下,我建议您尝试将所有子视图分组到RelativeLayout根目录中并将它们彼此放置。

另外,请考虑使用if已定义的父级(因为RelativeLayout在您的情况下是完美的),以避免绘制不必要的ViewGroup。

最后,您的XML表明某些视图与其他视图重叠(例如,使用layout_width和layout_height match_parent定义的XML底部的LinearLayout)。尽可能避免重叠。

您可以从link

了解有关优化布局的详情

我希望这有帮助!