2015-02-10 177 views
0

我使用的是ListView,它的每個項目都包含一些控件。當我向下滾動這個ListView時,它很慢。我知道ViewHolder模式是用來解決這個問題,我實現了這種模式。但它不能解決我的問題。Listview慢速滾動

我的ListView項的XML是

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

但它不工作propwerly - 我面臨着低再次滾動時的性能。我在哪裏錯了?

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

'secondsToTimeStringFormat'看起來很貴。你介意發佈嗎? – Blackbelt 2015-02-10 19:36:51

+0

@Blackbelt我發佈了它 – Junior222 2015-02-10 19:39:19

+0

不,它不是..'getModel'呢? – Blackbelt 2015-02-10 19:41:20

回答

0

好的做法 - 多特,而在列表中使用的觀點 - 你應該總是嘗試儘可能寬的扁平化您的看法爲了避免你的視圖中有多個ViewGroup。這會讓機器有更多時間來繪製視圖。

在你的情況下,我會建議你嘗試將你所有的子視圖分組在一個RelativeLayout根目錄下,並將它們放在另一個根目錄下。

爲了避免畫一個不必要的ViewGroup,還要考慮使用如果你的父母已經定義好了(因爲RelativeLayout對你來說是完美的)。

最後,XML表明某些視圖與其他視圖重疊(例如,使用layout_width和layout_height match_parent定義的XML底部的LinearLayout)。儘可能避免重疊。

您可以瞭解更多關於此link

我希望這有助於優化佈局!