androidx viewpager2的使用詳解

在Android 中,像經常使用的控件:TextView \ EditText \ Button\ImageView\ViewPager\RadioGroup\RadioButton\Switch\CheckBox等等html

這些大多數都是頗有歷史的控件了,咱們接下來將一下一個全新的控件:android

ViewPager2緩存

在沒推出androidx以前,android生態有些混亂,自從Google官方開始重視這一問題之後 就有了後續的jetpack和一系列的控件app

viewpager 咱們通常是exends PageAdapter或者其餘的適配器,完成不通類型之間頁面滑動 A--->B---->C  默認會緩存2個頁面的數據,有個緩存設計,也就是當前在B頁面---C頁面的數據會初始化,因此纔有了懶加載(Fragment)ide

懶加載原理請移步---------> https://blog.csdn.net/qq_29769851/article/details/90712756this

 

先看看ViewPager2的源碼:url

 

繼承自ViewGroup.net

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,在瞭解了使用方法之後,因而就開始體驗一下吧!rest

@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);
    }
    
});

相關文章
相關標籤/搜索