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文件中使用

  <com.example.xnpoolapp.ui.custom.NestedScrollableHost
                                    android:layout_width="match_parent"
                                    android:layout_height="@dimen/dp_144"
                                    android:clipChildren="false"
                                    android:layout_marginTop="@dimen/dp_47">
                                    <androidx.viewpager2.widget.ViewPager2
                                        android:id="@+id/banner"
                                        android:layout_width="match_parent"
                                        android:layout_height="match_parent"
                                        android:clipChildren="false"/>

 </com.example.xnpoolapp.ui.custom.NestedScrollableHost>

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<String> 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);
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom阅读 2,719评论 0 3
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,510评论 0 17
  • 这是谷歌官方给我们提供的一个兼容低版本安卓设备的软件包,里面包囊了只有在安卓3.0以上可以使用的api。而view...
    Ten_Minutes阅读 5,756评论 1 19
  • Application 标签 android:allowTaskReparenting android:allow...
    Shawn_Dut阅读 7,886评论 2 61
  • 葛大爷说过,二十一世纪什么最贵:人才。可以说企业的核心竞争力所在的两个地方一个是拥有难以复制的企业运营系统,另外一...
    LvJack阅读 207评论 0 0