How to scroll to a particular position in recyclerview inside scrollview?

谁都会走 提交于 2019-12-02 23:36:44
Vaibhav Jani

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

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

scrollView.smoothScrollTo(int x, int y);

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">

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 ..

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!