viewpager2的使用

1.viewpage2+tablayout

        fragments.add(new Fragment_PoolInfo());
        fragments.add(new Fragment_HashRate());
        fragments.add(new Fragment_SearchMiner());
        fragments.add(new Fragment_UserInfo());
        activityMainBinding.vpMain.setAdapter(new FragmentStateAdapter(this) {
            @Override
            public int getItemCount() {
                return fragments.size();
            }

            @NonNull
            @Override
            public Fragment createFragment(int position) {
                return fragments.get(position);
            }
        });
        activityMainBinding.vpMain.setOffscreenPageLimit(fragments.size());
        TabLayoutMediator tabLayoutMediator = new TabLayoutMediator(activityMainBinding.tabMain, activityMainBinding.vpMain, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                VpTableListBinding vpTableListBinding = DataBindingUtil.inflate(LayoutInflater.from(getBaseContext()), R.layout.vp_table_list, null, false);
                vpTableListBinding.ivTable.setImageResource(images[position]);
                vpTableListBinding.tvTable.setText(name[position]);
                vpTableListBinding.executePendingBindings();
                tab.setCustomView(vpTableListBinding.getRoot());
            }
        });
        tabLayoutMediator.attach();

2.viewpage2+viewpage2 不同方向的滑动

//自定义NestedScrollableHost
public class NestedScrollableHost extends FrameLayout {
    private int touchSlop;
    private float initialX;
    private float initialY;
    private ViewPager2 getParentViewPager() {
        ViewParent viewParent = getParent();
        if (!(getParent() instanceof View)) {
            viewParent = null;
        }

        View v;
        for(v = (View)viewParent; v != null && !(v instanceof ViewPager2); v = (View)viewParent) {
            viewParent = v.getParent();
            if (!(viewParent instanceof View)) {
                viewParent = null;
            }
        }

        View var2 = v;
        if (!(v instanceof ViewPager2)) {
            var2 = null;
        }

        return (ViewPager2)var2;
    }

    private View getChild() {
        return this.getChildCount() > 0 ? this.getChildAt(0) : null;
    }

    private boolean canChildScroll(int orientation, float delta) {
        int direction = -((int)Math.signum(delta));
        boolean var6=false;
        switch(orientation) {
            case 0:
                if(getChild()!=null){
                    var6= getChild().canScrollHorizontally(direction);
                }
                break;
            case 1:
                if(getChild()!=null){
                    var6= getChild().canScrollVertically(direction);
                }
                break;
            default:
                throw new IllegalArgumentException();
        }

        return var6;
    }

    public boolean onInterceptTouchEvent(@NotNull MotionEvent e) {
        handleInterceptTouchEvent(e);
        return super.onInterceptTouchEvent(e);
    }

    private void handleInterceptTouchEvent(MotionEvent e) {
        ViewPager2 viewPager2 = this.getParentViewPager();
        if (viewPager2 != null) {
            int orientation = viewPager2.getOrientation();
            if (canChildScroll(orientation, -1.0F) || this.canChildScroll(orientation, 1.0F)) {
                if (e.getAction() == MotionEvent.ACTION_DOWN) {
                    this.initialX = e.getX();
                    this.initialY = e.getY();
                    getParent().requestDisallowInterceptTouchEvent(true);
                } else if (e.getAction() == MotionEvent.ACTION_MOVE) {
                    float dx = e.getX() - this.initialX;
                    float dy = e.getY() - this.initialY;
                    boolean isVpHorizontal = orientation == 0;
                    float scaledDx = Math.abs(dx) * (isVpHorizontal ? 0.5F : 1.0F);
                    float scaledDy = Math.abs(dy) * (isVpHorizontal ? 1.0F : 0.5F);
                    if (scaledDx > (float)touchSlop || scaledDy > (float)touchSlop) {
                        if (isVpHorizontal == scaledDy > scaledDx) {
                            getParent().requestDisallowInterceptTouchEvent(false);
                        } else if (this.canChildScroll(orientation, isVpHorizontal ? dx : dy)) {
                            getParent().requestDisallowInterceptTouchEvent(true);
                        } else {
                            getParent().requestDisallowInterceptTouchEvent(false);
                        }
                    }
                }

            }
        }
    }

    public NestedScrollableHost(@NotNull Context context) {
        super(context);
        this.touchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
    }

    public NestedScrollableHost(@NotNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.touchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
    }
}

2.xml文件中使用

  
                                    

 

3.frament中使用

ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) framPoolinfoBinding.banner.getLayoutParams();
                params.leftMargin = getResources().getDimensionPixelSize(R.dimen.dp_15) * 2;
                params.rightMargin = params.leftMargin;
                CompositePageTransformer compositePageTransformer = new CompositePageTransformer();
                compositePageTransformer.addTransformer(new ScaleTransformer());
                compositePageTransformer.addTransformer(new MarginPageTransformer(getResources().getDimensionPixelSize(R.dimen.dp_10)));
                framPoolinfoBinding.banner.setPageTransformer(compositePageTransformer);


                final List images = new ArrayList<>();
                for (Bananer bananer : listRespon.getData()) {
                    images.add(bananer.getImage());
                }
                framPoolinfoBinding.banner.setOffscreenPageLimit(2);
                Vp_BannerAdapter vp_bannerAdapter = new Vp_BannerAdapter(getMContext(), images);
                framPoolinfoBinding.banner.setAdapter(vp_bannerAdapter);

你可能感兴趣的:(viewpager2的使用)