Android 如何禁止ViewPager预加载
时间: 2023-11-16 08:00:45 浏览: 180
Android 中可以通过设置 ViewPager 的 setOffscreenPageLimit() 方法来控制它的预加载行为。setOffscreenPageLimit() 方法接收一个整型参数,代表左右两边预加载的页面数。如果我们想要禁止 ViewPager 的预加载行为,只需将 setOffscreenPageLimit() 方法的参数设置为 0 即可。具体实现代码如下:
```
viewPager.setOffscreenPageLimit(0);
```
通过这样的设置,ViewPager 就不会提前加载后面的页面了,而是在用户滑动到对应位置时才加载并显示。
相关问题
android ViewPager轮播图禁止预加载切换页面时出现相邻页面白屏手动添加相邻页面,完整代码
以下是一个简单的实现 Android ViewPager 轮播图禁止预加载且切换页面时不出现相邻页面白屏的示例代码:
```java
public class BannerActivity extends AppCompatActivity {
private ViewPager mViewPager;
private BannerAdapter mAdapter;
private List<BannerItem> mDataList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_banner);
initData();
initView();
}
private void initView() {
mViewPager = findViewById(R.id.view_pager);
mAdapter = new BannerAdapter(getSupportFragmentManager(), mDataList);
mViewPager.setAdapter(mAdapter);
mViewPager.setOffscreenPageLimit(1);
mViewPager.setCurrentItem(mDataList.size() * 1000);
// 添加页面切换监听器,禁止预加载
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
mAdapter.addExtraPages(position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
private void initData() {
mDataList = new ArrayList<>();
mDataList.add(new BannerItem(R.drawable.banner_1, "Banner 1"));
mDataList.add(new BannerItem(R.drawable.banner_2, "Banner 2"));
mDataList.add(new BannerItem(R.drawable.banner_3, "Banner 3"));
mDataList.add(new BannerItem(R.drawable.banner_4, "Banner 4"));
mDataList.add(new BannerItem(R.drawable.banner_5, "Banner 5"));
}
private static class BannerAdapter extends FragmentPagerAdapter {
private List<BannerItem> mDataList;
private SparseArray<Fragment> mExtraPages = new SparseArray<>();
BannerAdapter(FragmentManager fm, List<BannerItem> dataList) {
super(fm);
mDataList = dataList;
}
@Override
public int getCount() {
return mDataList.size() * 10000;
}
@Override
public Fragment getItem(int position) {
int index = position % mDataList.size();
return BannerFragment.newInstance(mDataList.get(index));
}
public void addExtraPages(int position) {
int index = position % mDataList.size();
if (mExtraPages.get(index) == null) {
mExtraPages.put(index, BannerFragment.newInstance(mDataList.get(index)));
}
int left = index > 0 ? index - 1 : mDataList.size() - 1;
if (mExtraPages.get(left) == null) {
mExtraPages.put(left, BannerFragment.newInstance(mDataList.get(left)));
}
int right = index < mDataList.size() - 1 ? index + 1 : 0;
if (mExtraPages.get(right) == null) {
mExtraPages.put(right, BannerFragment.newInstance(mDataList.get(right)));
}
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
Fragment fragment = (Fragment) super.instantiateItem(container, position);
mExtraPages.put(position % mDataList.size(), fragment);
return fragment;
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
mExtraPages.remove(position % mDataList.size());
super.destroyItem(container, position, object);
}
}
public static class BannerFragment extends Fragment {
private BannerItem mItem;
public static BannerFragment newInstance(BannerItem item) {
BannerFragment fragment = new BannerFragment();
Bundle args = new Bundle();
args.putParcelable("item", item);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mItem = getArguments().getParcelable("item");
}
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_banner, container, false);
ImageView imageView = view.findViewById(R.id.image_view);
TextView textView = view.findViewById(R.id.text_view);
imageView.setImageResource(mItem.getImageResId());
textView.setText(mItem.getTitle());
return view;
}
}
public static class BannerItem implements Parcelable {
private int imageResId;
private String title;
public BannerItem(int imageResId, String title) {
this.imageResId = imageResId;
this.title = title;
}
protected BannerItem(Parcel in) {
imageResId = in.readInt();
title = in.readString();
}
public static final Creator<BannerItem> CREATOR = new Creator<BannerItem>() {
@Override
public BannerItem createFromParcel(Parcel in) {
return new BannerItem(in);
}
@Override
public BannerItem[] newArray(int size) {
return new BannerItem[size];
}
};
public int getImageResId() {
return imageResId;
}
public String getTitle() {
return title;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(imageResId);
dest.writeString(title);
}
}
}
```
在上面的代码中,我们禁止了 ViewPager 的预加载,并手动添加了相邻页面。具体实现细节可以参考代码注释。
阅读全文