Android recyclelerview新添加的项目已被appbarlayout

时间:2016-03-06 06:47:07

标签: android android-recyclerview android-appbarlayout

我在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 个答案:

答案 0 :(得分:1)

在顶部添加新项目后,请致电try { num1 = Float.parseFloat(e1.getText()+""); } catch (NumberFormatException e) { // do something else }

答案 1 :(得分:0)

在适配器中添加项目后调用notifyItemChanged(position);

答案 2 :(得分: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>

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

将您的回收站视图放在appbarlayout下方,然后关闭坐标布局标记

如果您没有必要,请删除imageview ..

答案 3 :(得分:0)

添加项目时,RecyclerView不会自动滚动到新项目位置。添加新项目时,需要以实际方式滚动它。 AppbarLayout不是罪魁祸首。只需按照以下代码行将RView滚动到新项目的位置即可。

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

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

相关问题