RecyclerView
1. RecyclerViewItem.java
public class RecyclerViewItem {
private String mImgName;
private String mMainText;
private String mSubText;
public String getImgName() {
return mImgName;
}
public void setImgName(String imgName) {
this.mImgName = imgName;
}
public String getMainText() {
return mMainText;
}
public void setMainText(String mainText) {
this.mMainText = mainText;
}
public String getSubText() {
return mSubText;
}
public void setSubText(String subText) {
this.mSubText = subText;
}
}
리스트 뷰에 나타낼 아이템 모델을 생성한다.
2. recycler_view_item.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp">
<ImageView
android:id="@+id/imgView_item"
android:layout_width="100dp"
android:layout_height="100dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/txt_main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/imgView_item" />
<TextView
android:id="@+id/txt_sub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/txt_main" />
</androidx.constraintlayout.widget.ConstraintLayout>
item 레이아웃을 꾸며준다.
3. MyRecyclerViewAdapter.java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.taek.test.R;
import com.taek.test.databinding.RecyclerViewItemBinding;
import java.util.List;
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.MyViewHolder> {
private List<RecyclerViewItem> list;
private Context context;
public MyRecyclerViewAdapter(List<RecyclerViewItem> list, Context context) {
this.list = list;
this.context = context;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(context).inflate(R.layout.recycler_view_item, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.bindItem(list.get(position));
}
@Override
public int getItemCount() {
return list.size();
}
class MyViewHolder extends RecyclerView.ViewHolder {
RecyclerViewItemBinding binding;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
binding = RecyclerViewItemBinding.bind(itemView);
}
void bindItem(RecyclerViewItem item) {
binding.imgViewItem.setImageResource(R.drawable.ic_launcher_background);
binding.txtMain.setText(item.getMainText());
binding.txtSub.setText(item.getSubText());
}
}
}
Adapter 생성
4. activity_recycler_view.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".list.RecyclerViewActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
recyclerView 액티비티 레이아웃
5. RecyclerViewActivity.java
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.os.Bundle;
import com.taek.test.databinding.ActivityRecyclerViewBinding;
import java.util.ArrayList;
import java.util.List;
public class RecyclerViewActivity extends AppCompatActivity {
private ActivityRecyclerViewBinding binding;
private List<RecyclerViewItem> list;
private MyRecyclerViewAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityRecyclerViewBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
list = new ArrayList<>();
for (int i = 0; i < 10; i++) {
addItem("iconName", "Taek" + i, "taek2.tistory.com");
}
adapter = new MyRecyclerViewAdapter(list, this);
binding.recyclerView.setAdapter(adapter);
binding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
private void addItem(String imgName, String mainText, String subText) {
RecyclerViewItem item = new RecyclerViewItem();
item.setImgName(imgName);
item.setMainText(mainText);
item.setSubText(subText);
list.add(item);
}
}
LifeCycle 신경 안쓰고 onCreate 메서드에 다 넣었다.
개발할 때는 LifeCycle 고려해서 하도록 합시다.
'앱 개발 > 안드로이드(Java)' 카테고리의 다른 글
Glide (0) | 2023.03.10 |
---|---|
ListView 구현 (0) | 2023.03.10 |
ViewBinding 적용하기 (0) | 2023.03.10 |
GPS Background service 구현하기 (1) | 2021.10.27 |
glide를 사용하여 GIF 로딩 화면 구현하기 (4) | 2021.08.17 |