androidx viewpager2的使用详解
程序员文章站
2022-06-15 09:38:13
在Android 中,像常用的控件:TextView \ EditText \ Button\ImageView\ViewPager\RadioGroup\RadioButton\Switch\CheckBox等等这些大多数都是很有历史的控件了,我们接下来将一下一个全新的控件:ViewPager2在没退出androidx之前,android生态有些混乱,自从Google官方开始重视这一问题以后 就有了后续的jetpack和一系列的控件viewpager 我们一般是exends PageA.....
在Android 中,像常用的控件:TextView \ EditText \ Button\ImageView\ViewPager\RadioGroup\RadioButton\Switch\CheckBox等等
这些大多数都是很有历史的控件了,我们接下来将一下一个全新的控件:
ViewPager2
在没推出androidx之前,android生态有些混乱,自从Google官方开始重视这一问题以后 就有了后续的jetpack和一系列的控件
viewpager 我们一般是exends PageAdapter或者其他的适配器,完成不通类型之间页面滑动 A--->B---->C 默认会缓存2个页面的数据,有个缓存设计,也就是当前在B页面---C页面的数据会初始化,所以才有了懒加载(Fragment)
懒加载原理请移步---------> https://blog.csdn.net/qq_29769851/article/details/90712756
先看看ViewPager2的源码:
继承自ViewGroup
setAdapter
/**
* <p>Set a new adapter to provide page views on demand.</p>
*
* <p>If you're planning to use {@link androidx.fragment.app.Fragment Fragments} as pages,
* implement {@link androidx.viewpager2.adapter.FragmentStateAdapter FragmentStateAdapter}. If
* your pages are Views, implement {@link RecyclerView.Adapter} as usual.</p>
*
* <p>If your pages contain LayoutTransitions, then those LayoutTransitions <em>must</em> have
* {@code animateParentHierarchy} set to {@code false}. Note that if you have a ViewGroup with
* {@code animateLayoutChanges="true"} in your layout xml file, a LayoutTransition is added
* automatically to that ViewGroup. You will need to manually call {@link
* android.animation.LayoutTransition#setAnimateParentHierarchy(boolean)
* getLayoutTransition().setAnimateParentHierarchy(false)} on that ViewGroup after you inflated
* the xml layout, like this:</p>
*
* <pre>
* View view = layoutInflater.inflate(R.layout.page, parent, false);
* ViewGroup viewGroup = view.findViewById(R.id.animated_viewgroup);
* viewGroup.getLayoutTransition().setAnimateParentHierarchy(false);
* </pre>
*
* @param adapter The adapter to use, or {@code null} to remove the current adapter
* @see androidx.viewpager2.adapter.FragmentStateAdapter
* @see RecyclerView#setAdapter(Adapter)
*/
public void setAdapter(@Nullable @SuppressWarnings("rawtypes") Adapter adapter) {
final Adapter<?> currentAdapter = mRecyclerView.getAdapter();
mAccessibilityProvider.onDetachAdapter(currentAdapter);
unregisterCurrentItemDataSetTracker(currentAdapter);
mRecyclerView.setAdapter(adapter);
mCurrentItem = 0;
restorePendingState();
mAccessibilityProvider.onAttachAdapter(adapter);
registerCurrentItemDataSetTracker(adapter);
}
描述的意思 大概是使用RecyclerView 的 dapter,在了解了使用方法以后,于是就开始体验一下吧!
@FindView(R.id.splash_viewpage2)
ViewPager2 viewPager2;
设置滑动方向 ---> 左右或者上下
viewPager2.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL);
ViewPager2的Adapter
public class SplashAdapter extends RecyclerView.Adapter<SplashAdapter.SplashVH> {
private List<SplashImageBean> list;
public SplashAdapter(List<SplashImageBean> list) {
this.list = list;
}
@NonNull
@Override
public SplashVH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = ((LayoutInflater)parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE))
.inflate(R.layout.item_splash_wellcome,parent,false);
return new SplashVH(view);
}
@Override
public void onBindViewHolder(@NonNull SplashVH holder, int position) {
holder.iv_splash_image_center.setBackgroundResource(list.get(position).getIcon());
// holder.imageView.startCountDown();
}
@Override
public int getItemCount() {
return list == null ? 0 : list.size();
}
static class SplashVH extends RecyclerView.ViewHolder{
private ImageView iv_splash_image_center;
private CountDownView countDownView;
public SplashVH(@NonNull View itemView) {
super(itemView);
this.iv_splash_image_center = itemView.findViewById(R.id.iv_splash_image_center);
countDownView = itemView.findViewById(R.id.cdv_splash_number);
}
}
那么 跟之前的 ViewPager有些API变更 比如 addOnPageChange 现已经更换为:
registerOnPageChangeCallback
Code:
viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
@Override
public void onPageSelected(int position) {
if (position == list.size()-1){
btn_splash_start.setVisibility(View.VISIBLE);
btn_splash_start.startAnimation(animation);
}else{
btn_splash_start.setVisibility(View.GONE);
}
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
super.onPageScrolled(position, positionOffset, positionOffsetPixels);
}
@Override
public void onPageScrollStateChanged(int state) {
super.onPageScrollStateChanged(state);
}
});
本文地址:https://blog.csdn.net/qq_29769851/article/details/107407168
上一篇: 安卓 OkHttp请求失败的原因及解决