0

我有一個有時會使XML(TextView Item)或其他XML(TextView +圖像項)膨脹的列表視圖。我遵循這些說明來獲得更好的性能。Android - 自定義列表視圖滾動速度很慢

http://www.google.com/events/io/2010/sessions/world-of-listview-android.html

https://dl.google.com/googleio/2010/android-world-of-listview-android.pdf

http://android.amberfog.com/?p=296

它現在更好,但仍無法獲得最佳的性能,仍然滾動速度很慢。這裏是我的代碼:

GetView:(// ViewHolder 0 - >部分項目 ViewHolder 1 - >參賽作品)

@Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View v = convertView; 
     ViewHolder[] viewHolders=new ViewHolder[2]; 
     final ListItems.Item i = items.get(position); 
     if (i != null) { 
      if(i.isSection()){ 
       TextView sectionView; 
       ListItems.SectionItem si = (ListItems.SectionItem)i; 
       if (v==null || ((ViewHolder[])v.getTag())==null || ((ViewHolder[])v.getTag())[0]==null) 
       { 
        v = vi.inflate(R.layout.order_list_item_section, null); 
        sectionView= (TextView) v.findViewById(R.id.list_item_section_text); 
        viewHolders[0]=new ViewHolder(); 
        viewHolders[0].Title=sectionView; 
        v.setTag(viewHolders); 
       } 
       else 
       { 
        viewHolders[0]=((ViewHolder[]) v.getTag())[0]; 
        sectionView=viewHolders[0].Title; 
       } 
       sectionView.setBackgroundColor(android.graphics.Color.BLACK); 
       sectionView.setText(si.getTitle()); 
      } 
      else if(i.isEntryItem()) 
      {    
       ListItems.EntryItem ei = (ListItems.EntryItem)i; 
       TextView title = null,subtitle = null; 
       ImageView img = null; 
       if (v==null || ((ViewHolder[])v.getTag())==null || ((ViewHolder[])v.getTag())[1]==null) 
       { 
        v = vi.inflate(R.layout.order_list_item_entry, null); 
        title = (TextView)v.findViewById(R.id.list_item_entry_title); 
        img=(ImageView)v.findViewById(R.id.list_item_entry_drawable); 
        subtitle = (TextView)v.findViewById(R.id.list_item_entry_summary); 
        viewHolders[1]=new ViewHolder(); 
        viewHolders[1].Title=title; 
        viewHolders[1].SubTitle=subtitle; 
        viewHolders[1].TableImage=img; 
        v.setTag(viewHolders); 
       } 
       else 
       { 
        viewHolders[1]=((ViewHolder[]) v.getTag())[1]; 
        title=viewHolders[1].Title; 
        subtitle=viewHolders[1].SubTitle; 
        img=viewHolders[1].TableImage; 
       } 
       title.setTextSize(20); 
       title.setTextColor(android.graphics.Color.BLACK); 
       title.setText(Html.fromHtml("<b><i>"+ei.title+"</b></i>")); 
       img.setImageBitmap(ei.tableImage); 
       img.setScaleType(ScaleType.FIT_XY); 
       img.setAdjustViewBounds(true); 
       if(!ei.subtitle.equals("")) 
       { 
        subtitle.setTextSize(15); 
        subtitle.setTextColor(android.graphics.Color.GRAY); 
        subtitle.setText(Html.fromHtml("<i>"+ei.subtitle+"</i>")); 
       } 
      } 

ViewHolder : 

    static class ViewHolder { 
      TextView Title; 
      TextView SubTitle; 
      ImageView TableImage; 
     } 

編輯:

order_list_item_section.xml

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

    <include 
     android:id="@+id/list_item_section_text" 
     layout="@android:layout/preference_category" /> 

</LinearLayout> 

order_list_item_entry.xml:

<?xml version="1.0" encoding="utf-8"?> 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:minHeight="?android:attr/listPreferredItemHeight" 
    android:gravity="center_vertical" 
    android:paddingRight="?android:attr/scrollbarSize" 
    android:background="#ffffff"> 

    <ImageView 
     android:id="@+id/list_item_entry_drawable" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:paddingLeft="6dp"/> 

    <RelativeLayout 
     android:layout_width="0dip" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="0dip" 
     android:layout_marginRight="6dip" 
     android:layout_marginTop="6dip" 
     android:layout_marginBottom="6dip" 
     android:layout_weight="1"> 

     <TextView android:id="@+id/list_item_entry_title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:singleLine="true" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:ellipsize="marquee" 
      android:fadingEdge="horizontal" /> 

     <TextView android:id="@+id/list_item_entry_summary" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@id/list_item_entry_title" 
      android:layout_alignLeft="@id/list_item_entry_title" 
      android:textAppearance="?android:attr/textAppearanceSmall" 
      android:singleLine="true" 
      android:textColor="?android:attr/textColorSecondary" /> 

    </RelativeLayout> 

</LinearLayout> 

我需要任何建議。謝謝。

+0

它有時會通過你如何定義你的XML佈局,礦山也張貼那些你充氣個XML造成的?你在擴展ArrayAdapter嗎?當你有這種類型的列表,你會膨脹不同類型的佈局時,可以考慮擴展BaseAdapter。然後你可以重複使用你的convertView,而不是每次膨脹getView被稱爲 – 2013-05-11 09:42:58

+0

現在發佈....我發佈了XML文件。是的,我使用ArrayAdapter,你能告訴我一個關於BaseAdapter的axample – 2013-05-11 09:45:54

+0

我試圖改變它的BaseAdapter,但結果仍然是相同的。也許真的稍微好一點。 – 2013-05-11 10:05:08

回答

2

如果被接受,我想我需要在這裏包括調試:

  1. 取出ellipsizefadingedge,併爲雙方的TextView將寬度設置爲match_parent,如果仍然緩慢滾動,試下。

  2. wrap_content上的ImageView scaleType="fitXY"在一起其實聽起來很,衝突如果你問我,嘗試特別的寬度和高度設置爲也許48dp或55dp,然後fitXY有道理。

這裏的功能,適當BaseAdapter類的例子:

public static final int SECTION = 2; 
public static final int ENTRY_ITEM = 1; 
private List<ListItems.Item> items; // I believe you have this List declared? 

public YourClassConstructor(){ 
    //Whatever you need to accept and assign, probably the list itself. 
} 

public int getCount() { 
    return items.size(); 
} 

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

public long getItemId(int position) { 
    return (long) position; // Or anything that can define your item 
} 

@Override 
public int getViewTypeCount() { 
    return 2; // 2 because you have Section and EntryItem 
} 

@Override 
public int getItemViewType(int position) { 
    int itemViewType; 
    ListItems.Item i = items.get(position); 

    if(i.isSection()){ 
     itemViewType = SECTION; 
    }else{ 
     itemViewType = ENTRY_ITEM; 
    } 

    return itemViewType; 
} 

@Override 
public View getView(final int position, final View convertView, final ViewGroup parent { 
    //Here your convertView will always be the one you need, either as a section or entry_item 
    //Unless there is no convertView available yet. 
    //So your (ViewHolder[])v.getTag())[0/1] will less likely be null. 
} 
+0

謝謝你的救命。 – 2013-05-11 10:55:10

0

v = vi.inflate(R.layout.order_list_item_section, null);只有如果v爲空。這將有很大幫助。

+0

你甚至沒有閱讀代碼! – 2013-05-11 09:59:39

+0

你是對的,對不起,不是藉口,但這就是爲什麼谷歌建議避免像這樣的一長串代碼'if(v == null ||((ViewHolder [])v.getTag())== null ||((ViewHolder [])v.getTag())[0] == null)':-) – herveRilos 2013-05-11 10:04:53

+0

但是,你可以從代碼中讀取我需要檢查它們:/ – 2013-05-11 10:05:54

相關問題