Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

느릿느릿 프로그래밍

[Android] RecyclerView의 scrollToPosition()/smoothScrollToPosition() 이 동작하지 않을 때 본문

프로그래밍/Android

[Android] RecyclerView의 scrollToPosition()/smoothScrollToPosition() 이 동작하지 않을 때

김슈달 2018. 7. 4. 02:32
반응형

앱을 개발하던 중에

RecyclerView 화면에서 다른 화면으로 전환 후 다시 RecyclerView 화면으로 돌아올 때(Activity간 이동)

마지막에 RecyclerView에서 봤던 아이템으로 화면 스크롤을 조정하기 위해

scrollToPosition()과 smoothScrollToPosition()을 사용하였으나 제대로 동작하지 않았다.


그래서 해당 문제를 해결하려고 서치해보았으나 정확한 원인은 알 수 없었다.

스택오버플로우에 비슷한 사례가 있었으나 다들 정확한 원인은 짚지 못하고 고통 받는 글들 뿐이었다..


*해결


비슷한 사례들 모두 Handler를 사용해 리사이클러뷰에 조금의 대기 시간을 준 후 scrollToPosition()을 사용하라는 조언으로 해결됐다고 했다.


그래서 나도 onActivityResult에 다음과 같은 코드를 넣어 문제를 해결할 수 있었다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode,resultCode,data);
 
        position = data.getIntExtra("position",0);

        if(resultCode == RESULT_OK){
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    recyclerView.scrollToPosition(position);
                }
            },100);
        }
    }
cs



참고 : https://stackoverflow.com/questions/40077807/android-recyclerview-scrolltoposition-not-scrolling-to-bottom-of-view
https://stackoverflow.com/questions/36426129/recyclerview-scroll-to-position-not-working-every-time


반응형
Comments