尽管预加载内容,ViewPager仍然很懒惰

时间:2014-08-29 11:55:12

标签: android android-viewpager android-webview

我有一个带有WebView内容的ViewPager。因为它不是很流畅,所以我尝试在显示它们之前创建并加载所有WebView。在我的onCreateView中,我只是从数组中取出预加载的WebView并将其添加到布局中。但是当我第一次访问页面时,它仍然非常懒惰。在那之后,它运作顺利。我还试图用setOffscreenPageLimit()设置我的寻呼机,但它没有效果。我还能做什么?

编辑: 这是我的活动:

public class TutorialActivity extends FragmentActivity {
/**
 * The number of pages (wizard steps) to show in this demo.
 */
private static final int NUM_PAGES = 5;

/**
 * The pager widget, which handles animation and allows swiping horizontally to access previous
 * and next wizard steps.
 */
private ViewPager mPager;

/**
 * The pager adapter, which provides the pages to the view pager widget.
 */
private PagerAdapter mPagerAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.activity_tutorial);

    // Instantiate a ViewPager and a PagerAdapter.
    mPager = (ViewPager) findViewById(R.id.pager);
    mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
    mPager.setAdapter(mPagerAdapter);
    mPager.setOffscreenPageLimit(NUM_PAGES);
}


/**
 * A simple pager adapter that represents 5 {@link TutorialScreenSlidePageFragment} objects, in
 * sequence.
 */
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
    public ScreenSlidePagerAdapter(FragmentManager fm) {
        super(fm);
        TutorialScreenSlidePageFragment.init(TutorialActivity.this);
    }

    @Override
    public Fragment getItem(int position) {
        return TutorialScreenSlidePageFragment.create(position);
    }

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

和我的Pagefragement:

public class TutorialScreenSlidePageFragment extends Fragment {
/**
 * The argument key for the page number this fragment represents.
 */
public static final String ARG_PAGE = "page";

private static final String assetPath = "file:///android_asset/tutorial/";

/**
 * The fragment's page number, which is set to the argument value for
 * {@link #ARG_PAGE}.
 */
private int mPageNumber;

static WebView[] webviews;

/**
 * Factory method for this fragment class. Constructs a new fragment for the
 * given page number.
 */
public static TutorialScreenSlidePageFragment create(int pageNumber) {
    TutorialScreenSlidePageFragment fragment = new TutorialScreenSlidePageFragment();
    Bundle args = new Bundle();
    args.putInt(ARG_PAGE, pageNumber);
    fragment.setArguments(args);
    return fragment;
}

public TutorialScreenSlidePageFragment() {
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mPageNumber = getArguments().getInt(ARG_PAGE);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // Inflate the layout containing a title and body text.
    ViewGroup rootView = (ViewGroup) inflater.inflate(
            R.layout.fragment_tutorial_screen_slide_page, container, false);

    ViewGroup vg = (ViewGroup) rootView.findViewById(R.id.webviewContainer);
    vg.addView(webviews[mPageNumber]);

    return rootView;
}

/**
 * Returns the page number represented by this fragment object.
 */
public int getPageNumber() {
    return mPageNumber;
}

// preload data
public static void init(Context context) {
    webviews = new WebView[5];

    for (int x = 0; x < 5; x++) {
        WebView webview = new WebView(context);
        webview.loadUrl(String.format("%sonboard%d.html", assetPath, x + 1));
        // Only disabled the horizontal scrolling:
        webview.getSettings().setLayoutAlgorithm(
                LayoutAlgorithm.SINGLE_COLUMN);
        webviews[x] = webview;
    }

大部分内容都是从谷歌示例中复制而来的。在我的PagerAdapter中,我尝试初始化所有网页浏览并加载他们的网址。

当我说它不顺畅时,我的意思是第一次从第1页到第2页没有平滑滚动。踩过所有页面后,它就像一个魅力。

0 个答案:

没有答案