Android прокрутки вверх скрывать вид и прокручивать вниз показывают эффект вида, как твиттер

Как сделать эффект прокрутки, например, твиттер, при прокрутке вверх. Скрыть вкладку viewpager (Главная, Обнаружение, активность). Или эффект, подобный прокрутке в facebook, в то время как прокрутка вверх скрыть опцию просмотра (статус, фотография, проверка) при прокрутке вниз показать опцию. Любой пример ссылки будет делать, пожалуйста, помогите.

Ответы

Ответ 1

Простое решение:

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

Использование:

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

        }

        @Override
        public void onScrollDown() {

        }
    });

Ответ 3

нет быстрого примера для этого. Но вы можете следить за тем, как вы прокручиваете и показываете или скрываете представление соответственно.

Например, получить первую видимую позицию ListView отслеживать это, и если она меньше, то прежде чем вы узнаете, что прокручиваете этот путь, вы можете показать представление. Если он больше, то скройте представление.

Это простой подход, если вы хотите уточнить, что вам нужно работать с onTouchListeners и y координатами движения.

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

Ответ 4

Что моя собственная реализация: обратите внимание:

  • Скрытый вид должен быть фиксированным.
  • Мы не скрываем представление Visiblity.GONE
  • Мы устанавливаем окончательную высоту в 0px

Вот код:

    //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();
            }
        }
    });`

Надеюсь, что это соответствует вашим потребностям:)