在滚动自定义布局上隐藏或显示

时间:2019-05-28 10:52:46

标签: android

我正在开发一个应用程序,并陷入了一些代码。实际上,我正在片段中使用搜索布局,并且在滚动RecyclerView时必须隐藏或显示该布局。我在滚动侦听器上使用了该功能,但在翻译时却显示了空白。您可以看到我的代码,然后我使用了缩放比例,但是没有得到任何预期的结果。

public void onScrolled(@NonNull RecyclerView view, int dx, int dy) {

    if (dy > 0) {
        if (!up)
            scaleView(searchLayout, searchLayout.getScaleY(), .0f);
        up=true;
        down=false;
    }
    else if (dy < 0) {
        if(!down)
        scaleView1(searchLayout, .0f, searchLayout.getScaleY());
        up=false;
        down=true;
    }

}

public void scaleView(View v, float startScale, float endScale) {

    Animation anim = new ScaleAnimation(
            1f, 1f, // Start and end values for the X axis scaling
            startScale, endScale, // Start and end values for the Y axis scaling
            Animation.RELATIVE_TO_SELF, 0f, // Pivot point of X scaling
            Animation.RELATIVE_TO_SELF, endScale); // Pivot point of Y scaling
    anim.setFillAfter(true); // Needed to keep the result of the animation
    anim.setDuration(1000);
    anim.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {
            searchLayout.setVisibility(View.GONE);
        }

        @Override
        public void onAnimationEnd(Animation animation) {
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    });
    v.startAnimation(anim);

}
public void scaleView1(View v, float startScale, float endScale) {
    Animation anim = new ScaleAnimation(
            1f, 1f, // Start and end values for the X axis scaling
            startScale, endScale, // Start and end values for the Y axis scaling
            Animation.RELATIVE_TO_SELF, 0f, // Pivot point of X scaling
            Animation.RELATIVE_TO_SELF, 0f); // Pivot point of Y scaling
    anim.setFillAfter(true); // Needed to keep the result of the animation
    anim.setDuration(1000);
    anim.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {
            searchLayout.setVisibility(View.VISIBLE);
        }

        @Override
        public void onAnimationEnd(Animation animation) {

        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    });
    v.startAnimation(anim);
}

滚动时出现空白。

1 个答案:

答案 0 :(得分:1)

将此代码添加到滚动视图中,并根据视图中的从属视图控制视图

在Recyclerview周围使用Nestedscroll视图

 scrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
            @Override
            public void onScrollChanged() {
               

                Rect scrollBounds = new Rect();
                scrollView.getHitRect(scrollBounds);
                if (yourView.getLocalVisibleRect(scrollBounds)) {
                                                                     // is your dependant view is in visible bounds
                    searchBar.setVisibility("Visibility.GONE");
                    
                                                                      //your animation

                } else {
                    searchBar.setVisibility("Visibility.VISISBLE");
                    
                                                                       //your animation


                }

            }
        });