ViewPager不能平滑滚动

时间:2015-04-24 08:05:21

标签: android android-viewpager

我在我的活动中实现了一个viewpager,在滚动时,将一组数据加载到适配器并在列表视图中显示它们。一切都工作正常,但我似乎无法找到如何使其更有效,因为滚动时,视图冻结,但例如在GMAIL应用程序中滚动电子邮件时滚动是如此顺利。

以下是我正在使用的代码:

活动:

在oncreate中

pageradapter adapter2 = new pageradapter(this, ArrayOfItems);   //ArrayOfItems is an Array containing the data that will be displayed
        ViewPager myPager = (ViewPager) findViewById(R.id.mypanelpager);
        myPager.setAdapter(adapter2);
pagechangelistener pageListener = new pagechangelistener();
        myPager.setOnPageChangeListener(pageListener);

pageradapter:

public pageradapter(Activity act, ArrayList <List<Item>>Arrayitems) {
    this.Arrayitems = Arrayitems;
    activity = act;
}

public int getCount() {
    return Arrayitems.size();
}

@Override
public Object instantiateItem(ViewGroup container, int position) {
    List<Item> item = Arrayitems.get(position);
    ListView view = new ListView(activity);
    taskdetailsarrayadaptertest adapter = new taskdetailsarrayadaptertest(activity, item);
    view.setAdapter(adapter);
    view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
    ((ViewPager) container).addView(view, 0);
    return view;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    ((ViewPager) container).removeView((View) object);
}

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
    return arg0 == ((View) arg1);
}

@Override
public Parcelable saveState() {
    return null;
}

pagechangelistener:

@Override
public void onPageSelected(int position)
{
    currentPage = position;
    int positionTemp = position + 1;
}

public final int getCurrentPage() {
    return currentPage;
}

2 个答案:

答案 0 :(得分:0)

首先你需要使用这样的持有者。

在ViewPager中

class Holder{
    ListView listView;
    Adapter adapter;
}

private ArrayList<Holder> mHolders;

//init holders

@Override
public Object instantiateItem(ViewGroup collection, int position) {
    Holder holder = mHolders.get(position);
    holder.listView.setAdapter(holder.adapter);
    collection.addView(holder.listView);
    return holder.listView;
}

第二次在listview中尝试异步加载数据

答案 1 :(得分:0)

设置myPager.setOffscreenPageLimit(1);