CollapsingToolbarLayout tidak membuka dan menutup

Saya mencoba menerapkan CollapsingToolbarLayout seperti berikut:

masukkan deskripsi gambar di sini

Tapi CollapsingToolbarLayout saya tidak pernah terbuka atau runtuh. Saya telah merujuk ke tutorial ini. Ini kode saya:

tata letak.xml

<?xml version="1.0" encoding="utf-8"?>
<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:background="#FF0"
    tools:context=".activities.TasksListActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:background="@android:color/transparent"
        android:fitsSystemWindows="true"
        app:elevation="0dp">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:expandedTitleGravity="top"
            app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:fitsSystemWindows="true"
                app:layout_collapseMode="pin" />

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

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

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/tasks_list_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:tint="@color/colorPrimary"
        app:srcCompat="@android:drawable/ic_input_add" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

Berikut ini kode javanya:

        toolbar = findViewById(R.id.toolbar);
        collToolbar = findViewById(R.id.collapsing_toolbar);

        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });
        collToolbar.setTitleEnabled(false);
        collToolbar.setTitle("This is title");

person Faisal Shaikh    schedule 29.10.2019    source sumber


Jawaban (2)


Ketinggian AppBarLayout Anda telah disetel ke 60dp. Ubah ke 120dp atau lebih besar dan atur judul ke toolbar sebelum mengatur toolbar sebagai supportActionBar

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".CollapsingToolbar">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:fitsSystemWindows="true"
        android:background="#FF0"
        app:elevation="0dp">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:expandedTitleGravity="bottom"
            app:expandedTitleTextAppearance="@style/TextAppearance.MaterialComponents.Headline5"
            app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:fitsSystemWindows="true"
                app:layout_collapseMode="pin" />

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

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

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/tasks_list_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="16dp"
        android:tint="@color/colorPrimary"
        app:srcCompat="@android:drawable/ic_input_add" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

Dalam file Java aktivitas Anda

        toolbar = findViewById(R.id.toolbar);
        collToolbar = findViewById(R.id.collapsing_toolbar);

        toolbar.setTitle("This is title");

        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });

masukkan deskripsi gambar di sini

Semoga ini membantu!

person Networks    schedule 29.10.2019

Atur tinggi AppBarLayout Anda ke sesuatu yang lebih besar - 60dp sangat dekat dengan tinggi toolbar standar 56dp.

<com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="60dp" <-- Change to something larger (i.e. 240dp)
    android:background="@android:color/transparent"
    android:fitsSystemWindows="true"
    app:elevation="0dp">
person PPartisan    schedule 29.10.2019
comment
Ini sebenarnya menyelesaikan masalah mengenai keruntuhan tetapi tetap saja, judulnya tetap di atas dan tidak turun saat digulir. - person Faisal Shaikh; 29.10.2019
comment
Coba ubah atribut app:expandedTitleGravity="top" Anda - person PPartisan; 29.10.2019
comment
Saya bilang modifikasi - ubah menjadi app:expandedTitleGravity="bottom", misalnya - person PPartisan; 29.10.2019
comment
tapi sudah app:expandedTitleGravity="top" di CollapsingToolbarLayout - person Faisal Shaikh; 29.10.2019