KoordinatorLayout mit RecyclerView & CollapsingToolbarLayout

Lesezeit: 6 Minuten

Benutzer-Avatar
Kieron

Ich habe versucht, eine CollapsingToolbar mit einer RecyclerView zu implementieren, indem ich die kleine Menge an Anleitungen hier verwende: http://android-developers.blogspot.co.uk/2015/05/android-design-support-library.html und das Projekt hier: https://github.com/chrisbanes/cheesesquareund ich habe derzeit folgendes Layout:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/detail_backdrop_height"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:fitsSystemWindows="true">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        android:fitsSystemWindows="true"
        app:theme="@style/Toolbar"
        app:contentScrim="@color/primary"
        app:expandedTitleMarginStart="48dp"
        app:expandedTitleMarginEnd="64dp">

        <ImageView
            android:id="@+id/backdrop"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"
            android:fitsSystemWindows="true"
            app:layout_collapseMode="parallax" />

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            app:theme="@style/Toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_collapseMode="pin" />

    </android.support.design.widget.CollapsingToolbarLayout>

</android.support.design.widget.AppBarLayout>

<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

<android.support.design.widget.FloatingActionButton
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    app:layout_anchor="@id/appbar"
    app:layout_anchorGravity="bottom|right|end"
    android:src="https://stackoverflow.com/questions/30546812/@drawable/ic_directions"
    android:layout_margin="@dimen/fab_margin"
    android:clickable="true"/>

 </android.support.design.widget.CoordinatorLayout>

Die Quelle ist wie folgt:

    setContentView(R.layout.activity_details_image);
    final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    loadImage();
    CollapsingToolbarLayout collapsingToolbar =
            (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
    collapsingToolbar.setTitle(formatName(getIntent().getStringExtra("name")));
    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    ArrayList<DetailsAdapter.Detail> details = new ArrayList<DetailsAdapter.Detail>();
    details.add(new DetailsAdapter.Detail("Main Facilities", "Children's Play Area, Ecotricity Electric Vehicle Charging Point, Lucky Coin, Multi Faith Room (southbound only), Showers", R.drawable.ic_moto));
    details.add(new DetailsAdapter.Detail("Restaurants", "Eat & Drink Co., Burger King, Costa, West Cornwall Pasty Co. (northbound only), Greggs, Costa Express, Krispy Kreme", R.drawable.ic_moto));
    details.add(new DetailsAdapter.Detail("Shops", "WHSmith, M&S Simply Food, Fone Bitz, Cotton Traders, Ladbrokes (southbound only)", R.drawable.ic_moto));
    details.add(new DetailsAdapter.Detail("Motel", "Travelodge", R.drawable.ic_moto));
    details.add(new DetailsAdapter.Detail("Forecourt", "BP (with: LPG), Costa Express, Air1 AdBlue", R.drawable.ic_moto));
    DetailsAdapter mAdapter = new DetailsAdapter(this, details);
    recyclerView.setAdapter(mAdapter);
    recyclerView.setItemAnimator(new DefaultItemAnimator());

Ich habe es ohne die einklappbare Symbolleiste getestet und es läuft einwandfrei

Es scrollt jedoch nicht, selbst wenn die Liste länger als die sichtbaren Teile ist. Was habe ich falsch gemacht?

  • Ich habe versucht, Ihr Layout mit einem einfachen RecyclerView zu verwenden, es hat gut funktioniert. Das Problem könnte bei der Stückzahl liegen oder vielleicht bei der Umsetzung des Adapters?

    – Rausch

    31. Mai 2015 um 2:59 Uhr

  • Es funktioniert ohne die Toolbar, wie gesagt. Adapter und Zubehör sind in Ordnung. Könntest du dein Projekt hochladen?

    – Kieron

    31. Mai 2015 um 8:11 Uhr


  • Eigentlich habe ich zum Testen das Projekt Cheesesquare modifiziert, ich habe nur die NestedScrollingView in der Detailansicht durch die RecyclerView im Projekt ersetzt, es hat funktioniert.

    – Rausch

    31. Mai 2015 um 8:18 Uhr

  • Und die Antwort unten erklärt warum. Sie haben die aktualisierte build.gradle erhalten, ich habe meine nicht aktualisiert. Scheint von vielen übersehen zu werden.

    – Kieron

    31. Mai 2015 um 9:48 Uhr

  • Schön, dass es jetzt funktioniert! Mir ist bewusst, dass nicht alle Bildlaufansichten funktionieren, z. B. ListView. Das neuste Update muss hierfür relevante Änderungen enthalten.

    – Rausch

    31. Mai 2015 um 9:57 Uhr

Stellen Sie sicher, dass Sie verwenden com.android.support:recyclerview-v7:22.2.0

(Mit Version vor 22.2.0 hat es bei mir auch nicht funktioniert)

  • Das hat es für mich behoben. Ich bin sprachlos. Danke Roi!

    – EboMike

    31. Mai 2015 um 8:44 Uhr

  • Obwohl es jetzt funktioniert, ist mein RecyclerView nicht so groß, wie es sein sollte – es ist match_parent, aber es scheint unten genau so viel zu fehlen, wie die Symbolleiste hoch ist, wenn sie erweitert wird. Ist das ein weiteres Zeichen für eine ältere Bibliothek? Ich könnte eine andere Frage hervorbringen, die ich nur poste, falls Sie eine schnelle Antwort haben. Ich verwende im Wesentlichen das gleiche Layout wie oben.

    – EboMike

    31. Mai 2015 um 8:47 Uhr

  • Im Moment habe ich die RecyclerView mit layout_height=”wrap_content” und layout_marginBottom=”?attr/actionBarSize” (die Höhe der Symbolleiste) erstellt.

    – Roi Divon

    31. Mai 2015 um 8:55 Uhr

  • @RoiDivon Ich denke, du meintest layout_marginTop=”?attr/actionBarSize”. Aber wenn die App-Leiste nicht angezeigt wird (beim Scrollen), gibt es einen leeren Raum zwischen der Statusleiste und der RecyclerView. Könnte jemand dieses Problem beheben?

    – Moschik L

    17. Juni 2015 um 9:35 Uhr


  • 23.1.1 hier. Gleicher Fehler. Hat jemand einen Workaround gefunden? Das ist kein Spass.

    – Lukas Allison

    2. März 2016 um 5:20 Uhr

Ich bin wahrscheinlich 1 Jahr zu spät, um darauf zu antworten. Ich habe eine Lösung gefunden. Hier ist es, fügen Sie layout_marginBottom in der verschachtelten Bildlaufansicht hinzu:

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

<android.support.design.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:id="@+id/main_coord_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.android.minhnguyencv.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="256dp"
        android:id="@+id/appbar"
        android:theme="@style/AppTheme.AppBarOverlay"
        app:elevation="4dp">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapse_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags = "scroll|exitUntilCollapsed"
            app:contentScrim="@color/colorPrimary"
            android:fitsSystemWindows="true"
            app:expandedTitleTextAppearance="@android:color/transparent">

            <ImageView
                android:id="@+id/coverPhoto"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="fitXY"
                android:fitsSystemWindows="true"
                android:src="https://stackoverflow.com/questions/30546812/@drawable/coverphoto"
                app:layout_collapseMode="parallax"
                android:clickable="true" />

            <ImageView
                android:id="@+id/profilePhoto"
                android:layout_width="150dp"
                android:layout_height="150dp"
                android:layout_gravity="center_horizontal|bottom"
                android:scaleType="fitXY"
                android:fitsSystemWindows="true"
                app:layout_collapseMode="parallax"
                android:clickable="true" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:popupTheme="@style/AppTheme.PopupOverlay"
                app:layout_collapseMode = "pin" />

        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/main_nested_scroll_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity = "fill_vertical"
        android:layout_marginBottom="?attr/actionBarSize"
        app:layout_behavior = "@string/appbar_scrolling_view_behavior">

        <include layout="@layout/content_main" />

    </android.support.v4.widget.NestedScrollView>

    <include layout="@layout/floating_button_menu"/>

</android.support.design.widget.CoordinatorLayout>

  • Dies war der einzige Fix für mich, der 23.3.0 ausführte

    – Fillobotto

    20. September 2016 um 17:27 Uhr

  • dasselbe mit Support-Bibliotheksversion 25.3.1.

    – AlexKost

    19. Juni 2017 um 12:05 Uhr

  • Ich benutze Androidx und hatte das gleiche Problem. Der obige Code hat nicht funktioniert.

    – Madina Saidova

    12. März 2020 um 11:14 Uhr

1051170cookie-checkKoordinatorLayout mit RecyclerView & CollapsingToolbarLayout

This website is using cookies to improve the user-friendliness. You agree by using the website further.

Privacy policy