列表视图滚动时隐藏视图

时间:2017-09-08 09:08:15

标签: java android listview scroll

我试图在listview滚动时隐藏一个视图 很难使其正常工作 - 如果可能我需要帮助

我有结构 1)CustomActionBar(relativelayout) 2)视图(id = 1)需要通过滚动隐藏 3)listview的标题(它的相对布局与一些标签id = 2) 4)listview项目

这里是我在listview中检测自上而下滚动的代码

inboxListview.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if(motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    y = motionEvent.getY();
                    prev_y = y;
                    dy= y;
                    Logger.e("prev y = "+prev_y);
                }
                if (motionEvent.getAction() == MotionEvent.ACTION_MOVE) {
                    Logger.e("DY = "+dy);
                    dy = motionEvent.getY();
                    Logger.e("DY2 = "+dy);
                    if(dy<prev_y) {
                        Logger.e("move down 2");
                        float temp = dy-prev_y;
                        HomeActivity.changeListener.onScrollListenerHelper(1,temp);
                    }

                    if(dy>prev_y) {
                        Logger.e("move up 2");
                        float temp = dy-prev_y;
                        HomeActivity.changeListener.onScrollListenerHelper(2,temp);
                    }


                    prev_y = dy;
                    Logger.e("prev y new = "+prev_y);
                    return true;
                }
                return false;
            }
        });

    }

    private float y;
    private float dy;
    private float prev_y;

和我试图隐藏视图的回调

 @Override
    public void onScrollListenerHelper(final int direction, final float ScrollYOffset) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (direction == 1) {
                    //move view up
                    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) topLayout.getLayoutParams();
                    int margin = params.topMargin;
                    params.topMargin = (margin + (int) ScrollYOffset);
                    Logger.e("TOP MARGIN " + params.topMargin);
                    Logger.e("RESUTL: " + (topLayout.getHeight() - topBar.getHeight()));
                    if (params.topMargin <= -(topLayout.getHeight() - topBar.getHeight())) {
                        params.topMargin = -(topLayout.getHeight() - topBar.getHeight());
                    }
                    topLayout.setLayoutParams(params);
                }

                if (direction == 2) {
                    //move view down
                    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) topLayout.getLayoutParams();
                    int margin = params.topMargin;
                    params.topMargin = (margin + (int) ScrollYOffset);
                    Logger.e("TOP MARGIN2 " + params.topMargin);
                    if (params.topMargin >= topBar.getHeight()) {
                        params.topMargin = topBar.getHeight();
                    }
                    topLayout.setLayoutParams(params);
                }
            }
        });

    }

以某种方式开展工作。不好,但工作。当我滚动listview时出现问题 - 在屏幕上看起来像跳跃自上而下。 可以帮助我改进我的代码吗? prototype

1 个答案:

答案 0 :(得分:0)

我认为你正在寻找这个:

https://github.com/saulmm/CoordinatorExamples