2016-11-01 40 views
0

我有一個ListViewImageView覆蓋行。 (我正在用畢加索加載圖片,我曾嘗試滑行,但畢加索略快)它在棒棒糖和kitkat上足夠流暢,但在棉花糖上,滾動速度變得非常慢,有時會得到ANR消息。ListView滾動棉花糖變得緩慢與圖像

這裏是我的列表項的xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:clipChildren="false" 
     android:clipToPadding="false"> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="170dp"> 

       <ImageView 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:id="@+id/img_genre_bg" 
        android:scaleType="fitXY"/> 

      <RelativeLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 
       <!--android:background="@color/trans_black"--> 

       <RelativeLayout 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_centerInParent="true"> 

        <ImageView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:id="@+id/img_play" 
         android:layout_centerHorizontal="true" 
         android:src="@drawable/play_button" 
         /> 
        <TextView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:id="@+id/tv_genre_name" 
         android:textSize="20sp" 
         android:textAllCaps="true" 
         android:layout_centerHorizontal="true" 
         android:textColor="@color/white" 
         android:layout_below="@+id/img_play"/> 
        <TextView 
         android:layout_width="wrap_content" 
         android:layout_height="wrap_content" 
         android:id="@+id/tv_genre_id" 
         android:visibility="gone"/> 
       </RelativeLayout> 


       <View 
        android:layout_width="match_parent" 
        android:layout_height="3dp" 
        android:background="@color/colorAccent" 
        android:layout_alignParentBottom="true"/> 
      </RelativeLayout> 

     </RelativeLayout> 

列表視圖:

<ListView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/lv_genre" 
    android:scrollbars="none" 
    android:divider="@null" 
    android:cacheColorHint="@android:color/transparent" 
    android:fastScrollEnabled="true" 
    android:persistentDrawingCache="scrolling" 
    android:scrollingCache="false"> 
</ListView> 

getView():

public View getView(int position, View view, ViewGroup viewGroup) { 
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View rowview=view; 
    if(rowview==null){ 
     rowview=inflater.inflate(R.layout.item_genre_list,viewGroup,false); 
    } 
    id = (TextView) rowview.findViewById(R.id.tv_genre_id); 
    TextView name = (TextView) rowview.findViewById(R.id.tv_genre_name); 
    ImageView img_genre_bg = (ImageView) rowview.findViewById(R.id.img_genre_bg); 
    ImageView img_play = (ImageView) rowview.findViewById(R.id.img_play); 


    opensans = Typeface.createFromAsset(context.getAssets(), "font/OpenSans-Light.ttf"); 
    name.setTypeface(opensans); 

    GENRE genre=new GENRE(); 
    genre=genres.get(position); 
    id.setText(genre.getGenre_id()); 
    name.setText(genre.getGenre_name()); 


     Picasso 
      .with(context) 
      .load(genre.getGenre_image()) 
      .placeholder(R.drawable.loading) 
      .into(img_genre_bg); 

    return rowview; 
} 

我曾嘗試加載單獨的AsyncTask線程上的圖像,但它仍然卡在滾動。將其更改爲RecyclerView也沒有幫助。

+0

您沒有在創建每一行時使用'ViewHolder'模式並創建字體 – kId

回答

1

這是因爲您每創建一行都創建Typeface。每次創建行時創建Asset的開銷可能會降低執行速度。

將這段代碼移動到您班級的Constructor;

opensans = Typeface.createFromAsset(context.getAssets(), "font/OpenSans-Light.ttf"); 

然後你就可以使用已創建的類型爲:

name.setTypeface(opensans); 

這是更好地移動到RecyclerView,因爲它比ListView更有效地管理內存更適合。

+0

好吧,我做了它,但速度問題沒有太大改變。 –

+1

你也必須使用'ViewHolder'模式。您正在爲每一行創建新的視圖。這個例子演示瞭如何使用'ViewHolder':https://dzone.com/articles/optimizing-your-listview –