issue when calling scrollToPosition in recycleview adapter is missing up the layout

25 views Asked by At

I have a RecyclerView which contains image and a button when starting the app I need to navigate to a specific page so I have used manager.scrollToPosition(pageId);

when using scrollToPosition the images do not fit the full screen and half of the next image shown also but this behavior only occurs when I use scrollToPosition but if I use smoothScrollToPosition everything works fine and the images are resized to fill the whole screen and only a single item is shown.

If I manually scroll to the element everything is working great? I can't use smoothScrollToPosition to scroll to page number 300 that will take forever and it looks very ugly. I have attached an image to show the expected behavior and the issue:

Expected behavior: enter image description here

Issue when using scrollToPosition:

enter image description here

I have tried to fix this by a snapHelper and using calculateDistanceToFinalSnap to scroll to fix the issue and have the image on top without any extra space but on different phones I get different behavior.

Here is my code for navigating to the page:

    public void goToPage(int pageId)
{
    try {
        Log.d(TAG,"goToPage(String pageId) : go to page:"+pageId);
        if(pageId<=  606 && pageId>=0)
        {
         //   imageRecycleView.smoothScrollToPosition(pageId);
            LinearLayoutManager manager=(LinearLayoutManager) imageRecycleView.getLayoutManager();

            //manager.scrollToPositionWithOffset(page,0);
            manager.scrollToPosition(pageId);
            new Thread(()->{}).start();
            imageRecycleView.postDelayed(() -> {
                View view = manager.findViewByPosition(pageId);
                if (view == null) {
                    return;
                }

                int[] snapDistance = snapHelper.calculateDistanceToFinalSnap(manager, view);
                if (snapDistance[0] != 0 || snapDistance[1] != 0)
                {
                    Log.d(TAG,"X:"+snapDistance[0]+"Y:"+snapDistance[1]+" toolbarsize:"+getToolBarHeight());
                    if(snapDistance[1]<=0||(snapDistance[1]*2)<=0)
                    {
                        return;
                    }

                    imageRecycleView.scrollBy(snapDistance[0], snapDistance[1]);
               
                }
            },500*2);
            invokePageChanged(pageId);
        }

    } catch (Exception e) {

        CommonFunctions.writeErrorMessage(e);
    }


}

This is my layout for the elements :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layoutDirection="ltr"
android:orientation="horizontal"
android:layout_margin="0px"
android:padding="0px"
android:layout_gravity="center"
android:gravity="center">

<ImageView
    android:id="@+id/imageView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="fitXY"
    android:adjustViewBounds="true"
    android:visibility="visible"
    />

<Button
    android:id="@+id/finish_button"
    android:visibility="gone"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/intro_button_label"
    android:layout_margin="20dp"
    android:padding="10dp"
    android:textColor="@color/white"
    android:textSize="40sp"
    android:textStyle="bold"
    android:background="@drawable/button2"
    />
/>
</LinearLayout>

This issue has also been asked before and nobody give an answer only this answer seems a fix but it's not it only works as it feels like it not all the time: https://stackoverflow.com/a/57207024/3126202

Fix: I ended up using both scrollToPositionWithOffset(pageNumber+2) and after that smoothScrollToPosition(pageN): the code is below to whomever needs it:

    public void goToPage(int pageId)
{
    try {
        final int correctPage=pageId;
        Log.d(TAG,"goToPage(String pageId) : go to page:"+pageId);
        if(pageId<=  606 && pageId>=0)
        {
            LinearLayoutManager manager=(LinearLayoutManager) imageRecycleView.getLayoutManager();

            if(pageId-2<=0)
            {
                Log.d(TAG,"this is an issue:"+(pageId-2));
                manager.scrollToPositionWithOffset(pageId+2,0);
            }else{
                manager.scrollToPositionWithOffset(pageId-2,0);
            }

            imageRecycleView.postDelayed(() -> {
                Log.d(TAG,"Calling smoothToPosition");
                imageRecycleView.smoothScrollToPosition(correctPage);
                invokePageChanged(pageId);
            },200*1);
        }

    } catch (Exception e) {

        CommonFunctions.writeErrorMessage(e);
    }
}
0

There are 0 answers