
Hi, I have a layout with Toolbar, PageSlidingTab and ViewPager. Inside the ViewPager there is a snippet with RecyclerView. I want to hide the toolbar when I view the RecyclerView. I achieved this 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 there is flicker when scrolling when the toolbar is hiding (as shown in the picture). I know this is due to the resizing of the layout. How can I fix this problem? Please suggest a good solution.
android scroll android-viewpager android-recyclerview android-toolbar
ASP Mar 12 '15 at 15:04 2015-03-12 15:04
source share