首页 > 解决方案 > 在 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>

我试着把它放在回收站里,但不起作用,请帮忙!

标签: javaxmlandroid-studio

解决方案


您可以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中设置以进行正确滚动。


推荐阅读