๋ฐ์ํ
Notice
Recent Posts
Recent Comments
Link
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
Tags
- ์๋ฐ์คํฌ๋ฆฝํธ
- ํ์ด์ฌ
- ์คํธ๋ฆผ๋ฆฟ ๊ธฐ๋ณธ
- Streamlit๊ธฐ๋ณธ
- Matplotlib ๊ธฐ์ด
- ์น๋์๋ณด๋ ์ ์
- ์คํธ๋ฆผ๋ฆฟ
- ํ์ด์ฌ๊ณต๋ถ
- MySQL
- java
- ์๋ฐ๊ธฐ์ด
- streamlit
- ์๋ฐ๊ณต๋ถ
- Streamlit๊ธฐ์ด
- ๋ฐ์ดํฐ๋ฒ ์ด์ค
- Pandas
- RESTful API
- serverless
- ํ๋ค์ค๊ณต๋ถ
- Android
- ์๋ฐ์ด๋ณด
- ์น๋์๋ณด๋ ๊ธฐ์ด
- ์๋ฐ์คํฌ๋ฆฝํธ ๊ณต๋ถ
- ์๋ฐ
- ์๋๋ก์ด๋ ์ฑ ๊ฐ๋ฐ
- JavaScript
- ํ๋ค์ค
- db
- ์๋ฐํ๋ก๊ทธ๋๋ฐ
- ์๋๋ก์ด๋ ์คํ๋์ค
Archives
- Today
- Total
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);
}
๋ฐ์ํ