0

I'm using ListView and each item of it contains some controls. And when I scroll down this Listview it is pretty slow. I know that ViewHolder pattern is used to resolve this problem and i implemented this pattern. But it sdoesn't solve my problem.

My xml of listview item is

<?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>

and my code is

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;
}

And ViewHolder is

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;
}

But it doesn't works propwerly - i faced with low perfomance when scrolling again. Where am i wrong?

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;
}
Junior222
  • 803
  • 3
  • 12
  • 22

1 Answers1

0

A good practice - and more especially while using views in lists - you should always attempt to flatten your view as wide as possible in order to avoid multiple ViewGroup inside your view. This will cause more time to the machine to draw your view.

In your case I would advise you to try to group all your subviews inside a RelativeLayout root and place them from one another.

Also consider using the if your parent is already defined (as a RelativeLayout would be perfect in your case) in order to avoid drawing an unnecessary ViewGroup.

Finally, your XML suggests that some views overlap others (such as the LinearLayout at the bottom of your XML defined with both layout_width and layout_height match_parent). Try to avoid overlapping whenever you can.

You can learn more about optimizing your layout from this link

I hope this helps!

Stephen Vinouze
  • 1,815
  • 1
  • 17
  • 28