美文网首页第三方控件Android知识recyclerView
解决SpringView与AppBarLayout嵌套使用的上下

解决SpringView与AppBarLayout嵌套使用的上下

作者: 西凉小可 | 来源:发表于2016-10-26 18:07 被阅读2222次

    在用RecyclerView做列表时,如果需要下拉刷新和上拉加载,结合SpringView是一个很快速简单的解决方案,使用方法简单到没朋友。
    SpringView 的github地址:
    https://github.com/liaoinstan/SpringView

    那如果在列表顶部能够有可以折叠的头部内容效果,使用官方提供的CoordinatorLayout & CollapsingToolbarLayout & AppBarLayout 这种实现头部折叠是非常的灵活。如果不熟悉的可以参考这篇译文,翻译的还是很不错的:
    http://www.jianshu.com/p/f418bf95db2d

    问题来了

    ?当用这个官方的折叠组合用到列表头部时,我采用了如下的结构
    <CoordinatorLayout> <AppBarLayout> <CollapsingToolbarLayout> </CollapsingToolbarLayout> </AppBarLayout> <SpringView> <RecyclerView/> </SpringView> </CoordinatorLayout>

    当在AppBarLayout中实现了内部元素的折叠效果后,再实现SpringView的下接刷新和上拉加载,当滑动时会出现滑动冲突,下拉刷新会在头部未完全展开的情况下开始下拉刷新。

    �分析原因

    其实就是一个touch事件消耗占用的问题,我们让指定的View在错误的时机获取并消耗了本不该由它处理的touch事件。这里所出的问题就是AppBarLayout在未完全折叠和未完全展开的情况下,上下滑动的touch事件被SpringView提前消耗了。

    解决思路

    在AppBarLayout中的内容未完全展开的情况下,将SpringView设置enable=false,从而禁止其处理touch事件。当完全展开后设置enable=true.

    1 实现AppbarLayout的OnOffsetChangedListener事件监听

    先说一下这个事件的回调参数,
    @Override public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) { }
    verticalOffset指的是AppBarLayout在上下滑动时的偏移量,当完全展开的时候值是0
    那也就是当我们判断这个偏移量为0的时候才将SpringView的enable=true。不为0的时候设置为false。

    2 代码示例
       public class MainActivity extends AppCompatActivity implements AppBarLayout.OnOffsetChangedListener {
    @Override  
    public void onOffsetChanged(AppBarLayout appBarLayout, int i) {  
        springView.setEnabled(i == 0);  
    }  
    
    @Override  
         protected void onResume() {  
        super.onResume();  
        app_bar.addOnOffsetChangedListener(this);  
    }  
    
    @Override  
    protected void onPause() {  
        super.onPause();  
        app_bar.removeOnOffsetChangedListener(this);  
    }  }  
    

    ==============================================

    还有更为特别的一种情况

    如果是使用了viewpager+fragment,在fragment中使用了SpringView+RecyclerView。那这个时候头部折叠的实现控件及代码就写在了Activity中,而SpringView是在fragment中。
    这种情况下有一个比较好的处理方式是,将AppBarLayout的引用保存到全局,可以保存到application中,并在fragment中继承AppbarLayout的OnOffsetChangedListener事件,在指定的fragment显示在界面上时对事件进行注册。
    注意:在viewpager中,fragment的生命周期变得不是那么简单,11个生命周期函数并不是在预期的情况下发生回调,这个与viewpager的预加载和缓存机制有关。

    在这里我们用到fragment的以下这个方法:

        public void setUserVisibleHint(boolean isVisibleToUser)
    

    当该fragment显示或隐藏时,会回调,显示时isVisibleToUser=true ,反之 false。

    下面代码中还处理的一个小问题

    viewpager中的第一个fragment的OnOffsetChangedListener回调,回调太早,此时发现获取application时,getContext为空,所以在getContext为空的情况下做了个延时处理。

    实现代码
       public class NewsLinearListFragment extends Fragment implements AppBarLayout.OnOffsetChangedListener {
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        LogEx.L("setUserVisibleHint---" + isVisibleToUser + mParam1);
    
        /**
         * 这段代码是为了解决springview 和tabBarLayout中嵌套时上下滚动冲突
         */
        if (isVisibleToUser && this.getContext() != null) {
            MyApplication application = (MyApplication) this.getContext().getApplicationContext();
            if (application.appBarLayout != null) {
                application.appBarLayout.addOnOffsetChangedListener(this);
            } 
        }else if (isVisibleToUser && this.getContext() == null) {
                //viewpager中第一页加载的太早,getContext还拿不到,做个延迟
                new Handler().post(new Runnable() {
    
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        if (NewsLinearListFragment.this.getContext() != null) {
    
                            MyApplication application = (MyApplication) NewsLinearListFragment.this.getContext().getApplicationContext();
                            if (application.appBarLayout != null) {
                                application.appBarLayout.addOnOffsetChangedListener(NewsLinearListFragment.this);
                            }
                        }
                    }
                });
            }
    }
    
    ......
    
    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        LogEx.L("#######onOffsetChanged" + mParam1);
        springView.setEnable(verticalOffset == 0);
    }

    相关文章

      网友评论

      • return_toLife:这个有问题,如果recyclerview的item需要点击事件,appbar的事件监听貌似会拦截掉点击事件,还在需求解决方案
      • Silicon_Valley:楼主解决了数据未满一屏不允许上拉加载的问题吗
      • 木马上的骑士:那你的上拉加载呢,也会有冲突的问题,这种方式处理起来很难
      • d6dd94e91b30:那要是顶部也要刷新呢?
        西凉小可:你说的顶部刷新是指的下拉刷新吗?都有。

      本文标题:解决SpringView与AppBarLayout嵌套使用的上下

      本文链接:https://www.haomeiwen.com/subject/fstjuttx.html