Android向上滚动隐藏视图并向下滚动显示视图效果,如twitter

时间:2014-02-17 08:53:02

标签: android performance android-listview

当向上滚动隐藏viewpager选项卡(主页,发现,活动)时,如何像推特一样滚动效果。或者像facebook滚动一样效果,当向下滚动显示选项视图时向上滚动隐藏选项视图(状态,照片,签入)。任何示例链接都可以提供帮助。

4 个答案:

答案 0 :(得分:5)

简易解决方案:

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() {

        }
    });

答案 1 :(得分:2)

答案 2 :(得分:1)

这是我自己的实施: 注意:

  • 要隐藏的视图应该是固定高度
  • 我们没有通过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();
            }
        }
    });`

希望它符合您的需求:)

答案 3 :(得分:0)

没有这方面的快速示例。但你可以做的是跟踪你滚动的方式,并相应地显示或隐藏视图

例如,获取ListView的第一个可见位置跟踪此信息,如果它更小,那么在您知道以这种方式向上滚动之前,您可以显示视图。如果它更大,则隐藏视图。

这是一个简单的方法,以防您需要更精确地使用onTouchListeners和运动的y坐标。

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