Android的 - ListView的scrollig太慢 [英] Android - ListView scrollig too slow

查看:128
本文介绍了Android的 - ListView的scrollig太慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的ListView 有一个图像,并为每个元素(举办文本的两行 RelativeLayout的 )。它的工作原理确定,但它的速度太慢,我知道问题出在哪里来自!

I have a ListView that has one image and two lines of texts for each element (organized by a RelativeLayout). It works ok, but it's too slow and I know where the problem comes from!

这是我使用自定义的适配器 getView()方法:

This is the getView() method for the custom adapter that I'm using:

public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = mLayoutInflater.inflate(R.layout.list_view_item, parent, false);
                mViewHolder = new ViewHolder();
                mViewHolder.cover = (ImageView) convertView.findViewById(R.id.app_icon);
                mViewHolder.title = (TextView) convertView.findViewById(R.id.selection);
                mViewHolder.description = (TextView) convertView.findViewById(R.id.app_short_description);
                convertView.setTag(mViewHolder);
            } else {
                mViewHolder = (ViewHolder) convertView.getTag();
            }

            // Here is the origin of the issue ! 
            final Feed currentFeed = getItem(position);
            mViewHolder.title.setText(currentFeed.getTitle());
            mViewHolder.description.setText(currentFeed.getDescription());

            try {
                if(currentFeed.getThumbnailUrl() != null) {
                     downloadThumbnail(mViewHolder.cover, currentFeed.getThumbnailUrl());
                }
            } catch(Exception e) {
                e.printStackTrace();
            }

            return convertView;
}

private static class ViewHolder {
        TextView title;
        TextView description;
        ImageView cover;
}

所以,我做了一些人工标杆,看来,分配的一个实例饲料这是缓慢的来源:

final Feed currentFeed = getItem(position);

我知道这是因为我写的这另一个版本来比较两个:

I know this because I have written another version of this to compare the two:

// Here is the origin of the issue ! 
            //final Feed currentFeed = getItem(position);
            mViewHolder.title.setText("Title");
            mViewHolder.description.setText("Description");

            try {
            if(currentFeed.getThumbnailUrl() != null) {
                 downloadThumbnail(mViewHolder.cover, "some url");
            }
        } catch(Exception e) {
            e.printStackTrace();
        }

这一次是平滑的方式(即使是使用 downloadThumbnail()法工作)。

This one was way smoother (even with the downloadThumbnail() method working).

我也precise,有15只在我的项目的ListView

I also precise that there are only 15 items on my ListView.

我知道,分配对象是非常昂贵的,因为垃圾收集的,但我不能任何其他方式做到这一点!

I know that allocating objects is very expensive because of garbage collection but I can't any other way to do it!

任何想法?

谢谢!

修改

不要过分在意的downloadThumbnail()方法,它已经做了一些缓存。而实际上,即使没有任何图片,它仍然缓慢。

Don't mind too much about the downloadThumbnail() method, it already does some caching. And actually even without any picture, it's still slow.

推荐答案

当用户滚动列表,getView被调用适配器上。请确保您不这样做同样的事情反复,例如生成缩略图。如果项目的数量限制(例如视频内容),那么你就可以创建所有的意见,并保持它准备获取视图。否则,你可能需要实现cacheing。

When user scrolls the list, getView gets called on the adapter. Make sure that you dont do same things repeatedly, for example generating thumbnail. If number of items is limited (for example video content), then you can create all views and keep it ready for get view. Otherwise you may have to implement cacheing.

下面code示出了适配器和ListView实现,其中,在所有的列表视图中创建并存储在存储器中。由于这是为视频浏览,内存不构成任何问题。 (数字内容的限制,最大100)

Below code shows an adapter and listView implementation, where in all listviews are created and stored in memory. Since this is meant for video browsing, memory does not pose any issue. (limited number of content, max 100)

视频列表适配器

import java.util.ArrayList;
import java.util.Formatter;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

import android.content.ContentResolver;
import android.content.Context;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

public class VideoListAdapter extends BaseAdapter {
    private Context mContext = null;
    private HashMap<String, VideoListItem> mHashedItems = new HashMap<String, VideoListItem>(); 
    private static final String TAG = "VideoListAdapter";

    public static final int VIDEO_CONTENT_ID       = 0;
    public static final int VIDEO_CONTENT_TITLE    = 1;
    public static final int VIDEO_CONTENT_DURATION = 2;
    public static final int VIDEO_CONTENT_RESOLUTION = 3;
    public static final int VIDEO_CONTENT_MIME = 4;

    private Cursor mCursorForVideoList = null;
    private ContentResolver mContentResolver = null;
    private int mListCount = 0;

    VideoListAdapter(Context context, ContentResolver cr) {
        mContext         = context;
        mContentResolver = cr;
        Log.i(TAG, "In the Constructor");

        mCursorForVideoList = 
            mContentResolver.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, 
                                  new String[] { MediaStore.MediaColumns._ID, 
                                                 MediaStore.MediaColumns.TITLE, 
                                                 MediaStore.Video.VideoColumns.DURATION,
                                                 MediaStore.Video.VideoColumns.RESOLUTION
                                               }, 
                                  null, 
                                  null,  
                                  null);
        mListCount = mCursorForVideoList.getCount();
    }

    @Override
    public int getCount() {
        return mListCount;
    }

    @Override
    public Object getItem(int arg0) {
        return getVideoListItem(arg0);
    }

    @Override
    public long getItemId(int position) {
        //Log.i(TAG, "position : " + position);
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //Log.i(TAG, "GetView :: Position : " + position);
        return getVideoListItem(position);
    }

    private VideoListItem getVideoListItem(int position)
    {
        //Log.i(TAG, "getVideoListItem :: Position : " + position);
        String key = Integer.toString(position);
        VideoListItem item = mHashedItems.get(key);
        if(item == null)
        {
            //Log.i(TAG, "New getVideoListItem :: Position : " + position);
            mCursorForVideoList.moveToPosition(position);
            mHashedItems.put(key, new VideoListItem(mContext, mContentResolver, mCursorForVideoList));
        }
        return mHashedItems.get(key);
    }

};

视频列表视图

import java.util.Formatter;
import java.util.Locale;

import android.content.ContentResolver;
import android.content.Context;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Typeface;
import android.provider.MediaStore;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

class VideoListItem extends LinearLayout
{
    private static final String TAG = "VideoListAdapter";

    private ImageView mThumbnail = null;
    private TextView mDuration   = null;
    private TextView mTitle      = null;
    private TextView mResolution = null;

    private LayoutInflater mLayoutFactory = null;

    private long mContentId = 0;

    public VideoListItem(Context context, ContentResolver cr, Cursor cursor) {
        super(context);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
        params.setMargins(10, 10, 10, 10);

        mLayoutFactory = LayoutInflater.from(context);
        View thisView = mLayoutFactory.inflate(R.layout.videolistitem, null);
        addView(thisView);

        mThumbnail  = (ImageView) findViewById(R.id.thumbnail); 
        mDuration   = (TextView)  findViewById(R.id.DDuration);
        mTitle      = (TextView)  findViewById(R.id.DTitle);
        mResolution = (TextView)  findViewById(R.id.DResolution);

        mThumbnail.setLayoutParams(new LinearLayout.LayoutParams(144, 144));

        Resources r = this.getResources();
        Bitmap bMap = MediaStore.Video.Thumbnails.getThumbnail(cr, cursor.getLong(VideoListAdapter.VIDEO_CONTENT_ID), MediaStore.Video.Thumbnails.MINI_KIND, null);
        if(bMap != null)
        {
            mThumbnail.setImageBitmap(Bitmap.createScaledBitmap(bMap, 128, 128, true)); 
        }
        else
        {
            mThumbnail.setImageDrawable(r.getDrawable(R.drawable.error));
        }
        mThumbnail.setPadding(16, 16, 16, 16);
        mTitle.setText(cursor.getString(VideoListAdapter.VIDEO_CONTENT_TITLE));
        mTitle.setSingleLine();
        mTitle.setTextColor(Color.GREEN);

        mResolution.setText(cursor.getString(VideoListAdapter.VIDEO_CONTENT_RESOLUTION));
        mResolution.setSingleLine();
        mResolution.setTextColor(Color.RED);

        mDuration.setText(stringForTime(cursor.getInt(VideoListAdapter.VIDEO_CONTENT_DURATION)));
        mDuration.setSingleLine();
        mDuration.setTextColor(Color.CYAN);

        mContentId = cursor.getLong(VideoListAdapter.VIDEO_CONTENT_ID);
    }

    public long getContentId()
    {
        return mContentId;
    }

    private StringBuilder mFormatBuilder = null;
    private Formatter mFormatter = null;

    private String stringForTime(int timeMs) {
        int totalSeconds = timeMs / 1000;

        mFormatBuilder = new StringBuilder();
        mFormatter = new Formatter(mFormatBuilder, Locale.getDefault());

        int seconds = totalSeconds % 60;
        int minutes = (totalSeconds / 60) % 60;
        int hours   = totalSeconds / 3600;

        mFormatBuilder.setLength(0);
        if (hours > 0) {
            return mFormatter.format("%d:%02d:%02d", hours, minutes, seconds).toString();
        } else {
            return mFormatter.format("%02d:%02d", minutes, seconds).toString();
        }
    }

};

Shash

这篇关于Android的 - ListView的scrollig太慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆