java - 在 Fragment 中隐藏 SearchView
问题描述
当我滚动我的片段时,我想隐藏我的搜索视图。我使用 RecyclerView 检索我的项目并滚动它们。
我的片段实际上是这样的:
当我滚动时,我希望它隐藏,然后当我返回顶部时,我希望它显示。
这是我的片段的 xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".stock.StockFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.SearchView
android:id="@+id/search_view"
android:layout_width="341dp"
android:layout_height="34dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:background="@drawable/search_layout"
android:gravity="center"
app:iconifiedByDefault="false"
app:queryHint="Cerca Articolo">
</androidx.appcompat.widget.SearchView>
</LinearLayout>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/refresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycleViewStock"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</LinearLayout>
我试着把它放在回收站里,但不起作用,请帮忙!
解决方案
您可以NestedScrollView
用于滚动所有项目。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".stock.StockFragment">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<androidx.appcompat.widget.SearchView
android:id="@+id/search_view"
android:layout_width="341dp"
android:layout_height="34dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:background="@drawable/search_layout"
android:gravity="center"
app:iconifiedByDefault="false"
app:queryHint="Cerca Articolo">
</androidx.appcompat.widget.SearchView>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="@+id/refresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycleViewStock"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
</LinearLayout>
之后recyclerView.setNestedScrollingEnabled(false);
在java中设置以进行正确滚动。
推荐阅读
- python - 如何将 3D nii 多类蒙版图像文件转换为 2D .png 图像
- django - Django 3.1+: (fields.E180) SQLite 不支持 JSONFields
- android - 无法在 Kotlin 中制作通用项目
- bash - 如何明确故意分词?
- python - Keras:如何部分加载权重?
- javascript - ReactJS - 导航栏逻辑问题
- php - 从一对多关系 laravel 中只查询一行
- postgresql - 远程服务器工作时如何解决postgresql连接问题?
- c - PSET5 拼写器分段错误
- apache-spark - 无法解决 intellij 和 spark 2.2 中的错误