如何提高listview的滚动速度

时间:2012-11-07 08:50:51

标签: android android-listview

  

编辑:好的,我接受了答案中给出的建议,差异是巨大!我已经用新的替换了我的SeriesAdapter。   对于一个知道在sql查询中进行计算(知道   观看总集数和观看总数的总数。我也   一旦加载了位图就将其存储在散列映射中,这样就不会   必须加载两次,我正在寻找其他解决方案,因为我   害怕OutOfMemoryException。

我是Android的新手,我想显示一个列表视图,其中包含我存储在外部存储器中的图像。

图像是先前下载的,现在就像我说的存储在外部存储器中一样,这里是图像http://thetvdb.com/banners/graphical/80348-g32.jpg的一个例子,我保存它们以节省一些空间时将图像压缩到80%。

我已经尝试了几种方法来使列表视图滚动顺畅,但我显然在这里。我已经为列表项和我的适配器提供了我的布局,以防我在这里做了一些奇怪的事情。

我会很感激能够改善我的列表视图的任何提示和技巧。

SeriesAdapter:

public static class SeriesAdapter extends ArrayAdapter<Series> {

    static class viewHolder
    {
        ImageView image;
        TextView information;
        String seriesId;
        String season;
        ProgressBar progress;
        TextView txtSmallView;
    }

    private final Context context;
    private final ArrayList<Series> series;
    private DateHelper dateHelper;
    private final DatabaseHandler db;
    Object mActionMode;
    int resource;

    public SeriesAdapter(Context context, int resource, ListView lv, ArrayList<ExtendedSeries> objects)
    {
        super(context, resource, objects);
        this.context = context;
        this.series = objects;
        this.resource = resource;
        db = new DatabaseHandler(context);
        dateHelper = new DateHelper();  

        cache = new HashMap<String, Bitmap>();
    }



    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {

        viewHolder holder;
        ExtendedSeries s = series.get(position);

        if(convertView == null)
        {
            convertView = View.inflate(context, resource, null);
            holder = new viewHolder();

            holder.image = (ImageView)convertView.findViewById(R.id.imgSeriesImage);
            holder.information = (TextView)convertView.findViewById(R.id.txtUpcomingEpisode);
            holder.progress = (ProgressBar)convertView.findViewById(R.id.pgrWatched);                       

            convertView.setTag(holder);
        }
        else
        {
            holder = (viewHolder)convertView.getTag();
        }


        if(s != null)
        {
            holder.seriesId = s.getImage();


            convertView.setTag(R.string.homeactivity_tag_id,s.getID());
            convertView.setTag(R.string.homeactivity_tag_seriesid,s.getSeriesId());

            holder.progress.setMax(s.getTotalEpisodes());
            holder.progress.setProgress(s.getWatchedEpisodes());
            holder.image.setImageBitmap(getBitmapFromCache(s.getImage()));
            holder.information.setText(s.getNextEpisodeInformation().equals("") ? context.getText(R.string.message_show_ended) : s.getNextEpisodeInformation());        

        }


        return convertView;

    }

Listitem布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<ImageView
    android:id="@+id/imgSeriesImage"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:focusable="false"
    android:scaleType="centerCrop"
    android:src="@drawable/noimage" />

<RelativeLayout
    android:id="@+id/relProgressView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:focusable="false"
    android:orientation="vertical" >

    <ProgressBar
        android:id="@+id/pgrWatched"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="fill_parent"
        android:layout_height="21dp"
        android:max="100"
        android:progress="50"
        android:progressDrawable="@drawable/progressbar" />

    <TextView
        android:id="@+id/txtUpcomingEpisode"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:padding="3dp"
        android:scrollHorizontally="true"
        android:scrollbars="none"
        android:shadowColor="@android:color/black"
        android:shadowDx="1"
        android:shadowDy="1"
        android:shadowRadius="1"
        android:textAllCaps="true"
        android:textColor="#ffffffff"
        android:textSize="11sp"
        android:textStyle="normal|bold"
        android:typeface="normal" />
</RelativeLayout>

活动布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ListView
        android:id="@+id/lstMySeries"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:longClickable="true"
        android:divider="#000000"
         />

2 个答案:

答案 0 :(得分:2)

getView方法需要尽可能轻,因为当屏幕上显示时,会为行中的每个项目调用此方法。

您已经实现了好viewHolder模式,但您还需要预处理“观看过的剧集”逻辑,这样您就不会在显示代码中循环和计数。您还需要在此方法调用之外执行db.GetAiredEpisodes调用。

答案 1 :(得分:0)

我不知道为什么你的代码在getView()方法中进行IO操作。这是一个昂贵的操作,你能不能把它变成DB中的一个字段???你不能在其他地方琐事???也许在数据加载器中,即加载系列的地方,。