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

标签 android

我正在开发一个应用程序,但遇到了一些代码问题。实际上,我在 fragment 内使用搜索布局,在滚动 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);
}

滚动时出现空白。

最佳答案

将此代码添加到您的 ScrollView 中,并根据您 View 中的依赖 View 控制 View

在 Recyclerview 周围使用 Nestedscroll view

 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


                }

            }
        });

关于android - 在滚动自定义布局隐藏或显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56340609/

相关文章:

android - 为自定义 ListView 创建搜索 View

java - Android应用程序由于button.setOnClickListener(this)而关闭

android - 使用 Fabric SDK 自定义推文 View

android - 更改屏幕方向问题 : onCreate

android - Assets 看起来有颗粒感/像素化

android - 如果没有验证证明、 session 信息、临时证明或注册 ID,则无法创建 PhoneAuthCredential。(导致 firebase 新更新)

Android 创建对齐字段布局

android - 如何在用户更改首选项时在运行时禁用 Crashlytics/Fabric

android - 好的http : SSLProtocolException: SSL handshake terminated

java - Android扫描apk中的包中的文件