SwipeRefreshLayout+RecyclerView冲突的几种解决方案

来源:互联网 发布:杭州交警网络教育平台 编辑:程序博客网 时间:2024/05/18 01:28
场景1

一种是SwipeRefreshLayout+RecyclerView在同一个界面的,
可以通过重写swipe、或者重写recycler控件,来解决。
这也是比较容易的

  • 方案1:在监听方法里判断,当RecyclerView可见的item位于第一个的时候,使swipeRefresh获取焦点
recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener(){          @Override          public void onScrolled(RecyclerView recyclerView, int dx, int dy) {              int topRowVerticalPosition =                      (recyclerView == null || recyclerView.getChildCount() == 0) ? 0 : recyclerView.getChildAt(0).getTop();              swipeRefreshLayout.setEnabled(topRowVerticalPosition >= 0);          }          @Override          public void onScrollStateChanged(RecyclerView recyclerView, int newState) {              super.onScrollStateChanged(recyclerView, newState);          }      }); 
  • 方案2: 重写RecyclerView,
public class FixedRecyclerView extends RecyclerView {    public FixedRecyclerView(Context context) {        super(context);    }    public FixedRecyclerView(Context context, AttributeSet attrs) {        super(context, attrs);    }    public FixedRecyclerView(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);    }    @Override    public boolean canScrollVertically(int direction) {        // check if scrolling up        if (direction < 1) {            boolean original = super.canScrollVertically(direction);            return !original && getChildAt(0) != null && getChildAt(0).getTop() < 0 || original;        }        return super.canScrollVertically(direction);    }}
  • 方案3: 重写swipeRefreshlayout
ublic class SwipeRefreshLayout extends android.support.v4.widget.SwipeRefreshLayout  {  public SwipeRefreshLayout(Context context)    {    super(context);    }  public SwipeRefreshLayout(Context context,AttributeSet attrs)    {    super(context,attrs);    }  @Override  public boolean canChildScrollUp()    {    View target=getChildAt(0);    if(target instanceof AbsListView)      {      final AbsListView absListView=(AbsListView)target;      return absListView.getChildCount()>0              &&(absListView.getFirstVisiblePosition()>0||absListView.getChildAt(0)              .getTop()<absListView.getPaddingTop());      }    else      return ViewCompat.canScrollVertically(target,-1);    }  }

场景2

还有一种是存在ViewPager的页面,
比如像这种,不单存在ViewPage,而且外层还加了一个滚动视图ScrollView。
需求是,当item处于顶端,再刷新。
也就是需要从Activity里监听Fragment中item的状态。

  • 方案:
    Activity中定义方法来获取Fragment滚动距离
    /*通过Fragmenbt的滚动距离回调    * 注意:这里只能处理当ScrollLayout完全隐藏后,Fragment的ScrollView才能开始滚动事件,    * 上滑的时候却优先执行ScrollLayout的方法     */    public void getonScroll(int i) {        srl.setEnabled(i<450)    }

Fragment中回调
如果RecyclerView外层还有ScrollView,还要处理滑动冲突
方法是给RecyclerView设置android:nestedScrollingEnabled="false"
属性,将滚动事件交给父滚动控件,并且在监听的时候监听scrollView的滚动事件

        scrollView.setOnScrollChangeListener(new View.OnScrollChangeListener() {            @Override            public void onScrollChange(View view, int i, int i1, int i2, int i3) {                Log.e("i======", "i:" + i + ",i1:" + i1 + ",i2:" + i2 + ",i3:" + i3);                if (i1>300) {                    activity.getonScroll(i1);                }            }        });

或者监听RecyclerView

        //这里获取不到recycler的监听事件,因为ScrollView冲突问题,在布局将滚动焦点交给了ScrollView处理        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {            @Override            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {                super.onScrollStateChanged(recyclerView, newState);            }            @Override            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {                super.onScrolled(recyclerView, dx, dy);                activity.getonScroll(dy);            }        });

想看详细源码可以参考:https://github.com/wapchief/imitationLOL
是一个仿掌上英雄联盟的练手项目,欢迎star。

阅读全文
0 0
原创粉丝点击