我正在开发一个应用程序,但遇到了一些代码问题。实际上,我在 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/