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 | 31 |
Tags
- Button
- RestAPI
- addTextChangedListner
- editText
- backgroundTint
- viewmodel
- PostgreSQL
- springboot
- livedata
- prolificinteractive/material-calendarview
- DialogFragment
- 리눅스
- podinit
- Dialog
- cocoapod
- calendar
- android
Archives
- Today
- Total
코코딩딩
[안드로이드/android] recyclerView 본문
게시판의 게시물 목록과 같은 리스트를 띄우기 위해 검색을 한 결과 recyclerView를 이용하는 것으로 결정했다. 공식 문서에 의하면 recyclerView는 대량의 데이터 세트를 효율적으로 표시할 수 있는 라이브러리이다.
블로그글들을 참고해서 만들었지만 다 만들고 난 후 안드로이드의 개발자문서를 들어가니 예제 코드가 잘 나와있다는 것을 뒤늦게 확인하였다. 앞으로는 개발자 문서를 가장 먼저 참고하고 이해가 안가면 블로그 글을 찾을 것이다.
각각의 뷰 항목의 모양을 결정하는 레이아웃 만들기
array에 담긴 데이터들을 리스트 형태로 뿌리기 위해 각 항목의 디자인을 xml 파일에 먼저 만들어준다.
// recyclerView_item.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/teal_700"
android:orientation="horizontal">
<TextView
android:layout_marginLeft="10dp"
android:layout_gravity="center"
android:id="@+id/test_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="테스트"
android:textColor="@color/black"
android:textSize="24sp"
tools:ignore="MissingConstraints" />
</LinearLayout>
더 멋진 디자인의 리스트를 원하면 이 xml 을 수정해서 사용하면 된다.
adapter 만들기
아래와 같이 adapter를 구성해 준다.
package com.example.recuclertest;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class TextAdapter extends RecyclerView.Adapter<TextAdapter.ViewHolder> {
private ArrayList<String> arrayList = null ;
public class ViewHolder extends RecyclerView.ViewHolder {
TextView textView;
ViewHolder(View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.test_tv) ;
}
}
public TextAdapter(ArrayList<String> list) {
arrayList = list ;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext() ;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) ;
View view = inflater.inflate(R.layout.recyclerview_item, parent, false) ;
ViewHolder vh = new ViewHolder(view) ;
return vh ;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
String text = arrayList.get(position) ;
holder.textView.setText(text) ;
}
@Override
public int getItemCount() {
return arrayList.size() ;
}
}
MainActivity 구성하기
package com.example.recuclertest;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import com.example.recuclertest.databinding.ActivityMainBinding;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
showRecyclerView(binding.recyclerView,makeList("테스트이름"));
}
public void showRecyclerView(RecyclerView recyclerView, ArrayList<String> list){
recyclerView.setLayoutManager(new LinearLayoutManager(this));
TextAdapter adapter = new TextAdapter(list);
recyclerView.setAdapter(adapter);
}
public ArrayList<String> makeList(String listname){
ArrayList<String> list = new ArrayList<>();
for (int i=0; i<10; i++) {
list.add(String.format(listname+" %d", i)) ;
}
return list;
}
}
showRecylerView 라는 메서드를 만들어 만든 adapter를 선언하고 recyclerView에 setAdapter에 넣어준다. 리스트를 띄우기 위해 필요한 array는 임의로 for문을 이용해 만들었지만 api호출 등을 이용해 db에서 데이터를 불러와 띄우는 용도로 활용 할 것이다.
MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
이 외에도 더 짜잘한 것을 많이 했지만 나중에 보충하도록 하겠다.
'일단기록 > 매일기록' 카테고리의 다른 글
[스프링/spring] restapi 실수한 부분 정리 (0) | 2022.05.02 |
---|---|
[안드로이드/android] recyclerview 접기,펼치기(아코디언) (0) | 2022.05.02 |
[안드로이드/android] 카카오api 카카오링크(v2-link) 메세지 전송 (1) | 2022.04.27 |
[안드로이드/android] customdialog, timepicker (0) | 2022.04.26 |
[리눅스] tomcat, apache 설치하기 (0) | 2022.04.25 |