【问题标题】:Layout Jitter when Toolbar hides while scrolling RecyclerView滚动 RecyclerView 时工具栏隐藏时的布局抖动
【发布时间】:2015-03-12 15:04:58
【问题描述】:

您好,我有一个带有工具栏、PageSlidingTab 和 ViewPager 的布局。在 ViewPager 中有一个带有 RecyclerView 的片段。我想在滚动 RecyclerView 时隐藏工具栏。我通过添加以下代码实现了它:

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


        }
    });

它可以正常工作,但在滚动时工具栏隐藏时会闪烁(如图所示)。我知道它的发生是因为布局调整大小。我该如何解决这个问题?请提出一个好的解决方案。

【问题讨论】:

    标签: android scroll android-viewpager android-recyclerview android-toolbar


    【解决方案1】:

    我遇到了同样的问题。我使用这个库解决了这个问题Android-ObservableScrollView

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多