内部片段ViewPager需要很长时间才能加载子片段

时间:2017-01-30 06:33:35

标签: android android-viewpager fragment

我有一个问题,即加载数据需要很长时间,并且仅在我滚动视图寻呼机时显示。好几次我已经使用了asynctask但没有工作。更换标签后,我调用相同的片段任何一个帮助我?

Here, is first time loaded data

After changing tab call same fragment

这是我的ViewPager适配器

public class AdapterCategoryViewPager extends FragmentPagerAdapter {
Context context;
private List<Fragment> fragmentList;
private List<Type> typeList;

public AdapterCategoryViewPager(FragmentManager fm, Context context, List<Fragment> fragmentList, List<Type> typeList) {
    super(fm);
    this.context = context;
    this.fragmentList = fragmentList;
    this.typeList = typeList;
}


@Override
public Fragment getItem(int position) {
    return fragmentList.get(position);
}

@Override
public CharSequence getPageTitle(int position) {
    return typeList.get(position).getType().toString();
}

@Override
public int getCount() {
    return fragmentList.size();
}
}

这是我的片段类别此功能我调用异步任务

private void setData(View v)
{
    addWomenCategory();
    ll_list.removeView(v);
    gridView.removeView(v);
    gridView.setMode(PullToRefreshBase.Mode.PULL_FROM_END);
    adapterCategory=new AdapterCategory(v.getContext(),categoryArrayList,height,width);
    gridView.setAdapter(adapterCategory);
    g=(GridView) gridView.getRefreshableView();
    g.setHorizontalSpacing(h7);
    g.setVerticalSpacing(h7);
   // g.setEnabled(false);

    gridView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<GridView>() {
        @Override
        public void onRefresh(final PullToRefreshBase<GridView> refreshView) {
            gridView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    refreshView.getLoadingLayoutProxy().setRefreshingLabel("Loding Data...");
                    refreshView.getLoadingLayoutProxy().setReleaseLabel("Refresh complete");
                    addWomenCategory();
                    adapterCategory.notifyDataSetChanged();
                    gridView.setRefreshing(false);
                    gridView.onRefreshComplete();

                }
            },2000);
        }
    });
}

这是我的HomeFragment,它实际上在数组列表中添加了片段类别这个函数我也在asynctask中调用

private void setData(View v)
{
    addType();
    pager.setOffscreenPageLimit(3);
    adapterType=new AdapterType(typeArrayList,v.getContext(),height,width);
    fragmentList.add(new FragmentListCategory());
    fragmentList.add(new FragmentMenListCategory());
    fragmentList.add(new FragmentKidListCategory());
    fragmentList.add(new FragmentListCategory());
    fragmentList.add(new FragmentMenListCategory());
    fragmentList.add(new FragmentKidListCategory());

    pager.setAdapter(new AdapterCategoryViewPager(getFragmentManager(),v.getContext(),fragmentList,typeArrayList));

   tabs.setViewPager(pager);
    setUpTabStrip(v);


}

2 个答案:

答案 0 :(得分:1)

我已经使用通用图像加载器库来显示图像,我每次在适配器中初始化它的对象来解决这个问题我在主要活动的整个应用程序中初始化该配置对象的onCreate()方法

答案 1 :(得分:0)

由于addWomenCategeory()是一个异步任务,主线程中的代码不会等待结果。 考虑移动代码

adapterCategory.notifyDataSetChanged(); gridView.setRefreshing(false); gridView.onRefreshComplete();

添加Womencategory()方法,从后端收到结果