ViewPager2 应用和DiffUtil的解释
目前ViewPager2对Fragment支持只能FragmentStateAdapter,FragmentStateAdapter在遇到预加载时,
只会创建Fragment对象,不会把Fragment真正的加入到布局中,所以自带懒加载效果。
DiffUtil 可以局部刷新
ViewPager2 应用
//ViewPager2 基于 RecyclewView 实现,复制了RecyclewView的很多功能。
public class ViewPager2Activity extends AppCompatActivity {
private ViewPager2 viewPager2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_pager2);
viewPager2 = findViewById(R.id.view_page2);
MyAdapter myAdapter = new MyAdapter();
//滑动方向 ORIENTATION_VERTICAL 竖向 默认是横向
//viewPager2.setOrientation(ViewPager2.ORIENTATION_VERTICAL);
//是否禁止用户滑动
//viewPager2.setUserInputEnabled(false);
//优化了预加载 ViewPager2.OFFSCREEN_PAGE_LIMIT_DEFAULT = -1 不进行预加载
//viewPager2.setOffscreenPageLimit(ViewPager2.OFFSCREEN_PAGE_LIMIT_DEFAULT);
//优化了页面切换动画,还可以多个叠加-->PageTransformer
CompositePageTransformer compositePageTransformer = new CompositePageTransformer();
compositePageTransformer.addTransformer(new ScaleInTransformer());
compositePageTransformer.addTransformer(new MarginPageTransformer(10));
viewPager2.setPageTransformer(compositePageTransformer);
//一屏多页 展示多个pagerView
RecyclerView recyclerView = (RecyclerView) viewPager2.getChildAt(0);
int padding = 80;
recyclerView.setPadding(padding, 0, padding, 0);
recyclerView.setClipToPadding(false);
viewPager2.setCurrentItem(1);
viewPager2.setAdapter(myAdapter);
//滑动监听事件
viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
super.onPageScrolled(position, positionOffset, positionOffsetPixels);
//滑动中
Log.d("ViewPager2_T", "onPageScrolled-->" + "positionOffset-->" + positionOffset);
Log.d("ViewPager2_T", "onPageScrolled-->" + "positionOffsetPixels-->" + positionOffsetPixels);
}
@Override
public void onPageSelected(int position) {
super.onPageSelected(position);
}
@Override
public void onPageScrollStateChanged(int state) {
super.onPageScrollStateChanged(state);
}
});
}
//自定义适配器
private String[] colors = new String[]{"#CCFF99", "#41F1E5", "#8D41F1", "#FF99CC"};
class MyAdapter extends RecyclerView.Adapter<MyAdapter.PagerViewHolder> {
@NonNull
@Override
public PagerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View root = LayoutInflater.from(ViewPager2Activity.this).inflate(R.layout.item_text, parent, false);
return new PagerViewHolder(root);
}
@Override
public void onBindViewHolder(@NonNull PagerViewHolder holder, int position) {
holder.bindColor(position % colors.length);//无限循环
}
@Override
public int getItemCount() {
return Integer.MAX_VALUE; //无限循环
//return colors.length;
}
public class PagerViewHolder extends RecyclerView.ViewHolder {
//https://blog.csdn.net/m0_61544080/article/details/120642180
private TextView mTextView;
public PagerViewHolder(@NonNull View itemView) {
super(itemView);
mTextView = itemView.findViewById(R.id.tv_text);
}
private void bindColor(int index) {
mTextView.setText(index + "");
mTextView.setBackgroundColor(Color.parseColor(colors[index]));
}
}
}
//模拟滑动 --先开启 viewPager2.beginFakeDrag(); 一般配合按钮使用
private void fakeDragBy(ViewPager2 viewPager2) {
viewPager2.beginFakeDrag();
if (viewPager2.fakeDragBy(100f)) {
viewPager2.endFakeDrag();
}
}
//防止没有重复使用fragment-->https://mp.weixin.qq.com/s/MOWdbI5IREjQP1Px-WJY1Q
/*
public class MyPagerAdapter extends FragmentStatePagerAdapter {
SparseArray<Fragment> registeredFragments = new SparseArray<Fragment>();
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return 0;
}
@Override
public Fragment getItem(int position) {
return MyFragment.newInstance(...);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment fragment = (Fragment) super.instantiateItem(container, position);
registeredFragments.put(position, fragment);
return fragment;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
registeredFragments.remove(position);
super.destroyItem(container, position, object);
}
public Fragment getRegisteredFragment(int position) {
return registeredFragments.get(position);
}
}
*/
}
item_text.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/tv_text"
android:layout_width="match_parent"
android:layout_height="280dp"
android:layout_gravity="center_horizontal"
android:background="@color/colorPrimaryDark"
android:gravity="center"
android:textColor="#000000"
android:textSize="30sp" />
</LinearLayout>
DiffUtil
//不重写
DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new DiffUtil.Callback() {
@Override
public int getOldListSize() {
return oldList.size();
}
@Override
public int getNewListSize() {
return newsList.size();
}
@Override
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
return false;
}
@Override
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
return false;
}
});
//会自动计算新老数据集的差异,并根据差异情况,自动调用更新数据
diffResult.dispatchUpdatesTo(myAdapter);
//重写调用
DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new DiffCallBack(mDatas, newDatas), true);
diffResult.dispatchUpdatesTo(mAdapter);
class DiffCallBack extends DiffUtil.Callback {
private List<String> oldData, newsData;
public DiffCallBack(List<String> old, List<String> news) {
this.oldData = old;
this.newsData = news;
}
//旧数据
@Override
public int getOldListSize() {
return oldData != null ? oldData.size() : 0;
}
@Override
public int getNewListSize() {
return newsData != null ? newsData.size() : 0;
}
//比较新旧数据内的属性是否有相同的
@Override
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
return false;
}
//判断两个item内容(对象数据)是否有相同
@Override
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
return false;
}
}