get scroll Y of RecyclerView or Webview

纵然是瞬间 提交于 2019-12-21 04:08:30

问题


I have created a activity Webview content placed in Recyclerview. I want to get the scroll Y position when I scroll webview. I tried Webview.getScrollY(), Webview.getY(), RecyclerView.getScrollY(), RecyclerView.getY(),... but it do not work fine. I can't get current scroll Y. Is there any suggest for get scroll Y of Webview or RecyclerView ?


回答1:


Use a RecyclerView.OnScrollListener for the RecyclerView and a View.OnScrollChangeListener for the webview.

You'll have to keep track of the total scroll yourself, like this:

private int mTotalScrolled = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ...

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

                mTotalScrolled += dy;

            }
        });

    ...

}

private int getScrollForRecycler(){
    return mTotalScrolled;
}



回答2:


computeVerticalScrollOffset()is more convenient at this situation as @Pkmmte mentioned.

mTotalScrolled  = recyclerView.computeVerticalScrollOffset();


来源:https://stackoverflow.com/questions/32459696/get-scroll-y-of-recyclerview-or-webview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!