Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

解决SwipeRefreshLayout与ViewPager2 操作冲突造成不适的体验感 #140

Open
soapgu opened this issue May 5, 2022 · 0 comments
Open
Labels
problem problem or trouble 安卓 安卓

Comments

@soapgu
Copy link
Owner

soapgu commented May 5, 2022

  • 当SwipeRefreshLayout遇到ViewPager2

我们知道SwipeRefreshLayout是往下滑动触发刷新的
而ViewPager2(默认)是左右滑动来翻页的

当SwipeRefreshLayout遇到ViewPager2,则是体验灾难。
图片

  • 理论上的井水不犯河水!
    SwipeRefreshLayout是处理上下滑动操作,ViewPager2是处理左右滑动的,理论上大家个管个是完全不冲突的

  • 实际上的体验
    理想是丰满的,而现实是骨感的!事实上左右滑动确实会引起ViewPager2翻页,但是只要一点点的上下的位移就会引起翻页中断,理论上的水平垂直互动实际上并不容易实现!

  • 解决问题

这个问题已经放了一段时间了,现在基本上腾出手来清理各种犄角旮旯问题的时候了。
目标:
在滑动状态下,相关的事件不要“漏到”SwipeRefreshLayout里面去。

 @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mSwipe = view.findViewById(R.id.swipe_refresh);
        mSwipe.setColorSchemeResources(R.color.primary);

        this.spacePage = view.findViewById(R.id.space_page);
        this.bookingPage = view.findViewById(R.id.booking_page);
        this.visitorPage = view.findViewById(R.id.visitor_page);

this.spacePage.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
                setSwipeEnable(state);
            }
        });
        this.bookingPage.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
                setSwipeEnable(state);
            }
        });
        this.visitorPage.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
                setSwipeEnable(state);
            }
        });
    ...
   }

    /***
     * 设置刷新工具的状态
     * @param state 当时ViewPager2的滚动栏状态
     */
    private void setSwipeEnable( int state ){
        this.mSwipe.setEnabled( state == ViewPager2.SCROLL_STATE_IDLE );
    }
  1. 拿到ViewPager2,SwipeRefreshLayout控件
  2. 调用registerOnPageChangeCallback,实现onPageScrollStateChanged方法
  3. 根据state来设置SwipeRefreshLayout的enable状态

综合下来觉得这个方法最合适,直接把SwipeRefreshLayout控件封了,这样事件怎么都漏不到SwipeRefreshLayout的处理里面去了。
另外也不用在绑定里面处理,因为纯粹是View层的“内部矛盾”,不需要扩展到业务层

@soapgu soapgu added 安卓 安卓 problem problem or trouble labels May 5, 2022
@soapgu soapgu changed the title 解决SwipeRefreshLayout与ViewPager 操作冲突造成不适的体验感 解决SwipeRefreshLayout与ViewPager2 操作冲突造成不适的体验感 May 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
problem problem or trouble 安卓 安卓
Projects
None yet
Development

No branches or pull requests

1 participant