简体   繁体   English

滚动停止时的回收器视图

[英]Recycler View on Scroll Stop

Basically I want my recyclerview to automatically scroll to a position where the item is not half shown.基本上我希望我的 recyclerview 自动滚动到 position,其中项目没有显示一半。 Like the one in googleplay.就像googleplay中的那个。

I have written a code我写了一段代码

public void scrollToVisible(){
    int firstVisibleItemPosition = ((LinearLayoutManager) recyclerView.getLayoutManager()).findFirstVisibleItemPosition();
    View view = recyclerView.getLayoutManager().getChildAt(0);
    if (firstVisibleItemPosition > 0 && view != null) {
        int offsetTop = view.getTop();
        if (firstVisibleItemPosition - 1 >= 0 && adapter.getItemCount() > 0) {
            ((LinearLayoutManager) recyclerView.getLayoutManager()).scrollToPositionWithOffset(firstVisibleItemPosition - 1, offsetTop);
        }
    }
}

The problem comes next.接下来问题来了。 I dont know where to put this code.我不知道把这段代码放在哪里。 I have a vague idea to put it when the recyclerview stops on scrolling but I've been searching for quite some time now and i cant find such a method.当 recyclerview 停止滚动时,我有一个模糊的想法,但我已经搜索了很长一段时间,但我找不到这样的方法。 when i put it on the onScroll some unexpected behavior comes out当我把它放在 onScroll 上时,出现了一些意想不到的行为

You may create a CustomRecyclerView extending RecyclerView 您可以创建一个CustomRecyclerView扩展RecyclerView

public class CustomRecyclerView extends RecyclerView {
@Override
public void onScrollStateChanged(int state) {
    super.onScrollStateChanged(state);

    // check if scrolling has stopped
    if (state == SCROLL_STATE_IDLE) {
         LinearLayoutManager linearLayoutManager = (LinearLayoutManager) getLayoutManager();
         // use code here
    }
}

If it maybe of any help to someone: 如果对某人有帮助:

    mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {

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

                    super.onScrolled(recyclerView, dx, dy);

                        Log.d("y value",String.valueOf(dy));
                    if (dy > 0) {                       

                       //scrolling up

                    } else {
// Scrolling down
                    }
                }

                @Override
                public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                    super.onScrollStateChanged(recyclerView, newState);

                    if (newState == AbsListView.OnScrollListener.SCROLL_STATE_FLING) {
                        // Do something
                        Log.e("SCROLL_STATE_FLING","SCROLL_STATE_FLING");
                    } else if (newState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
                        Log.e("SCROLLTOUCH_SCROLL","SCROLL_STATE_TOUCH_SCROLL");
                        //slideUp(party_view);
                        // Do something
                    } else if (newState==AbsListView.OnScrollListener.SCROLL_STATE_IDLE){
                        // Do something
                        //slideDown(party_view);
                        Log.e("SCROLL_STATE_IDLE","SCROLL_STATE_IDLE");
                    }
                }
            });

complete example with UI synchronization UI 同步的完整示例

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);

            Handler handler = new Handler(getMainLooper());

            if (newState == 0) {
                handler.removeCallbacks(MainActivity.this::hideFab);
                handler.postDelayed(MainActivity.this::showFab, 400);
            } else {
                handler.removeCallbacks(MainActivity.this::showFab);
                handler.postDelayed(MainActivity.this::hideFab, 100);
            }

        }
    });


private void hideFab() {
    addFile.hide();
    addText.hide();
    camera.hide();
}



private void showFab() {
    addFile.show();
    addText.show();
    camera.show();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM