Android GridView适配器滞后

时间:2014-04-27 10:36:13

标签: android gridview android-adapter lag

我正在使用GridView在我的应用程序上显示一些图像。

当我得到UniversalImageLoader(带有i)时,我也使用Uri来显示这些图像。

我在我的适配器中设置了一个View holder,但当我滚动GridView它的时间有点迟了。这是不可接受的。

这是我的代码:

public class GridViewAdapter extends ArrayAdapter<Object>
{
    private Context context = null;
    private List<Object> objects = null;
    private ImageLoader imageLoader = null;
    private DisplayImageOptions options = null;

    public CountDownAdapter(Context context, List<Object> objects)
    {
        super(context, android.R.layout.simple_list_item_1, objects);
        this.context = context;
        this.objects = objects;

        this.imageLoader = ImageLoader.getInstance();
        if (!this.imageLoader.isInited()) this.imageLoader.init(ImageLoaderConfiguration.createDefault(context));
        this.options = new DisplayImageOptions.Builder()
            .showImageOnLoading(R.drawable.ic_downloading)
            .showImageForEmptyUri(R.drawable.ic_downloading)
            .showImageOnFail(R.drawable.ic_downloading)
            .cacheInMemory(true)
            .cacheOnDisk(true)
            .considerExifParams(true)
            .bitmapConfig(Bitmap.Config.RGB_565)
            .build();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        CountDownHolder holder = null;

        if (convertView == null)
        {
            LayoutInflater layoutInflator = LayoutInflater.from(getContext());
            convertView = layoutInflator.inflate(R.layout.adapter_gridview, null);

            holder = new CountDownHolder();
            holder.img_thumbnail = (ImageView) convertView.findViewById(R.id.img_thumbnail);
            holder.text_event = (TextView) convertView.findViewById(R.id.text_event);
            holder.text_date = (TextView) convertView.findViewById(R.id.text_date);
            holder.text_daysleft = (TextView) convertView.findViewById(R.id.text_daysleft);
            convertView.setTag(holder);
        }
        else
        {
            holder = (CountDownHolder) convertView.getTag();
        }

        final Object object = this.objects.get(position);

        // Apply background
        imageLoader.displayImage("file://" + countDown.getThumbnail().getPath(), holder.img_thumbnail, options);
        //holder.img_thumbnail.setImageURI(countDown.getThumbnail());

        // Format date
        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yy", Locale.getDefault());
        String date = formatter.format(new Date(countDown.getDateEvent()));

        // Format event
        String event = countDown.getEvent().substring(0, 1).toUpperCase(Locale.getDefault()) + countDown.getEvent().substring(1).toLowerCase(Locale.getDefault());

        // Format days
        String days = null;
        if (countDown.wasBefore() && countDown.getTimeRemaining() == 1) days = context.getString(R.string.main_yesterday);
        else if (countDown.getTimeRemaining() == 0) days = context.getString(R.string.main_today);
        else if (countDown.getTimeRemaining() == 1) days = context.getString(R.string.main_tomorrow);
        else days = countDown.getTimeRemaining() +" "+ context.getString(R.string.main_days);

        // Set values
        holder.text_event.setText(event);
        holder.text_date.setText(date);
        holder.text_daysleft.setText(days);

        // Attach listener
        // ...

        return convertView;
    }

    public List<Object> getObjects()
    {
        return objects;
    }

    public void setObjects(List<Object> objects)
    {
        this.objects = objects;
    }
}

我做错了什么或者我忘了做一些重要的事情?

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

问题解决了:

我使用的是在getView方法中初始化的自定义字体。只需将其移至构造函数即可解决滞后问题。