Android

recyclerview 滚动监听

2017-05-24  本文已影响36人  tea9

RecyclerView的滚动事件OnScrollListener研究
http://www.open-open.com/lib/view/open1477623086381.html

滚动参数
http://www.tuicool.com/articles/7RzUjaR
http://www.open-open.com/lib/view/open1477623086381.html

//此方法只是关闭软键盘
private void hintKbTwo() {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
if(imm.isActive()&&getCurrentFocus()!=null){
if (getCurrentFocus().getWindowToken()!=null) {
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
}

mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (newState == SCROLL_STATE_DRAGGING) {
hintKbTwo();
}
}

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            int juli = getScollYDistance();
            int firstVisibleItem = mManager.findFirstVisibleItemPosition();

            int section = getSectionForPosition(firstVisibleItem);
            int nextSection = getSectionForPosition(firstVisibleItem + 1);
            int nextSecPosition = getPositionForSection(+nextSection);
            if (firstVisibleItem != lastFirstVisibleItem) {
                ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) titleLayout.getLayoutParams();
                params.topMargin = 0;
                titleLayout.setLayoutParams(params);
                title.setText(mData.get(getPositionForSection(section)).getSortLetters());
            }
            if (nextSecPosition == firstVisibleItem + 1) {
                View childView = recyclerView.getChildAt(0);
                if (nextSecPosition == firstVisibleItem + 1) {
                    int titleHeight = titleLayout.getHeight();
                    int bottom = childView.getBottom();
                    ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) titleLayout.getLayoutParams();
                    if (bottom < titleHeight) {
                        float pushedDistance = bottom - titleHeight;
                        params.topMargin = (int) pushedDistance;
                        titleLayout.setLayoutParams(params);
                    } else {
                        if (params.topMargin != 0) {
                            params.topMargin = 0;
                            titleLayout.setLayoutParams(params);
                        }
                    }
                }
            }
            lastFirstVisibleItem = firstVisibleItem;

        }
    });
上一篇下一篇

猜你喜欢

热点阅读