Подтвердить что ты не робот

Как перейти к определенной позиции в recyclerview внутри scrollview?

Я много искал в отношении моего запроса, и ни один из них не был полезен. У меня есть recyclerview и некоторые статические данные внутри прокрутки, которые находятся внутри корневого родителя, как показано ниже.

Я установил -

scrollview.scrollto(0,0);

потому что каждый раз, когда я открываю действие, он переходит к recyclerview firstitem, и он пропускает статические данные выше recyclerview.

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

для smoothscroll.

проблема заключается в with-

layoutmanager.scrollToPositionWithOffset(pos,0);

это не работает. Я установил aboveline после установки адаптера для recyclerview. Также попытался с NestedScrollView, но напрасно.

хотя я использовал

layoutmanager.scrollToPosition(pos);

Для тех, кто пропускает код, я установил match_parent для ScrollView и fillviewport в true.

Вот мой макет.

<?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>
4b9b3361

Ответ 1

Необходимо прокрутить ScrollView, получив верхнюю позицию дочернего элемента RecyclerView:

float y = recyclerView.getY() + recyclerView.getChildAt(selectedPosition).getY();    
scrollView.smoothScrollTo(0, (int) y);

Ответ 2

вы можете использовать метод просмотра прокрутки для достижения желаемых местоположений в виде:

scrollView.smoothScrollTo(int x, int y);

Ответ 3

Я знаю, что отвечаю довольно поздно, но если у вас проблема с автопрокруткой, вам нужно добавить android: descendantFocusability = "blocksDescendants" в RelativeLayout. Итак, ваш тег RealtiveLyout будет выглядеть так:

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

Ответ 4

Шаг 1. Добавьте эту строку для выбора этой позиции в recyclerview

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

Шаг 2: После проверки ниже конструктора, получив текущую позицию

  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;
    }

Шаг 3: Эта функция также входит в адаптер

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

Последний шаг 4. Добавьте эту функцию вне адаптера в свой класс

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

Попытайтесь использовать эту строку в элементе списка listview

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

Эта позиция будет выбрана, когда вы откроете список.

Ответ 5

Используйте этот метод scrollToPosition (int position), это работает для меня. Надеюсь, что это поможет вам.

        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);

это мой layoutstructer

enter image description here