美文网首页Android
recyclerview 滚动监听

recyclerview 滚动监听

作者: tea9 | 来源:发表于2017-05-24 17:17 被阅读36次

    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;
    
            }
        });

    相关文章

      网友评论

        本文标题:recyclerview 滚动监听

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