繁体   English   中英

在 Fragment 中隐藏 SearchView

[英]Hide SearchView in Fragment

当我滚动我的片段时,我想隐藏我的搜索视图。 我使用 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 中进行正确滚动。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM