首页 > 解决方案 > 滚动自定义布局隐藏或显示

问题描述

我正在开发一个应用程序并卡在一些代码上。实际上,我在片段内使用搜索布局,并且在滚动时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);
}

我在滚动时得到空白。

标签: android

解决方案


将此代码添加到您的滚动视图中,并根据您视图中的依赖视图控制视图

在 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


                }

            }
        });


推荐阅读