[SOLVED] How to make SearchView always on top in NestedScrollView?

Issue

This Content is from Stack Overflow. Question asked by Arda

I need to fix the place of searchView to the top. Even if user scrolls searchView must stay on the top. My xml is like that:

<?xml version="1.0" encoding="utf-8"?>


<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/colorOffWhite"
tools:context=".HomeFragment"
android:fillViewport="true">

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <SearchView
        android:id="@+id/searchView"
        android:background="@color/colorBorder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"

        />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/horizontalCategoryRV"
        android:layout_width="412dp"
        android:layout_height="121dp"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/searchView" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_marginStart="5dp"
        android:gravity="center_vertical"
        android:text="@string/recent_products"
        android:textSize="18sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/horizontalCategoryRV">

    </TextView>


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv_dashboard_items"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView"
        android:layout_marginBottom="70dp"/>

</androidx.constraintlayout.widget.ConstraintLayout>


</androidx.core.widget.NestedScrollView>

How can I implement a solution for this? I tried it with toolbar but I don’t know if it’s right to that in a fragment. (This is a fragment’s layout and I’m on Kotlin)



Solution

you can try use property
android:iconifiedByDefault="false" on your XML


This Question was asked in StackOverflow by Arda and Answered by Acera It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?