Layout Jitter when Toolbar hides while scrolling R

2019-05-30 18:36发布

enter image description here

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条回答
Fickle 薄情
2楼-- · 2019-05-30 18:57

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

查看更多
登录 后发表回答