manhtuan21
manhtuan21

Reputation: 3455

How to reset position ScrollView inside RecycleView everytime RecycleView Scrolled

I have a RecycleView scroll Horizontal and every item of it has a ScrollView scroll Vertical. When I scroll down the ScrollView,it will remember that position and doesn't reset to the top when i scroll the RecycleView to another item,like this :

enter image description here

Could anyone give me an advice to make the ScrollView reset to the top when i scroll the RecycleView ?

Upvotes: 1

Views: 1178

Answers (2)

Amir Hossein Mirzaei
Amir Hossein Mirzaei

Reputation: 2375

you can call this :

scrollView.fullScroll(ScrollView.FOCUS_TOP)

or this :

 scrollView.scrollTo(0,0);

and you can use this in your onBindeViewHolder in your adapter when you are initializing the viewHolder or you can attach scroll Listener to your RecyclerView and detect scrolling with the scroll Listener

maybe something like this !

  recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {

                    LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();

                    int fVitem = linearLayoutManager.findFirstVisibleItemPosition();
                    int lVitem = linearLayoutManager.findLastVisibleItemPosition();

                    for (int i = fVitem; i <= lVitem; i++) {

                        View view = linearLayoutManager.findViewByPosition(i);

                        ScrollView scrollView;


                        if (view.getTag() != null) scrollView = (ScrollView) view.getTag();
                        else {
                            scrollView = view.findViewById(R.id.myScrollView);
                            view.setTag(scrollView);
                        }
                        scrollView.scrollTo(0, 0);
                    }

                }
            });

but i think if you use obBindViewHolder would be better for performance

Upvotes: 1

Aiko West
Aiko West

Reputation: 791

Call this, when you detect scrolling the RecyclerView

yourScrollView.fullScroll(ScrollView.FOCUS_UP);

Upvotes: 0

Related Questions