androidscrollandroid-recyclerviewshow-hide

View flicking while hiding/showing on scroll in recycler view


I have linear layout at the bottom and I want to hide that view on scroll up and show on scroll down. I was able to achieve that with scroll listener on recycler view . But there is one problem, when you are scrolling slow view is flickering (showing and hiding fast).

This is my code

bottom = (LinearLayout) getActivity().findViewById(R.id.linerabottom);
    recycleList.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            if (dy > 0) {
                bottom.setVisibility(View.GONE);

            } else {
                bottom.setVisibility(View.VISIBLE);
            }

        }
    });

Can you help me to find out what is the issue ?


Solution

  • This is normal, because your dy at some point of time fluctuates between dy >= 0 and dy < 0. If you want to achieve sort of quick return view, you should bound it to something like this:

     recycleList.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                mTotalDy += dy;
                if (dy > 0 && mTotalDy >= bottom.getHeight()) {
                    bottom.setVisibility(View.GONE);
    
                } else if(recyclerView.getScrollState() == RecyclerView.SCROLL_STATE_IDLE && bottom.getVisiblity() == View.GONE) {
                    bottom.setVisibility(View.VISIBLE);
                    mTotalDy = 0;
                }
    
            }
        });