【问题标题】:Android scroll up hide view and scroll down show view effect like twitterAndroid 向上滚动隐藏视图和向下滚动显示视图效果,如 twitter
【发布时间】:2014-02-17 08:53:01
【问题描述】:

如何在向上滚动隐藏viewpager选项卡(主页,发现,活动)时做类似twitter的滚动效果。或类似 facebook 滚动的效果,而向上滚动隐藏选项视图(状态、照片、签到)时,向下滚动显示选项视图。任何示例链接都可以,请提供帮助。

【问题讨论】:

    标签: android performance android-listview


    【解决方案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() {
    
            }
        });
    

    【讨论】:

    • 这可以应用于线性布局吗?
    • 不,在 ScrollView 中使用 LinearLayout。不要忘记为 ScrollView 设置 OnScrollObserver。
    • 我试过这个但是如何为 ScrollView 设置 OnScrollObserver ??
    【解决方案2】:
    【解决方案3】:

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

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

    希望它符合您的需求:)

    【讨论】:

    • 你还没有添加fastScrollSB :)
    【解决方案4】:

    这方面没有简单的例子。但是您可以做的是跟踪您滚动的方式并相应地显示或隐藏视图

    例如,获取 ListView 的第一个可见位置来跟踪它,如果它更小,那么在你知道你正在向上滚动之前,你可以显示视图。如果它更大,则隐藏视图。

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

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

    【讨论】:

    • 感谢您的回复。我会试着弄清楚,如果你找到好的例子,请告诉我。你知道他们如何处理选项视图动画吗?当向下滚动时,选项视图也会慢慢向下滚动
    • 你可以在显示的时候做普通的翻译动画,或者如上所述如果你需要更精确你需要使用onTouchListeners。
    • 这可能会在这里得到回答:stackoverflow.com/questions/15464649/…,您可以查看示例应用程序:code.google.com/p/romannurik-code/source/browse/misc/…
    猜你喜欢
    • 1970-01-01
    • 2016-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-09
    • 1970-01-01
    • 2013-11-14
    相关资源
    最近更新 更多