web-dev-qa-db-fra.com

Coordonnateur Mise en page avec RecyclerView & CollapsingToolbarLayout

J'ai essayé d'implémenter un CollapsingToolbar avec un RecyclerView en utilisant la petite quantité de conseils ici: http://Android-developers.blogspot.co.uk/2015/05/Android-design-support-library. html et le projet ici: https://github.com/chrisbanes/cheesesquare , et j'ai actuellement la mise en page suivante:

<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="@drawable/ic_directions"
    Android:layout_margin="@dimen/fab_margin"
    Android:clickable="true"/>

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

La source est la suivante:

    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());

Je l'ai testé sans la barre d'outils réductrice et le défilement est parfait

Cependant, il ne défile pas, même lorsque la liste est plus longue que les parties visibles. Qu'est ce que j'ai mal fait?

47
Kieron

Assurez-vous que vous utilisez com.Android.support:recyclerview-v7:22.2.

(Avec la version antérieure à 22.2.0, cela ne fonctionnait pas non plus pour moi)

63
Roi Divon

je suis probablement 1 an de retard pour répondre à cette question. J'ai trouvé une solution. La voici: ajoutez layout_marginBottom dans la vue de défilement imbriquée:

<?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="@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>
6
minh nguyen