0

I want when insert a new item at the first element and after notifyItemInsered it should scroll to the recently item added. It add the element to the first with success but recyclerView.scrollTo(); is not working.

Note: The recyclerView has a setNestedScrollView to false.

I tried to do this:

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

But it doesn't work probably.

my xml layout:

<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>
Ali Habbash
  • 777
  • 2
  • 6
  • 29

3 Answers3

0

You can try this:

mRecyclerView.smoothScrollToPosition(0);
Hossein Golshani
  • 1,847
  • 5
  • 16
  • 27
Audrius
  • 64
  • 1
  • 5
0

you may have to use this:

linearLayoutManager.scrollToPositionWithOffset(pos, 0);

or take a look here

Igor Romcy
  • 336
  • 4
  • 7
0

That is because probably when you call scroll to the position, adapter is not yet updated, so it does scroll to first position, but since adapter is not updated it looks like nothing happend. You can try:

mRecyclerView.postDelayed( {
    mRecyclerView.smoothScrollToPosition(0)
}, 100)

If you give main thread a little time(100ms) to process the data in adapter and than call it, it should work.

Torima
  • 157
  • 2
  • 13