2016-03-06 85 views
0

我在android上執行此應用程序,並注意到當我在頂部向recyclerview添加新項目時,該項目將被appbarlayout阻止。 app screenshot hereAndroid recyclerview新增項目被appbarlayout阻止

例如,如果我添加一個新項目,它將顯示爲「444」之上的第一個項目,但添加後我必須向下滾動才能看到它,否則它會被appbarlayout阻止。

有沒有解決方案?謝謝。

佈局文件:

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

    <android.support.v7.widget.Toolbar 
    android:id="@+id/toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="?attr/actionBarSize" 
    android:background="?attr/colorPrimary" 
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
    app:layout_scrollFlags="scroll|enterAlways" /> 

    <android.support.design.widget.TabLayout 
     android:id="@+id/tabs" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

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

<android.support.v7.widget.RecyclerView 
    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/crime_recycler_view" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context="com.largerexer.aaronc.criminalintent.CrimeListActivity" 
    tools:showIn="@layout/activity_fragment"/> 

回答

1

增加頂部的新項目後,調用mRecyclerView.smoothScrollToPosition(0);

+0

良好的解決方法! –

+0

這不完全是一個工作。數據更改後,Recycler視圖不會自動滾動。 –

+0

謝謝!所以每次改變時,我都必須手動滾動? –

0

呼叫notifyItemChanged(position);在適配器中添加項目之後

+0

我給它打了電話,並且該項目被添加,剛剛被應用程序欄阻止 –

+0

不要給您的recyclerView提供match_parent高度。讓它fill_parent –

+0

請評價我的答案。 –

0
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true"> 

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

     <android.support.design.widget.CollapsingToolbarLayout 
      android:id="@+id/collapsing_toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="@dimen/collapse_toolbar_height" 
      android:fitsSystemWindows="true" 
      app:contentScrim="?attr/colorPrimary" 
      app:expandedTitleMarginBottom="@dimen/expand_title_btm_margin" 
      app:expandedTitleMarginEnd="@dimen/expand_title_end_margin" 
      app:expandedTitleMarginStart="@dimen/expand_title_start_margin" 
      app:layout_scrollFlags="scroll|exitUntilCollapsed"> 

      <ImageView 
       android:id="@+id/header" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:background="@drawable/no_poster" 
       android:fitsSystemWindows="true" 
       android:scaleType="fitXY" 
       app:layout_collapseMode="parallax" /> 

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

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

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

我也有類似的問題,但這個解決它,我... PLZ嘗試,讓我知道......我只在一個設備中以縱向模式檢查過它。

地方appbarlayout下方的回收視圖,然後關閉協調佈局標籤

請刪除ImageView的,如果沒有必要,你..

+0

對不起,它不適用於我的設備 –

+0

出了什麼問題? –

+0

該項目仍然被阻止 –

0

添加項目時,RecyclerView不會自動滾動到新項目的位置。您需要在添加新項目時務實地進行滾動。 AppbarLayout在這裏不是罪魁禍首。只需按照以下代碼行將RView滾動到新項目的位置即可。

this.notifyItemRangeInserted(startingPosition, noOfItemsInserted);//or this.notifyItemInserted(newItemPosition); 
    ((LinearLayoutManager) mListRecyclerView.getLayoutManager()).scrollToPositionWithOffset(newItemPosition, 0); 

在你的情況只是滾動到位置零。

+0

我在分片代碼中調用了onRestoreInstanceState,有點想記住上次滾動的位置 –

+0

你有沒有試過上面的代碼行? –

相關問題