当前位置: 首页 > 知识库问答 >
问题:

使用回收器视图的协调器布局

吴伟志
2023-03-14

我一直在尝试使用此处的少量指导来实现带有RecyclerView的CollapsingToolbar:http://android-developers.blogspot.co.uk/2015/05/android-design-support-library.html 和这里的项目:https://github.com/chrisbanes/cheesesquare,我目前有以下布局:

<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>

来源如下:

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

我已经在没有折叠工具栏的情况下对其进行了测试,并且滚动正常

但是,即使列表比可见部分长,它也不会滚动。我做错了什么?

共有2个答案

萧德庸
2023-03-14

我可能晚了一年回答这个问题。我找到了一个解决方案。在这里,在嵌套滚动视图中添加layout_marginBottom:

<?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>
夔宏深
2023-03-14

确保您使用的是com.android.support:recyclerview-v7:22.2.0

(对于 22.2.0 之前的版本,它也对我不起作用)

 类似资料:
  • 当点击fab时,snack bar出现,为此我必须使用协调器布局将fab水平移动到snack bar上方。但现在回收器视图的前几个项目是不可见的。它只从项目3开始。能否有人帮助解决这一点和什么改变要做。提前谢谢!!

  • 当我尝试在回收器视图中选择一个元素时,不会调用OnBindViewWholder。我使用的是一个水平布局,所有元素最初都不显示(7个元素中有4个显示,当用户向右移动时,3个元素交替显示)。通常,当用户单击一个元素时,OnBindViewWholder应该被激发,但对我来说却没有发生。它只在初始化时触发。因为它没有激发,所以我不能单击回收器视图中的任何元素。也许和我的布局有关?我不确定 MyAdap

  • 我的布局结构如下: 当我将RecyclerView添加到布局时,NestedScrollView将位于AppBarLayout下,如下图所示: 我测试了一些解决这个问题的方法,比如将NestedScrollView放入FrameLayout,或者向NestedScrollView添加和属性,但这些方法无法解决这个问题。

  • 主要活动: 适配器:

  • 在其他回收器视图中有一个回收器视图。两者都需要垂直滚动。外部回收器视图滚动正常,但内部回收器视图滚动不正常。 这是代码: ViewAdapter如下: 我尝试了以下两种回收商观点,但都无法解决问题 也尝试了这个:

  • 我刚开始在firebase工作。我设法上传了文本和图像,但是,我无法检索要显示在回收器视图中的图像,只能检索文本。我用的是毕加索依赖。我已经包括了我的主要活动。java类,该类负责显示从问题“我的适配器”中的firebase检索的回收器视图项。java类和模型类。我相信,在我将图像URI上载到firebase存储时,我可能犯了没有存储图像URI的错误,因此适配器无法检索图像位置。我想这可能是因为我