2015-05-30 121 views
46

我一直在嘗試使用少量的指導在這裏實現了RecyclerView一個CollapsingToolbar:http://android-developers.blogspot.co.uk/2015/05/android-design-support-library.html,這裏的項目:https://github.com/chrisbanes/cheesesquare,我目前有以下佈局:CoordinatorLayout與RecyclerView&CollapsingToolbarLayout

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

我沒有摺疊式工具測試,它滾動精細

但是,即使列表比可見部分長,它也不會滾動。我做錯了什麼?

+0

我試着用一個簡單的RecyclerView使用你的佈局,它工作的很好。問題可能與項目的數量有關,或者可能與適配器的實現有關? – razzledazzle

+0

它沒有工具欄,就像我說的。適配器和項目都很好。你能上傳你的項目嗎? – Kieron

+0

實際上,爲了測試我修改了項目Cheesesquare,我唯一做的事情是在項目中使用RecyclerView替換細節視圖中的NestedScrollingView,它工作正常。 – razzledazzle

回答

63

確保您使用com.android.support:recyclerview-v7:22.2.0

(用前22.2.0版本,它並沒有爲我工作,要麼)

+0

爲我修好了。我無語了。感謝Roi! – EboMike

+0

儘管它現在正在工作,但我的RecyclerView並不像它應該那麼高 - 它是match_parent,但它似乎從底部完全丟失,因爲擴展時工具欄很高。這是一個更老的圖書館的另一個跡象?我可能會產生另一個問題,只是發佈這個,以防你有一個快速的答案。我使用的佈局與上述基本相同。 – EboMike

+3

現在我所做的是使用layout_height =「wrap_content」和layout_marginBottom =「?attr/actionBarSize」(工具欄的高度)製作RecyclerView –

6

即時消息可能要晚1年才能回答。我找到了解決方案。這裏是在嵌套滾動視圖中添加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> 
+0

這是運行23.3.0 – fillobotto

+0

的唯一修復程序,與支持庫版本25.3.1相同。 – AlexKost

+0

適用於我。好的解決方案儘管我用ScrollView。 – InsParbo

相關問題