https://codebunny99.tistory.com/164
์ด ์ดํ๋ฆฌ์ผ์ด์ ์ ๋ฒ ์ด์ค๋ก ํ์ฌ ์ฝ๋๋ฅผ ์์ฑํ์๋ค.
MainActivity ์์ ์์ฑํ๋ค
1. recyclerView ์ ๋ฐ ์์น์ //์คํฌ๋กค ์ฒ๋ฆฌ ~ ์ฝ๋ ๋ฅผ์์ฑํ๋ค
// ์คํฌ๋กค ์ฒ๋ฆฌ๋ฅผ ์ํ ์ฝ๋
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();
}
}
});
2. ํฌ์คํธ๋งจ์์ nextPageToken์ ์ฌ์ฉํ๋ ๊ฒ์ ํ์ธํ๋ค.
3. MainActivity์์ nextPageToken ํ์ฑ ๋ถ๋ถ์ ์ถ๊ฐํด์ค๋ค.
๋ฉค๋ฒ๋ณ์์ ์ถ๊ฐํ๊ธฐ
nextPageToken์ ๊ธฐ์กด์ ํ์ฑ์ ์ถ๊ฐํด ํ์ฑํด์ค๋ค.
4. MainActivity์ addNetworkData() ํจ์ ๋ง๋ค์ด์ฃผ๊ธฐ(Create)
๋๋ฅด๋ฉด, ์๋์ ๊ฐ์ ์์น์ ํจ์๊ฐ ๋ง๋ค์ด์ง๋๋ฐ, ์๋์ ๊ฐ์ด nextPageToken ๋ถ๋ถ์ ์ถ๊ฐ๋ก ์์ฑํด์ค๋ค
// ์ถ๊ฐ๋ก 20๊ฐ์ฉ์ ๋ ํธ์ถํ ๋ ์ฌ์ฉํ๋ ํจ์
private void addNetworkData() {
progressBar.setVisibility(View.VISIBLE);
// ์ ํ๋ธ API๋ฅผ ํธ์ถํด์ ๋ฐ์ดํฐ๋ฅผ ๋ฐ์์จ๋ค
RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
String url = "https://www.googleapis.com/youtube/v3/search?key="
+ Config.YoutubeKey
+ "&part=snippet&maxResults=20&type=video&order=date&q="
+ keyword
+ "&pageToken=" + nextPageToken;
์๋๋ถํฐ๋ ๊ธฐ์กด์ ์์ฑํ onCreate ํจ์์์ ๋ณต์ฌํ์ฌ ๊ทธ๋๋ก ๋ถ์ฌ๋ฃ๊ธฐ ํ ํ์ url์ ์์ ์์ฑํ์๊ธฐ ๋๋ฌธ์
url์ด๋ผ๊ณ ๋ฐ๊ฟ์ค ๋ค์,
JsonObjectRequest request = new JsonObjectRequest(
Request.Method.GET,
url,
null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
// ํ๋ก๊ทธ๋ ์ค ๋ฐ๋ฅผ ์ ์ ์ ๋์์ ์ฌ๋ผ์ง๊ฒ ํ๋ ์ฝ๋
progressBar.setVisibility(View.GONE);
try {
nextPageToken = response.getString("nextPageToken");
JSONArray items = response.getJSONArray("items");
for (int i = 0; i < items.length(); i++) {
JSONObject data = items.getJSONObject(i);
JSONObject id = data.getJSONObject("id");
String videoId = id.getString("videoId");
JSONObject snippet = data.getJSONObject("snippet");
String title = snippet.getString("title");
String description = snippet.getString("description");
JSONObject thumbnails = snippet.getJSONObject("thumbnails");
JSONObject medium = thumbnails.getJSONObject("medium");
String thumbUrl = medium.getString("url");
JSONObject high = thumbnails.getJSONObject("high");
String url = high.getString("url");
Youtube youtube = new Youtube(videoId, title, description, thumbUrl, url);
YoutubeArrayList.add(youtube);
}
adapter.notifyDataSetChanged();
} catch (JSONException e) {
Toast.makeText(MainActivity.this, "ํ์ฑ ์๋ฌ", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
// ํ๋ก๊ทธ๋ ์ค ๋ฐ๋ฅผ ์ ์ ์ ๋์์ ์ฌ๋ผ์ง๊ฒ ํ๋ ์ฝ๋
progressBar.setVisibility(View.GONE);
Toast.makeText(MainActivity.this, "๋คํธ์ํฌ ํต์ ์๋ฌ", Toast.LENGTH_SHORT).show();
}
}
);
queue.add(request);
์ค๊ฐ์ //์ญ์ ํ ๋ถ๋ถ์ ์ญ์ ํด์ฃผ๊ณ , //์ถ๊ฐํ ๋ถ๋ถ์ ์ญ์ ํ ๊ทธ ์๋ฆฌ์ ์๋ก์ด ์ด๋ํฐ๋ฅผ ์ถ๊ฐํด์ฃผ๋ฉด ๋๋ค.
// ์ญ์ ํ ๋ถ๋ถ
YoutubeArrayList.clear(); // ๊ธฐ์กด ๋ฐ์ดํฐ ์ด๊ธฐํ
adapter = new YoutubeAdapter(MainActivity.this, YoutubeArrayList);
recyclerView.setAdapter(adapter);
// ์ถ๊ฐํ ๋ถ๋ถ
adapter.notifyDataSetChanged();
+
<Retrofit2 ๋ผ์ด๋ธ๋ฌ๋ฆฌ>
4๋ฒ ๋์ ์ MainActivity์ ์ด ๋ถ๋ถ์ ์์ฑํด์ค๋ค.
private void addNetworkData() {
progressBar.setVisibility(View.VISIBLE);
Retrofit retrofit = NetworkClient.getRetrofitClient(MainActivity.this);
PostingApi api = retrofit.create(PostingApi.class);
Call<PostingList> call = api.getPostingList("Bearer " + token, offset, limit );
call.enqueue(new Callback<PostingList>() {
@Override
public void onResponse(Call<PostingList> call, Response<PostingList> response) {
progressBar.setVisibility(View.GONE);
if(response.isSuccessful()){
PostingList postingList = response.body();
count = postingList.count;
offset = offset + count;
postingArrayList.addAll( postingList.items );
adapter.notifyDataSetChanged();
}else{
}
}