【发布时间】:2015-08-13 08:05:49
【问题描述】:
我一直在尝试使用此处的少量指导:http://android-developers.blogspot.co.uk/2015/05/android-design-support-library.html 和此处的项目:https://github.com/chrisbanes/cheesesquare 来实现带有 RecyclerView 的 CollapsingToolbar,我目前具有以下布局:
<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());
我已经在没有折叠工具栏的情况下对其进行了测试,它可以正常滚动
但它不会滚动,即使列表比可见部分长。我做错了什么?
【问题讨论】:
-
我尝试将您的布局与简单的 RecyclerView 一起使用,效果很好。问题可能出在项目数量上,也可能出在适配器的实现上?
-
它可以在没有工具栏的情况下工作,就像我说的那样。适配器和物品很好。你能上传你的项目吗?
-
其实,为了测试我修改了Cheesesquare项目,我唯一做的就是把details view中的NestedScrollingView替换成项目中的RecyclerView,就成功了。
-
下面的答案解释了原因。你得到了更新的 build.gradle,我没有更新我的。似乎被许多人忽视了。
-
很高兴它现在可以工作了!我知道并非所有滚动视图都有效,例如 ListView。最新更新必须包含与此相关的更改。
标签: java android xml android-appcompat android-design-library