ViewPager在Android中滚动滞后

时间:2015-04-18 19:12:14

标签: android android-asynctask android-viewpager android-gridview

我正在使用ViewPager来显示各种GridView,每个GridView都有一组图像。 enter image description here

我只是将所有图像复制并粘贴到drawables文件夹中,并且每个图像都有300px X 300px,当在不同网格之间滑动时,刷卡滞后很多,我尝试将图像调整为一半大小,它变得更好但是问题仍然存在。

然后我尝试使用AsyncTask加载每个图像,如下所示:

public class ImageAdapter extends BaseAdapter {
private Context mContext;
private Integer[] mSeries;

public ImageAdapter(Context c, int n) {
    super();
    this.mContext = c;
    this.mSeries = ArrayImages.getImages(n);
}

public int getCount() {
    return mSeries.length;
}

public Object getItem(int position) {
    return null;
}

public long getItemId(int position) {
    return 0;
}

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {
        // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(200, 200));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
    } else {
        imageView = (ImageView) convertView;
    }

    imageView.setDrawingCacheEnabled(true);
    new LoadImage(imageView, position).execute();
    return imageView;
}

class LoadImage extends AsyncTask<Object, Void, Integer> {

    private ImageView imv;
    private int position;

    public LoadImage(ImageView imv, int pos) {
        this.imv = imv;
        this.position = pos;
    }

    @Override
    protected Integer doInBackground(Object... params) {
        return mSeries[position];
    }
    @Override
    protected void onPostExecute(Integer result) {
        if(result != 0 && imv != null){
            imv.setVisibility(View.VISIBLE);
            imv.setImageResource(result);
        }else{
            imv.setVisibility(View.GONE);
        }
    }
}

但是这个问题仍然存在,因为有250个图像,我制作了一些数组来保存它们,当我需要它们时,我只是问:ArrayImages.getImages(n)不确定这是否可能是问题。

由于我的FragmentPagerAdapter必须创建所有16个GridFragment并获取这些数组:

public class ViewPagerAdapter extends FragmentStatePagerAdapter {

private int NumbOfTabs = 16; // Store the number of tabs, this will also be passed when the ViewPagerAdapter is created


// Build a Constructor and assign the passed Values to appropriate values in the class
public ViewPagerAdapter(FragmentManager fm) {
    super(fm);
}

//This method return the fragment for the every position in the View Pager
@Override
public Fragment getItem(int index) {
    // TODO Auto-generated method stub
    switch (index) {
        case 0:
            return new GridFragment(1);

        case 1:
            return new GridFragment(2);
        ...
        case 16:
            return new GridFragment(16);

我可以做些什么来改善负荷并消除滞后?

2 个答案:

答案 0 :(得分:1)

我使用此库将图像从Web加载到适配器,它只有3个类,您可以编辑它以加载本地图像,并且易于实现。 https://github.com/thest1/LazyList

答案 1 :(得分:1)

好的,所以在Vinicius向我介绍了一个图书馆但是即使这样我也无法解决问题,我决定搜索其他人并发现Picasso库它是完美且易于加载图像的,它完成所有工作对你而言。

我只是按照这个例子完成了它!

http://www.101apps.co.za/articles/gridview-tutorial-using-the-picasso-library.html