1

I have an activity where I have set NavigationDrawer and works fine. This activity hosts a fragment where I have used:

setHasOptionsMenu(true);

To enable the menu. I have added a SearchView and override onCreateOptionsMenu():

@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.menu, menu);
    MenuItem searchItem = menu.findItem(R.id.search);
    //Do some search stuff
}

And everything works fine. This the layout file of my fragment:

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

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <ProgressBar
        android:id="@+id/progress_bar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

Now I want to collapse the toolbar while I'm scrolling through the RecyclerView. I know I can add an AppBarLayout and inside it a Toolbar in my layout file, but is the any possibility to achieve the same behaviour with the existing one in a ConstraintLayout?

Jorn
  • 252
  • 1
  • 15
  • The animations won't be as smooth and you'll be reinventing the wheel. Better to just add an AppBarLayout and a Toolbar. Also, consider CoordinatorLayout if you'll be adding the appbarlayout – denvercoder9 Sep 16 '19 at 08:11
  • Maybe this will help. https://www.journaldev.com/13927/android-collapsingtoolbarlayout-example – rminaj Sep 16 '19 at 08:13
  • @sonnet Thank you for that. All examples I've found are using a `CoordinatorLayout`. Is this the best way? If I would still want to achieve this with a `ConstraintLayout`, how should I do? – Jorn Sep 16 '19 at 08:15
  • @Jorn AFAIK, `CoordinatorLayout` is the only native view that got the functionality that you are looking for. If you want to fully hide the `toolbar`, you'll need an in-depth knowledge about android development to make one. – rminaj Sep 16 '19 at 08:20

3 Answers3

1

With CoordinatorLayout:

The best thing would have been to use a CoordinatorLayout. A similar question here should be able to help you out.

Since you want to achieve that with ConstraintLayout:

You would have to create an OnScrollListener class for your RecyclerView. The class would have an onScrolled() method which would have parameters - dx, dy, the amounts of horizontal and vertical scrolls. Here is a link on how you can follow through with.

Hope it helps :)

Mayokun
  • 1,084
  • 1
  • 8
  • 20
0

You can do this with the help of CordinaterLayout.Kindly Check out this answer.

Hide AppBar when scrolling down

umer farooq
  • 183
  • 2
  • 7
  • Thank you but my question is related to a `ConstraintLayout`. Can I achieve the same thing with it? If yes, how? – Jorn Sep 16 '19 at 08:16
  • Yes ,you can achieve the same the with the ConstraintLayout. 1: Add ParentLayout of CordinateLayout of your ConstraintLayout 2: Use can also use android.support.design.widget.CollapsingToolbarLayout 3: Enable nestedscroll view of your rescyclerView. – umer farooq Sep 16 '19 at 08:21
  • This could be a comment. Please don't just answer just to post a link to another existing solution – denvercoder9 Sep 16 '19 at 08:25
  • @umerfarooq I don't want to use an `AppBarLayout` with a ` Toolbar` in .XML file, I just want to use the existing one. Is it even possible? – Jorn Sep 16 '19 at 08:31
0
 <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  xmlns:app="http://schemas.android.com/apk/res-auto">

<androidx.core.widget.NestedScrollView
  android:layout_width="match_parent"
  android:layout_height="match_parent">

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
 </androidx.core.widget.NestedScrollView>

 <ProgressBar
    android:id="@+id/progress_bar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

 </androidx.constraintlayout.widget.ConstraintLayout>

Use this layout in fragment (Wrap Recyclerview in Nestedscrollview).

In main layout use AppbarLyaout inside Coordinator layout Like this

  <androidx.coordinatorlayout.widget.CoordinatorLayout 
    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:id="@+id/DashCoor"
        android:fitsSystemWindows="true"
        tools:context=".Activities.DashboardActivity">


        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">


            <com.google.android.material.appbar.CollapsingToolbarLayout
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:visibility="gone"
                app:expandedTitleMarginStart="48dp"
                app:expandedTitleMarginEnd="64dp"
                app:layout_scrollFlags="scroll|enterAlways|snap"
                >
                ​


               //Layout to be collpased

            </com.google.android.material.appbar.CollapsingToolbarLayout>

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:titleTextColor="@color/white"
                android:fitsSystemWindows="true"
                android:minHeight="?attr/actionBarSize"
                app:theme="@style/ThemeOverlay.AppCompat.Light"
                app:layout_scrollFlags="scroll|enterAlways|snap"
                />

         </com.google.android.material.appbar.AppBarLayout>

         </androidx.coordinatorlayout.widget.CoordinatorLayout>

Hope you solve it :)

MohanKumar
  • 960
  • 10
  • 26