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);
}
我在滚动时得到空白。
解决方案
将此代码添加到您的滚动视图中,并根据您视图中的依赖视图控制视图
在 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
}
}
});
推荐阅读
- error-handling - 在默认流上启动内核是否会“捕获”其执行错误?
- python - QLabel 和自动换行:如何以逗号为基础换行(与空格)
- android - Xamarin- MainActivity.OnNewIntent 在单击时应用程序处于后台时使用错误的参数调用推送通知
- vba - 连接到雪花的 VBA 生成错误
- sql-server - 在 SQL 中生成每日总和的最有效方法?
- python - 在与某些参数匹配的对象列表中查找值的总和
- c++ - 指向常量值的 const 指针示例
- jquery - 如何将特定代码添加到我的链接中?jQuery
- reactjs - React debounce search 在初始化之前无法访问'updateSearchQuery'
- ajax - 如何在基于算法的无限滚动中防止重复