0

我有一个有 6 个屏幕的 ViewPager (mViewPagerMain)。在屏幕 4 中,我在另一个 (mViewPagerSub) 中有另一个 ViewPager。当使用 mViewPagerSub 时,我在我的 CustumViewPager 禁用 mViewPagerMain 的滚动 - 效果很好。现在的问题:当我滚动浏览主页时,一切正常,所有东西都加载了。当我从屏幕 4(包含子屏幕)滚动回屏幕 2 并返回到屏幕 4 时,子屏幕内的前 2 个屏幕未加载。如果我在 sub 中来回滚动,一切都会正确加载。

创建:

protected void onCreate(Bundle savedInstanceState) {
   mSectionsPagerAdapterMain = new SectionsPagerAdapterMain(getSupportFragmentManager());
   mSectionsPagerAdapterSub = new SectionsPagerAdapterSub(getSupportFragmentManager());
   mViewPagerMain = (CustomViewPager) findViewById(R.id.pagerMain);
   mViewPagerMain.setAdapter(mSectionsPagerAdapter);
}

SectionPagerAdapters:(就像在 google 示例中一样,对于 sub 相同)

public class SectionsPagerAdapter extends FragmentPagerAdapter {
    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        Fragment fragment = null;
        switch (position) {
        case 0:
            fragment = new SettingsFragment(0);
            break;
        case 1:
            fragment = new SettingsFragment(1);
            break;
        case 2:
            fragment = new SettingsFragment(2);
            break;
        // and so on...
        default:
            fragment = new SettingsFragment(0);
        }
        return fragment;
    }

    @Override
    public int getCount() {
        return 6;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
        case 0:
            return getString(R.string.title_section1).toUpperCase();
        case 1:
            return getString(R.string.title_section2).toUpperCase();
        // and so on ...
        }
        return null;
    }
}

Settingsfragment:(就像在谷歌示例中一样)

public static class SettingsFragment extends Fragment {

    int position = 0;

    public SettingsFragment(int position) {
        this.position = position;

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView;
        if (position == 0) {
            rootView = inflater.inflate(R.layout.main0, container, false);
            main0(rootView);
            return rootView;

        } else if (position == 1) {
            rootView = inflater.inflate(R.layout.main1, container, false);
            main1(rootView);
            return rootView;
        } else if (position == 2) {
            rootView = inflater.inflate(R.layout.main2, container, false);
            main2(rootView);
            return rootView;
        } 
                    //...and so on...
    }

}

相关页面:main4():

public static void main4(View rootView) {
    subView = rootView;
    new setAdapterTask().execute();
}

以及加载寻呼机的异步任务:

private static class setAdapterTask extends AsyncTask<Void, Void, Void> {
    protected Void doInBackground(Void... params) {
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        mViewPagerSub = (CustomViewPager)subView.findViewById(R.id.pagerSub);
        mViewPagerSub.setAdapter(mSectionsPagerAdapterSub);
        mViewPagerSub.refresh();
    }
}

CustomViewPager.refresh():

public void refresh() {
       getAdapter().notifyDataSetChanged();
}

我尝试使用这样的 PageListener 来模拟滚动,但没有效果:

private static class PageListener extends SimpleOnPageChangeListener {
        public void onPageSelected(int position) {
            currentPage = position;
            if (mViewPagerSub != null && currentPage == 3) {
                mViewPagerSub.setCurrentItem(5);
            } else if (mViewPagerSub != null && currentPage == 4) {
                mViewPagerSub.setCurrentItem(0);
            }
            if (mViewPagerSub != null)
                mViewPagerSub.refresh();
        }
}
4

1 回答 1

1

回答我自己的问题:这成功了:

private static class PageListener extends SimpleOnPageChangeListener {
   public void onPageSelected(int position) {
      currentPage = position;
      if(currentPage==4 && mViewPagerSub!=null){
         new setAdapterTask().execute();
      }
   }
}
于 2013-02-08T11:59:18.797 回答