[안드로이드][프로젝트2] 네이버 검색 API + RecyclerView + CardView+ Hanlder의 postDelayed
네이버 검색 API + RecyclerView + CardView를 이용하여 사진과 같이 정보가 나오길 바랐는데, 처음에 했을 때는 오류가 나왔다. 그래서 Log.d를 열심히 찍어가며 어디서 문제가 생기는 건지 봤고, 원인을 발견했다.
답은 네트워크 연결보다 array 불러오는 게 빨라서 안 됐던 것이었다.
그래서 array 불러오는 곳에다가 handler의 postDelayed 메소드를 이용하여 지연시켰더니 내가 원하는대로 나왔다.
handler의 postDelayed 메소드
// 2초간 멈추게 하고싶다면
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
// 하려는 것을 넣어주자. ㅎㅎ
}
}, 2000); // 2000은 2초를 의미합니다.
안드로이드에서 pause나 sleep하는 방법
두 라인 사이에 잠깐의 딜레이를 주고싶은데요. 설명하자면 유저가 버튼을 클릭하면 버튼의 배경을 바꿔서 보여주고 다시 1초뒤에 원래 배경으로 돌아오게 만들고 싶어요.thisbutton.setBackgroundResou
hashcode.co.kr
내가 썼던 메소드는 다음과 같다. 접은글을 열어보자.
private void getAllBooks(){
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
//리사이클러뷰에 표시할 데이터 리스트 생성
//add data to it.
for(int i = 0; i<5;i++)
{
bookList.add(new ModelBook(title[i],image[i],publisher[i],description[i]));
}
//adapter
//we are initializing our adapter class and passing our arraylist to it.
adapterBooks = new AdapterBooks(getActivity(), bookList);
//set adapter to recycler view
recyclerView.setAdapter(adapterBooks);
}
}, 2000); // 2000은 2초를 의미합니다.
}
RecyclerView + CardView 쓰는 것은 다음 사이트를 참고하였다.
https://www.geeksforgeeks.org/cardview-using-recyclerview-in-android-with-example/
CardView using RecyclerView in Android with Example - GeeksforGeeks
A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
www.geeksforgeeks.org