19

How to do scrolling effect like twitter when scroll Up hide viewpager tab (Home, Discover, activity). Or effect like facebook scrolling, while scroll up hide option view(status, photo, checkin) when scroll down show option view. Any example link will do please help.

NaiveBz
  • 834
  • 2
  • 8
  • 19

4 Answers4

6

Easy solution:

public abstract class OnScrollObserver implements AbsListView.OnScrollListener {

public abstract void onScrollUp();

public abstract void onScrollDown();

@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}

int last = 0;
boolean control = true;

@Override
public void onScroll(AbsListView view, int current, int visibles, int total) {
    if (current < last && !control) {
        onScrollUp();
        control = true;
    } else if (current > last && control) {
        onScrollDown();
        control = false;
    }

    last = current;
}

Usage:

listView.setOnScrollListener(new OnScrollObserver() {
        @Override
        public void onScrollUp() {

        }

        @Override
        public void onScrollDown() {

        }
    });
Vansuita Jr.
  • 1,949
  • 17
  • 18
3

EDIT: better, you have this library https://github.com/ksoichiro/Android-ObservableScrollView

You can look at this https://github.com/LarsWerkman/QuickReturnListView

Source: https://stackoverflow.com/a/25304575/244702

Community
  • 1
  • 1
Kevin Robatel
  • 8,025
  • 3
  • 44
  • 57
2

That's my own implementation: notice:

  • View to be hidden should be fixed height
  • We are not hiding the view by Visiblity.GONE
  • We are setting the final height to 0px

Here is the code:

    //Your view which you would like to animate
    final RelativeLayout yourViewToHide = (yourViewToHideativeLayout) findViewById(R.id.topWrapper);
    //The initial height of that view
    final int initialViewHeight = yourViewToHide.getLayoutParams().height;
    listView.setOnScrollListener(new AbsListView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {

        }

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
            //Try catch block for NullPointerExceptions
            try{
                //Here is a simple delay. If user scrolls ListView from the top of the screen to the bottom then continue
                if(firstVisibleItem % visibleItemCount == 0) {
                    //Here we initialize the animator, doesn't matter what values You will type in
                    ValueAnimator animator = ValueAnimator.ofInt(0, 1);
                    //if Scrolling up
                    if (fastScrollSB.getProgress() > view.getFirstVisiblePosition()){
                        //Getting actual yourViewToHide params
                        ViewGroup.LayoutParams params = yourViewToHide.getLayoutParams();
                        if (!animator.isRunning()) {
                            //Setting animation from actual value to the initial yourViewToHide height)
                            animator.setIntValues(params.height, initialViewHeight);
                            //Animation duration
                            animator.setDuration(500);
                            //In this listener we update the view
                            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                                @Override
                                public void onAnimationUpdate(ValueAnimator animation) {
                                    ViewGroup.LayoutParams params = yourViewToHide.getLayoutParams();
                                    params.height = (int) animation.getAnimatedValue();
                                    yourViewToHide.setLayoutParams(params);
                                }
                            });
                            //Starting the animation
                            animator.start();

                        }
                        System.out.println("Scrolling up!");
                    //If not scrolling
                    } else if (fastScrollSB.getProgress() == view.getFirstVisiblePosition()) {

                        System.out.println("Not Scrolling!");
                    //If scrolling down
                    } else if (fastScrollSB.getProgress() < view.getFirstVisiblePosition()){
                        //Getting actual yourViewToHide params
                        ViewGroup.LayoutParams params = yourViewToHide.getLayoutParams();
                        if (!animator.isRunning()) {
                            //Setting animation from actual value to the target value (here 0, because we're hiding the view)
                            animator.setIntValues(params.height, 0);
                            //Animation duration
                            animator.setDuration(500);
                            //In this listener we update the view
                            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                                @Override
                                public void onAnimationUpdate(ValueAnimator animation) {
                                    ViewGroup.LayoutParams params = yourViewToHide.getLayoutParams();
                                    params.height = (int) animation.getAnimatedValue();
                                    yourViewToHide.setLayoutParams(params);
                                }
                            });
                            //Starting the animation
                            animator.start();

                        }
                        System.out.println("Scrolling down!");

                    }
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    });`

Hope it fits your needs :)

0

there is no quick example for this. But what you can do is keep track which way you are scrolling and show or hide the view accordingly

For example get first visible position of the ListView keep track of this and if it is smaller then before you know you are scrolling up this way you can show the view. In case it is bigger then hide the view.

This is a simple approach in case you want to have more precise you need to work with onTouchListeners and y coordinates of the movement.

http://developer.android.com/reference/android/widget/ListView.html

QVDev
  • 1,093
  • 10
  • 17
  • Thanks for you replied. i will try to figure it out, if u find good example please do let me know. And do you have any idea how they do for the option view animation? when scroll down the option view will slowly scroll down as well – NaiveBz Feb 17 '14 at 09:50
  • You can do normal translate animation when showing or as mentioned above if you need more precise you need to use onTouchListeners. – QVDev Feb 17 '14 at 10:14
  • 1
    This might be answered here: http://stackoverflow.com/questions/15464649/android-listview-floating-first-row, which you can check out the sample app from: https://code.google.com/p/romannurik-code/source/browse/misc/scrolltricks/ – Robert Apr 02 '14 at 06:37