2016-11-01 2 views
0

У меня есть ListView с ImageView, охватывающий ряд. (Я загружаю изображение с помощью Picasso. Сначала я попытался скользить, но Picasso немного быстрее). Он работает достаточно гладко на леденец и 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:

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

Смежные вопросы