๊ด€๋ฆฌ ๋ฉ”๋‰ด

ruriruriya

[Android] ์•ˆ๋“œ๋กœ์ด๋“œ - RecyclerView ํŽ˜์ด์ง• ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ• ๋ณธ๋ฌธ

๐Ÿค–Android/Java

[Android] ์•ˆ๋“œ๋กœ์ด๋“œ - RecyclerView ํŽ˜์ด์ง• ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•

๋ฃจ๋ฆฌ์•ผใ…‘ 2024. 1. 3. 12:30
๋ฐ˜์‘ํ˜•

25๊ฐœ์”ฉ ๋ฆฌ์‚ฌ์ดํด๋Ÿฌ๋ทฐ๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๋ฉฐ ํŽ˜์ด์ง•์„ ํ•ด๋ณด์ž!

 

1. ๋ฆฌ์‚ฌ์ดํด๋Ÿฌ๋ทฐ ScrollListener ์ƒ์„ฑ

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {});



2. ScrollListener ํ•จ์ˆ˜ ์˜ค๋ฒ„๋ผ์ด๋”ฉ.

onScrollStateChanged : ์ƒํƒœ ๋ณ€ํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์ž๋™ ์ƒ์†๋˜์–ด ์ˆ˜์ •ํ•˜์ง€ ์•Š์•„๋„ ๋Œ.
- onScrolled : ์†๊ฐ€๋ฝ์œผ๋กœ ์Šคํฌ๋กคํ•  ๋•Œ๋งˆ๋‹ค ํ˜ธ์ถœ๋˜๋Š” ํ•จ์ˆ˜๋กœ ์ˆ˜์ •์ด ํ•„์š”ํ•˜๋‹ค.

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {

    // ์ƒํƒœ ๋ณ€ํ•˜๋Š” ๊ฒƒ์ด๋ผ ์‹ ๊ฒฝ์“ธ ๊ฒƒ ์—†์Œ.
    @Override
    public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);
    }

    // ์†๊ฐ€๋ฝ์œผ๋กœ ์Šคํฌ๋กค ํ•  ๋•Œ๋งˆ๋‹ค ํ˜ธ์ถœ๋˜๋Š” ํ•จ์ˆ˜.(์ด ํ•จ์ˆ˜๋งŒ ์ž‘์„ฑํ•˜๋ฉด ๋œ๋‹ค.)
    @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){
            //๋„คํŠธ์›Œํฌ ํ†ตํ•ด์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€๋กœ ๋ฐ›์•„์˜ค๊ณ , ํ™”๋ฉด์— ํ‘œ์‹œ.
            addNetworkData();
        }
    }
});


3. addNetworkData ํ•จ์ˆ˜ ์ž‘์„ฑ.

private void addNetworkData() {
    progressBar.setVisibility(View.VISIBLE);

    RequestQueue queue = Volley.newRequestQueue(MainActivity.this);

    // ๋ฐ์ดํ„ฐ๋ฅผ ํŽ˜์ด์ง•์ฒ˜๋ฆฌํ•ด์„œ ์ถ”๊ฐ€๋กœ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ์œผ๋ฏ€๋กœ,
    // pageToken ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ์žˆ์–ด์•ผ ํ•œ๋‹ค.

    String url = Config.SEARCH_URL+"?key="+Config.GOOGLE_API_KEY+"&part=snippet&q="+keyword+" &type=video&maxResults=25&order=date&pageToken="+pageToken;

    JsonObjectRequest request = new JsonObjectRequest(
            Request.Method.GET,
            url, // ์ƒˆ๋กœ ์ƒ์„ฑํ•œ url
            null, //๋ณด๋‚ผ ๋ฐ์ดํ„ฐ ์—†์Œ
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    progressBar.setVisibility(View.GONE);
                    try {

                        pageToken = response.getString("nextPageToken");
                        JSONArray items = response.getJSONArray("items");

                        for(int i = 0; i < items.length();i++){
                            String videoId = items.getJSONObject(i).getJSONObject("id").getString("videoId");
                            String title = items.getJSONObject(i).getJSONObject("snippet").getString("title");
                            String description = items.getJSONObject(i).getJSONObject("snippet").getString("description");
                            String mediumUrl = items.getJSONObject(i).getJSONObject("snippet").getJSONObject("thumbnails").getJSONObject("medium").getString("url");
                            String highUrl = items.getJSONObject(i).getJSONObject("snippet").getJSONObject("thumbnails").getJSONObject("high").getString("url");



                            Video video = new Video(videoId,title, description, mediumUrl, highUrl);

                            videoArrayList.add(video);

                        }
                        // ์–ด๋Œ‘ํ„ฐ๋Š” ์ด๋ฏธ ์žˆ์–ด์„œ ์ƒˆ๋กœ ๋งŒ๋“ค์ง€ ์•Š์•„๋„ ๋Œ.
                        // ๋ฐ”๋€.ํ™”๋ฉด์— ๋ณด์—ฌ์ฃผ๊ธฐ
                        adapter.notifyDataSetChanged();


                    } catch (JSONException e) {
                        //์œ ์ €ํ•œํ…Œ ์•Œ๋ฆฌ๊ธฐ //ํ† ์ŠคํŠธ ์“ฐ๊ธฐ.
                        return;
                    }

                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    progressBar.setVisibility(View.GONE);
                }
            }
    );

    queue.add(request);


}
๋ฐ˜์‘ํ˜•