상세 컨텐츠

본문 제목

Android - 리사이클러뷰 페이징 처리하는 방법

Android

by yjh0922 2022. 7. 20. 18:05

본문

@Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                int lastPosition = ((LinearLayoutManager)recyclerView.getLayoutManager()).findLastCompletelyVisibleItemPosition();
                int totalCount = recyclerView.getAdapter().getItemCount();
                // 스크롤을 맨 끝까지 한것!
                if(lastPosition + 1 == totalCount) {
                    //리스트 마지막(바닥) 도착!!!!! 다음 페이지 데이터 로드!!
                    //데이터 로드후의 사용할 코드는 밑의 부분에 작성해준다.
 
                }
            }
        })

 

예시 코드

 

			@Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                int lastPosition = ((LinearLayoutManager)recyclerView.getLayoutManager()).findLastCompletelyVisibleItemPosition();
                int totalCount = recyclerView.getAdapter().getItemCount();
                // 스크롤을 맨 끝까지 한것!
                if(lastPosition + 1 == totalCount){
//              if (pageToken == null)
//                    {
//                        return;
//                    }
//
//                    // 1. 프로그레스바를 돌린다.
//                    progressBar.setVisibility(View.VISIBLE);
//
//                    // 2. URL을 조합한다.
//                    // ?part=snippet&key=[자신의 API KEY]&q=축구&maxResults=20
//                    String url = Config.BASE_URL + "?part=snippet&key="+
//                            Config.GOOGLE_API_KEY + "&q="+keyword+"&maxResults=20&pageToken="+pageToken;
//
//                    RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
//                    JsonObjectRequest request = new JsonObjectRequest(
//                            Request.Method.GET,
//                            url,
//                            null,
//                            new Response.Listener<JSONObject>()
//                            {
//                                @Override
//                                public void onResponse(JSONObject response)
//                                {
//                                    // 데이터를 받아오면, 프로그레스바를 안보이게 한다.
//                                    progressBar.setVisibility(View.INVISIBLE);
//
//                                    try {
//                                        if (response.has("nextPageToken")){
//                                            pageToken = response.getString("nextPageToken");
//                                        }
//                                        else
//                                        {
//                                            pageToken = null;
//                                        }
//
//                                        JSONArray dataList = response.getJSONArray("items");
//
//                                        for(int i = 0; i < dataList.length(); i++){
//                                            String title = dataList.getJSONObject(i)
//                                                    .getJSONObject("snippet").getString("title");
//                                            String description = dataList.getJSONObject(i)
//                                                    .getJSONObject("snippet").getString("description");
//
//                                            String imgUrl = dataList.getJSONObject(i)
//                                                    .getJSONObject("snippet").getJSONObject("thumbnails")
//                                                    .getJSONObject("medium").getString("url");
//
//                                            String videoId = dataList.getJSONObject(i)
//                                                    .getJSONObject("id").getString("videoId");
//
//                                            Video video = new Video(title, description, imgUrl, videoId);
//                                            videoList.add(video);
//                                        }
//
//                                    }
//                                    catch (JSONException e)
//                                    {
//                                        e.printStackTrace();
//                                    }
//
//                                    adapter.notifyDataSetChanged();
//
//                                }
//                            },
//                            new Response.ErrorListener()
//                            {
//                                @Override
//                                public void onErrorResponse(VolleyError error)
//                                {
//                                    // 데이터를 받아오면, 프로그레스바를 안보이게 한다.
//                                    progressBar.setVisibility(View.INVISIBLE);
//
//                                }
//                            }
//                    );
//                    queue.add(request);
//
//
//
//                }
//
//            }
//        });

 

관련글 더보기