ViewPager + FragmentPagerAdapter应用程序运行缓慢,其中一个片段具有GridView

时间:2014-10-28 03:35:31

标签: android android-fragments android-viewpager

我的布局中有ViewPager,ViewPager包含三个片段。我正在使用gridview作为其中一个片段,每个网格由图像组成(6个图像)。当我在使用gridview的片段和带有平面文本的片段之间进行过渡时,滑动非常慢。

我已经在片段的OnCreateView()中初始化了gridview和layout inflater,它响应缓慢。 我应该使用 FragmentStatePagerAdapter 吗?但我使用的片段数量只有3个。如何在不同的标签之间平滑过渡?

使用gridview的片段类代码

public class MainTourism extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        ViewGroup rootView = (ViewGroup) inflater.inflate(
                R.layout.fragment_tourism, null);
        GridView gridview = (GridView) rootView.findViewById(R.id.gridview);
        gridview.setAdapter(new TourismImageAdapter(getActivity()));

        return rootView;
    }

}

适配器类的代码

public class TourismImageAdapter extends BaseAdapter    {

private Integer[] mThumbIds = {
        R.drawable.a,
        R.drawable.b,
        R.drawable.c,
        R.drawable.d};

private Context mContext;
public TourismImageAdapter(Context c){
    mContext = c;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return mThumbIds.length;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    ImageView imageView;
    if (convertView == null) {  // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(150, 150));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
    } else {
        imageView = (ImageView) convertView;
    }

    imageView.setImageResource(mThumbIds[position]);
    return imageView;
}

}

0 个答案:

没有答案