recyclerView的学习(四)

男人要么穿上军装保家卫国,要么穿上西装运筹帷幄
最近看战狼二,看的是激动不已,今天也趁热打铁总结下recyclerVeiw中一些常见的小知识点。

如果在开发中要嵌套scrollview怎么办?

其实在开发中嵌套scrollview网上有的说重写LayoutManager等等,效果不是很明显,你可以去搜搜。现在给出的解决方案是
NestedScrollView 包裹RecyclerView。妈妈再也不用担心嵌套滑动的问题。然后在代码中加上:

my_recyler.setNestedScrollingEnabled(false); 

这就是解决嵌套recyclerVeiw的终极解决方法。

RecyclerView.OnScrollListener

(1)滚动事件分类

列表的滚动一般分为两种:

1.手指按下 -> 手指拖拽列表移动 -> 手指停止拖拽 -> 抬起手指
2.手指按下 -> 手指快速拖拽后抬起手指 -> 列表继续滚动 -> 停止滚动
上面的过程的状态变化如下:

1.静止 -> 被迫拖拽移动 -> 静止
2.静止 -> 被迫拖拽移动 -> 自己滚动 -> 静止
OnScrollListener

/**
 * An OnScrollListener can be added to a RecyclerView to receive messages when a scrolling event
 * has occurred on that RecyclerView.
 * 

* @see RecyclerView#addOnScrollListener(OnScrollListener) * @see RecyclerView#clearOnChildAttachStateChangeListeners() * */ public abstract static class OnScrollListener { /** * Callback method to be invoked when RecyclerView's scroll state changes. * * @param recyclerView The RecyclerView whose scroll state has changed. * @param newState The updated scroll state. One of {@link #SCROLL_STATE_IDLE}, * {@link #SCROLL_STATE_DRAGGING} or {@link #SCROLL_STATE_SETTLING}. */ public void onScrollStateChanged(RecyclerView recyclerView, int newState){} /** * Callback method to be invoked when the RecyclerView has been scrolled. This will be * called after the scroll has completed. *

* This callback will also be called if visible item range changes after a layout * calculation. In that case, dx and dy will be 0. * * @param recyclerView The RecyclerView which scrolled. * @param dx The amount of horizontal scroll. * @param dy The amount of vertical scroll. */ public void onScrolled(RecyclerView recyclerView, int dx, int dy){} }

onScrollStateChanged

回调的两个变量的含义:

  • recyclerView: 当前在滚动的RecyclerView
  • newState: 当前滚动状态
    newState 状态码
/**
 * The RecyclerView is not currently scrolling.(静止没有滚动)
 */
public static final int SCROLL_STATE_IDLE = 0;

/**
 * The RecyclerView is currently being dragged by outside input such as user touch input.
 *(正在被外部拖拽,一般为用户正在用手指滚动)
 */
public static final int SCROLL_STATE_DRAGGING = 1;

/**
 * The RecyclerView is currently animating to a final position while not under outside control.
 *(自动滚动)
 */
public static final int SCROLL_STATE_SETTLING = 2;


状态发生改变的时候会调用。接下来针对两种滑动过程具体onScrollStateChanged 是怎么打印信息的

  • 1.静止 -> 被迫拖拽移动 -> 静止
image.png
  • 2.静止 -> 被迫拖拽移动 -> 自己滚动 -> 静止
image.png
onScrolled()

回调的三个变量含义:
recyclerView : 当前滚动的view
dx : 水平滚动距离
dy : 垂直滚动距离

dx > 0 时为手指向左滚动,列表滚动显示右面的内容
dx < 0 时为手指向右滚动,列表滚动显示左面的内容
dy > 0 时为手指向上滚动,列表滚动显示下面的内容
dy < 0 时为手指向下滚动,列表滚动显示上面的内容
具体的日志信息就不打印了。
不过这个方法值判断recyclerVeiw有么有滑动到底部的方法

判断滑动到底部的方法

方法一:第一个可见item的位置 + 当前可见的item个数 >= item的总个数

loadingMoreListener = 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);

            if (dy > 0) //向下滚动
            {
                int visibleItemCount = mLinearLayoutManager.getChildCount();//该方法是得到当前可见的子视图的个数
                int totalItemCount = mLinearLayoutManager.getItemCount();//返回的是当前所有的item的个数
                int pastVisiblesItems = mLinearLayoutManager.findFirstVisibleItemPosition();//返回的是从顶部开始划出屏幕是个数,即已经可见的视图的个数

                if (!loading && (visibleItemCount + pastVisiblesItems) >= totalItemCount) {
                    loading = true;
                    loadMoreDate();
                }
            }
        }
};


image.png

方法二:通过canScrollVertically 来判断

loadingMoreListener = new RecyclerView.OnScrollListener() {
    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);
        if(!loading && !recyclerView.canScrollVertically(1)){
            loading = true;
            loadMoreDate();
        }
    }

    @Override
    public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        super.onScrolled(recyclerView, dx, dy);

//                if (dy > 0) //向下滚动
//                {
//                    int visibleItemCount = mLinearLayoutManager.getChildCount();
//                    int totalItemCount = mLinearLayoutManager.getItemCount();
//                    int pastVisiblesItems = mLinearLayoutManager.findFirstVisibleItemPosition();
//
//                    if (!loading && (visibleItemCount + pastVisiblesItems) >= totalItemCount) {
//                        loading = true;
//                        loadMoreDate();
//                    }
//                }
    }
};

方法三:

 RecyclerView.OnScrollListener listener = new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            Log.i("jimmy","onScrollStateChanged"+"滑动状态"+newState);
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
//            Log.i("jimmy","onScrolled"+"滑动dx的值="+dx+"滑动dy的值"+dy);

            if (dy > 0) //向下滚动
            {
//                int visibleItemCount = recyclerView.getLayoutManager().getChildCount();
                int totalItemCount = recyclerView.getLayoutManager().getItemCount();
//                int pastVisiblesItems = manager.findFirstVisibleItemPosition();
                int lastvisibleitemcount = manager.findLastVisibleItemPosition();
//                Log.i("jimmy","onScrolled==visibleItemCount的值=="+visibleItemCount+"totalItemCount的值为==="+totalItemCount+"pastVisiblesItems的值为==="+pastVisiblesItems);
                Log.i("jimmy","onScrolled===lastvisibleitemcount的值=="+lastvisibleitemcount);

//                if ((visibleItemCount + pastVisiblesItems) >= totalItemCount) {
//                    Log.i("jimmy","onScrolled滑动到底部了");
//                }
                if ((lastvisibleitemcount + 1) >= totalItemCount) {
                    Log.i("jimmy","onScrolled滑动到底部了");
                }
            }


        }
    };
image.png

最后还要说说adapter的封装问题,水平有限

推荐个学习链接:https://github.com/CymChad/BaseRecyclerViewAdapterHelper

http://www.jianshu.com/p/ce347cf991db

你可能感兴趣的:(recyclerView的学习(四))