代码之家  ›  专栏  ›  技术社区  ›  Aaron Chang

Android recyclerview新添加的项目被appbarlayout阻止

  •  1
  • Aaron Chang  · 技术社区  · 9 年前

    我在android上做这个应用,注意到当我在顶部的recyclerview中添加一个新项目时,该项目将被appbarlayout阻止。 app screenshot here

    例如,如果我添加一个新项目,它将显示为“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"/>
    
    4 回复  |  直到 9 年前
        1
  •  1
  •   vipul mittal    9 年前

    在顶部添加新项目后,调用 mRecyclerView.smoothScrollToPosition(0);

        2
  •  0
  •   Adnan Amjad    9 年前

    呼叫 notifyItemChanged(position); 在适配器中添加项目后

        3
  •  0
  •   Programming Pirate    9 年前
    <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>
    

    我有一个类似的问题,但这一个为我解决了它…请试着让我知道…我只在一个设备中以纵向模式检查了它。

    将回收器视图置于appbarlayout下方,然后关闭坐标布局标记

    如果您不需要,请删除图像视图。。

        4
  •  0
  •   Mahendra Chhimwal    9 年前

    添加项目时,RecyclerView不会自动滚动到新项目位置。添加新项目时,您需要务实地滚动它。AppbarLayout不是罪魁祸首。只需按照代码行将RView滚动到新项目的位置。

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

    在您的情况下,只需滚动到零位置。