Layout Jitter when Toolbar hides while scrolling RecyclerView

僤鯓⒐⒋嵵緔 提交于 2019-12-23 01:41:35

问题


Hi , I have a layout with Toolbar, PageSlidingTab and ViewPager. Inside ViewPager there is a fragment with RecyclerView. I want to hide the Toolbar as i scroll the RecyclerView. I have achieved it by adding the following code :

 toolbar  = ((MyActivity)getActivity()).getToolbar();

    mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
        int toolbarMarginOffset = 0;

        private int dp(int inPixels){
            return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, inPixels, getActivity().getResources().getDisplayMetrics());
        }



        @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);
            toolbarMarginOffset += dy;
            if(toolbarMarginOffset>dp(56)){

                toolbarMarginOffset = dp(56);

            }

            if(toolbarMarginOffset<0){

                toolbarMarginOffset = 0;

            }


            ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams)toolbar.getLayoutParams();

            params.topMargin = -1*toolbarMarginOffset;

            toolbar.setLayoutParams(params);


        }
    });

It works fine as expected but while scrolling there is a flicker when the toolbar is hiding (As shown in image). I know its happening because of Layout resize. How can i fix this issue? Please suggest a good solution to this.


回答1:


I had the same problem. I solved the problem using this library Android-ObservableScrollView



来源:https://stackoverflow.com/questions/29013315/layout-jitter-when-toolbar-hides-while-scrolling-recyclerview

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