添加新项目后滚动到RecyclerView中的第一个元素

时间:2018-10-18 17:42:44

标签: android android-recyclerview android-nestedscrollview

我想要在第一个元素上插入新项目时,在notifyItemInsered之后,它应该滚动到最近添加的项目。 它将元素成功添加到第一个元素,但是recyclerView.scrollTo(); 不起作用。

注意:recyclerView的setNestedScrollView设置为false。

我试图这样做:

newsNestedScrollView.smoothScrollTo(newsCommentTitle.getScrollX(),newsCommentTitle.getScrollY());

但这可能行不通。

我的xml布局:

<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/content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/news_details_footer_background_color"
    android:focusableInTouchMode="true">

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

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/news_details_collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="280dp"
            android:fitsSystemWindows="true"
            app:contentScrim="?colorPrimary"
            app:expandedTitleTextAppearance="@android:color/darker_gray"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:id="@+id/news_details_main_image"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax"
                tools:src="@mipmap/news7_header_bg" />

            <View
                android:layout_width="match_parent"
                android:layout_height="140dp"
                android:layout_gravity="top"
                android:background="@drawable/scrim_up_to_down" />

            <View
                android:layout_width="match_parent"
                android:layout_height="140dp"
                android:layout_gravity="bottom"
                android:background="@drawable/scrim_down_to_up" />

            <TextView
                android:id="@+id/news_details_title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:layout_marginLeft="@dimen/spacing_large"
                android:layout_marginTop="@dimen/spacing_large"
                android:layout_marginRight="@dimen/spacing_large"
                android:layout_marginBottom="@dimen/spacing_xlarge"
                android:textColor="@android:color/white"
                android:textSize="20sp"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                tools:text="Google AI Defeats Human Go Champion" />

            <TextView
                android:id="@+id/news_details_time"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom|center_horizontal"
                android:layout_marginLeft="@dimen/spacing_large"
                android:layout_marginRight="@dimen/spacing_large"
                android:layout_marginBottom="@dimen/spacing_regular"
                android:textColor="@android:color/white"
                tools:text="15 min" />

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

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

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

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/news_details_nested_scroll"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/spacing_huge"
        android:background="@color/colorWhite"
        android:fillViewport="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
        <!--android:layout_marginBottom="56dp"-->

        <android.support.constraint.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:orientation="vertical">

            <TextView
                android:id="@+id/news_details_description"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:lineSpacingExtra="@dimen/spacing_middle"
                android:padding="@dimen/spacing_large"
                android:textSize="20sp"
                android:textStyle="bold"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                tools:text="sdkjsdfkjsdfjdfsk sfdjkfdsjsfk djfdsk sfdjk lfdjf dskjf fjjfdsfdj sdjkfl sdfdsklj sdfjklsfd" />

            <WebView
                android:id="@+id/news_details_content"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_margin="@dimen/spacing_regular"
                android:lineSpacingExtra="@dimen/spacing_middle"
                android:textSize="18sp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                 app:layout_constraintTop_toBottomOf="@+id/news_details_description"
                app:layout_constraintVertical_chainStyle="packed" />

            <!--<TextView-->
            <!--android:id="@+id/news_details_content"-->
            <!--android:layout_width="match_parent"-->
            <!--android:layout_height="wrap_content"-->
            <!--android:layout_marginEnd="16dp"-->
            <!--android:layout_marginTop="24dp"-->
            <!--android:layout_marginStart="16dp"-->
            <!--android:text="aasdkjasdkljasdklj k aksdjlkd klasjd kj dkajs kj das kjasklds klj as"-->
            <!--android:textSize="16sp"-->
            <!--app:layout_constraintEnd_toEndOf="parent"-->
            <!--app:layout_constraintTop_toBottomOf="@id/textView2"-->
            <!--app:layout_constraintStart_toStartOf="parent"-->
            <!--/>-->

            <!--<ImageView-->
            <!--android:id="@+id/imageVideo"-->
            <!--android:layout_width="0dp"-->
            <!--android:layout_height="200dp"-->
            <!--android:scaleType="centerCrop"-->
            <!--android:layout_marginTop="24dp"-->
            <!--android:src="@drawable/bg_polygon"-->
            <!--app:layout_constraintDimensionRatio="w,15:9"-->
            <!--app:layout_constraintEnd_toEndOf="parent"-->
            <!--app:layout_constraintHorizontal_bias="0.545"-->
            <!--app:layout_constraintStart_toStartOf="parent"-->
            <!--app:layout_constraintTop_toBottomOf="@id/news_details_content" />-->

            <!--<ImageView-->
            <!--android:layout_width="wrap_content"-->
            <!--android:layout_height="25dp"-->
            <!--app:layout_constraintBottom_toBottomOf="@id/imageVideo"-->
            <!--app:layout_constraintEnd_toEndOf="@id/imageVideo"-->
            <!--app:layout_constraintStart_toStartOf="@id/imageVideo"-->
            <!--app:layout_constraintTop_toTopOf="@id/imageVideo"-->
            <!--app:srcCompat="@drawable/ic_playback" />-->

            <!--<TextView-->
            <!--android:id="@+id/textView5"-->
            <!--android:layout_width="match_parent"-->
            <!--android:layout_height="wrap_content"-->
            <!--android:layout_margin="16dp"-->
            <!--android:text="asdl;asd asdkl;asd d askd;lask dask ;laksd lk"-->
            <!--android:textSize="16sp"-->
            <!--app:layout_constraintEnd_toEndOf="parent"-->
            <!--app:layout_constraintStart_toStartOf="parent"-->
            <!--app:layout_constraintTop_toBottomOf="@id/imageVideo"-->
            <!--/>-->

            <View
                android:id="@+id/above_comments_separator"
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_marginTop="@dimen/spacing_large"
                android:background="@android:color/darker_gray"
                app:layout_constraintTop_toBottomOf="@id/news_details_content" />

            <TextView
                android:id="@+id/news_details_comments_title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="@dimen/spacing_large"
                tools:text="Comment (3)"
                android:textSize="18sp"
                android:textStyle="bold"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/above_comments_separator" />

            <android.support.v7.widget.RecyclerView
                android:id="@+id/news_details_recycler_view"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_marginTop="@dimen/spacing_large"
                android:layout_marginBottom="@dimen/spacing_mlarge"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/news_details_comments_title"
                />

            <include
                android:id="@+id/news_details_load_more_progress_bar"
                layout="@layout/layout_progress_bar"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/spacing_regular"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/news_details_recycler_view" />

        </android.support.constraint.ConstraintLayout>

    </android.support.v4.widget.NestedScrollView>

    <RelativeLayout
        android:id="@+id/news_details_add_comment_section"
        android:layout_width="match_parent"
        android:layout_height="@dimen/spacing_huge"
        android:layout_gravity="bottom"
        android:background="@color/news_details_footer_background_color"
        android:focusableInTouchMode="true"
        android:visibility="visible">

        <android.support.design.widget.TextInputEditText
            android:id="@+id/news_details_comment_input"
            style="@style/NewsDetailsInput"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerVertical="true"
            android:layout_toStartOf="@+id/news_details_add_comment_button"
            android:gravity="center_vertical"
            android:hint="@string/write_comment_hint"
            android:inputType="text"
            android:padding="12dp" />

        <ImageButton
            android:id="@+id/news_details_add_comment_button"
            android:layout_width="@dimen/spacing_huge"
            android:layout_height="match_parent"
            android:layout_alignParentEnd="true"
            android:layout_centerVertical="true"
            android:background="@android:color/transparent"
            android:foreground="?selectableItemBackgroundBorderless"
            android:textColor="@color/colorAccent"
            app:srcCompat="@drawable/ic_send" />

        <Button
            android:id="@+id/news_details_edit_comment_button"
            android:layout_width="@dimen/spacing_huge"
            android:layout_height="match_parent"
            android:layout_alignParentEnd="true"
            android:layout_centerVertical="true"
            android:background="@android:color/transparent"
            android:foreground="?selectableItemBackground"
            android:textColor="@color/colorAccent"
            android:textAllCaps="true"
            android:text="@string/edit_button"
            android:visibility="gone"
            />

        <TextView
            android:id="@+id/news_details_sign_error"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="@string/comment_user_error"
            android:background="@color/news_details_footer_background_color"
            android:visibility="gone"
            android:textSize="12sp"
            />

    </RelativeLayout>

    <include
        android:id="@+id/news_details_included_error"
        layout="@layout/layout_error"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:visibility="gone" />

    <com.wang.avi.AVLoadingIndicatorView
        android:id="@+id/news_details_progress_bar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:visibility="gone"
        app:indicatorColor="@color/colorPrimary" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/news_details_share_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_marginBottom="@dimen/spacing_mhuge"
        android:visibility="gone"
        app:fabSize="normal"
        app:srcCompat="@drawable/ic_share" />

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

2 个答案:

答案 0 :(得分:0)

您可以尝试以下方法:

mRecyclerView.smoothScrollToPosition(0);

答案 1 :(得分:0)

您可能必须使用此:

linearLayoutManager.scrollToPositionWithOffset(pos, 0);

or take a look here