31

I have searched a lot regarding my query and none of them was useful. I have a recyclerview and some static data inside a scroll view which is inside a root parentlayout as show below.

I have set -

scrollview.scrollto(0,0);

because everytime i open the activity it jumps to the recyclerview firstitem and it skips out the static data above the recyclerview.

recyclerview.setNestedScrollingEnabled(false); 
recyclerview.setfocusable(false);

for smoothscroll.

the problem is with-

layoutmanager.scrollToPositionWithOffset(pos,0);

it is not working. I have set the aboveline after setting adapter to recyclerview. Also tried with NestedScrollView but in vain.

although I have used

layoutmanager.scrollToPosition(pos);

For those who skip the code, i have set match_parent to ScrollView and fillviewport to true.

Here is my layout.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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/bottomsheet"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.inkdrops.khaalijeb.BrandCouponActivity">

    <RelativeLayout
        android:id="@+id/inclusionviewgroup"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <static data/>

        <ScrollView
            android:id="@+id/scrollviewmain"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/one"
            android:fillViewport="true"
            android:scrollbars="none"
            android:layout_above="@+id/donelayout">


            <staticdata/>

                <TextView
                    android:id="@+id/dealstext"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/card1"
                    android:layout_marginTop="10dp"
                    android:layout_marginLeft="10dp"
                    android:textSize="16sp"
                    android:text="Deals &amp; Coupons"
                    android:textColor="#444444" />

                <RelativeLayout
                    android:id="@+id/recyclerlayout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/dealstext"
                    android:layout_marginTop="5dp"
                    android:layout_marginLeft="8dp"
                    android:layout_marginRight="8dp"
                    android:background="@color/colorbackground">

                    <android.support.v7.widget.RecyclerView
                        android:id="@+id/coupon_rec_view"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="@color/colorbackground"
                        android:visibility="visible"/>


                </RelativeLayout>

                <statisdata>


            </RelativeLayout>

        </ScrollView>

        include layout="@layout/activity_coupons"
            android:visibility="gone"
            />

    </RelativeLayout>

</RelativeLayout>
Nirav Ranpara
  • 15,268
  • 4
  • 40
  • 57
Aman Verma
  • 2,465
  • 4
  • 21
  • 41
  • Could you post some of your android code as well? Are you sure you're referencing the right `layoutManager`? Not the `RecyclerView`'s but the `ScrollView`'s right? – Felix Guo Jul 24 '17 at 21:35
  • You want to retain scroll postion on rotation or just programmatically want to scroll to certain position? – akshay_shahane Jul 25 '17 at 20:39
  • paste your adapter and related activity/fragment code as well @akshay_shahane. – Radhey Jul 26 '17 at 11:01

5 Answers5

34

Need to scroll ScrollView by getting top position of RecyclerView's child :

float y = recyclerView.getY() + recyclerView.getChildAt(selectedPosition).getY();    
scrollView.smoothScrollTo(0, (int) y);
user1025013
  • 125
  • 1
  • 9
Vaibhav Jani
  • 12,018
  • 10
  • 60
  • 71
0

you can use scroll view method to reach your desired locations as:

scrollView.smoothScrollTo(int x, int y);
Pratik Pitale
  • 1,036
  • 1
  • 10
  • 25
-1

I know I'm answering pretty late but if you get an auto scroll issue, you need to add android:descendantFocusability="blocksDescendants" in your RelativeLayout. So, your RealtiveLyout tag will look like this:

<RelativeLayout
    android:id="@+id/inclusionviewgroup"
    android:layout_width="match_parent"
    android:descendantFocusability="blocksDescendants"
    android:layout_height="match_parent">
Abhi
  • 1,589
  • 1
  • 14
  • 24
-2

Step 1: Add this line for selecting that position in the recyclerview

  adaptercat.setSelectedItem(Integer.parseInt(dealtypeid));

Step 2: Once check below constructor by getting current position

  public CatgerotyAdapter(Context context, ArrayList<HashMap<String, String>> arraylist,String selectedPosition) {
        this.context = context;
        this.data = arraylist;
        resultp = new HashMap<>();
        currentItemPos=Integer.parseInt(selectedPosition)-1;
    }

Step 3: And this function also in adapter

  public void setCurrentItem(int item)
    {
        this.currentItemPos = item;
    }

Last step 4. Add this function outside adapter in your class

 private void onPagerItemClick2(Integer tag)
{
    adaptercat.setCurrentItem(tag);
    catsp.setAdapter(adaptercat);
}

Pust this line in your listview item click listner

  ((ClassName) context).onPagerItemClick2(position);

That position will be selected when will you open listview ..

Venkatesh
  • 972
  • 11
  • 24
-3

Use this method scrollToPosition (int position) this is work for me. hope this will help you.

        mMessagesRecyclerView = (RecyclerView) findViewById(R.id.messagesRecyclerView);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setStackFromEnd(true);

        mMessagesRecyclerView.setLayoutManager(layoutManager);
        mMessagesAdapter = new MessagesAdapter();
        mMessagesRecyclerView.setAdapter(mMessagesAdapter);

        mMessagesRecyclerView.scrollToPosition(your position);

this is my layoutstructer

enter image description here

yathavan
  • 153
  • 1
  • 12