如何ViewPager负荷片段只有当其选择负荷、片段、ViewPager

2023-09-06 03:37:17 作者:夨薏後ㄆ從來

使用内Viewpager 3个片段,问题的IM装载在asytask和装载机大数据,在设备,如HTC的一部作品很好,但在低端设备需要大量的时间IM。这是因为主要是,当我实现pagerAdapter我把片段的ArrayList里面,这股力量的片段instatiate时的主要活动的加载。我需要的只是负荷的第一个分片(主),当用户的Swype加载另一个片段是什么。它的任何方式来实现这一目标?这是我的pageAdapater

 公共类PagerAdapter扩展FragmentPagerAdapter {

    私人最终的ArrayList<片断> mFragments =新的ArrayList<片断>();
   //私人最终的ArrayList<字符串> titulos =新的ArrayList<字符串>();

   //私人诠释NUM_PAGES = 0;
    公共PagerAdapter(FragmentManager经理,诠释NUM_PAGES){
        超(经理);
     // this.NUM_PAGES = NUM​​_PAGES;
    }


    公共无效addFragment(片段片段,串题){
        mFragments.add(片段);
            notifyDataSetChanged();
    }

    @覆盖
    公众诠释getCount将(){
        //返回NUM_PAGES;
       返回mFragments.size();
    }

    @覆盖
    公共片段的getItem(INT位置){
        返回mFragments.get(位置);

    }

}
 

解决方案

我要加我的解决方案,在这里,因为我遇到了类似的问题。我的异步任务不加载大量的数据,但它prevents不必要的网络电话。这是我加入我的片段:

 私人布尔_hasLoadedOnce = FALSE; //你的布尔字段

@覆盖
公共无效setUserVisibleHint(布尔isFragmentVisible_){
    super.setUserVisibleHint(isVisibleToUser);


    如果(this.isVisible()){
        //我们检查该片段是可见
        如果(isFragmentVisible_&安培;!&安培;!_hasLoadedOnce){
            //运行的异步任务,在这里,因为用户只是专注于您的片段
            _hasLoadedOnce = TRUE;
        }
    }
}
 

通过上面的code,你的片段将被加载,但你的异步任务将无法运行,直到用户实际滚动到片段的第一次。一旦显示,你的异步任务会自动运行的第一次。然后,你可以提供一种方式,通过一个按钮来加载更多的数据或拉来刷新。以上片段是在我ViewPager,似乎很好地工作。

国网江苏电科院 张潼 基于非侵入式负荷辨识的聚合负荷需求响应能力在线评估

im using 3 Fragments inside a Viewpager, the problem its im loading big data in asytask and loaders, in devices like HTC one works well, however in low end devices take a lot of time. THis is because mainly when i implement the pagerAdapter i put the Fragments inside an arrayList, this force the fragments instatiate when the main activity its loaded. What i need its just "load" the first fragment (main) and when the user "swype" load the other fragment. its any way to achieve this? this is my pageAdapater

public class PagerAdapter extends FragmentPagerAdapter  {

    private final ArrayList<Fragment> mFragments = new ArrayList<Fragment>();
   // private final ArrayList<String> titulos = new ArrayList<String>();

   // private int NUM_PAGES =0;
    public PagerAdapter(FragmentManager manager,int num_pages) {
        super(manager);
     //   this.NUM_PAGES = num_pages;
    }


    public void addFragment(Fragment fragment,String title) {
        mFragments.add(fragment);
            notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        //return NUM_PAGES;
       return mFragments.size();
    }

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

    }

} 

解决方案

I'm gonna add my solution here since I faced a similar issue. My asynchronous task wasn't loading huge amounts of data, but it prevents unnecessary network calls. Here's what I added in my Fragment:

private boolean _hasLoadedOnce= false; // your boolean field

@Override
public void setUserVisibleHint(boolean isFragmentVisible_) {
    super.setUserVisibleHint(isVisibleToUser);


    if (this.isVisible()) {
        // we check that the fragment is becoming visible
        if (!isFragmentVisible_ && !_hasLoadedOnce) {
            //run your async task here since the user has just focused on your fragment
            _hasLoadedOnce = true;
        }
    }
}

With the above code, your Fragment will be loaded, but your async task will not run until the user actually scrolls to the Fragment for the first time. Once displayed, your async task will run for the first time automatically. Then you can provide a way to load more data via a button or pull to refresh. The above Fragment was in my ViewPager and seemed to work fine.