사용자가 관리하는 많은 수의 데이터 집합(Data Set)을
개별 아이템 단위로 구성하여 화면에 출력하는 뷰그룹(ViewGroup)
스크롤 시 (가장 먼저) 처음 화면에 보이는 뷰 객체를 삭제하지 않고 기억하고 있다가, 뒤로 넘겨 재활용한다.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="0dp"
android:layout_height="110dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:background="@drawable/asdf"
tools:ignore="MissingConstraints">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txt1"
android:text="@string/txt1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginStart="15dp"
android:layout_marginTop="15dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txt2"
android:text="@string/txt2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/txt1"
android:layout_marginStart="15dp"
android:layout_marginTop="15dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txt3"
android:text="@string/txt3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/txt2"
app:layout_constraintBaseline_toBaselineOf="parent"
android:layout_marginStart="15dp"
android:layout_marginTop="10dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txt4"
android:text="@string/txt4"
app:layout_constraintStart_toEndOf="@id/txt3"
app:layout_constraintTop_toBottomOf="@id/txt2"
app:layout_constraintBaseline_toBaselineOf="parent"
android:layout_marginTop="10dp"
android:layout_marginStart="10dp"
/>
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:src="@android:color/holo_green_light"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/txt2"
app:layout_constraintBaseline_toBaselineOf="parent"
android:layout_marginEnd="15dp"
android:layout_marginTop="10dp"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
<?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=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview"
tools:listitem="@layout/recyclerviewitem"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.myapplication;
public class DataModel {
private String title;
private String contents;
private String time;
private String writer;
public DataModel(String title, String contents, String time, String writer) {
this.title = title;
this.contents = contents;
this.time = time;
this.writer = writer;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContents() {
return contents;
}
public void setContents(String contents) {
this.contents = contents;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getWriter() {
return writer;
}
public void setWriter(String writer) {
this.writer = writer;
}
}
package com.example.myapplication;
import android.content.Context;
import android.util.Log;
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 RecyclerViewAdapter extends RecyclerView.Adapter {
String TAG = "RecyclerViewAdapter";
ArrayList<DataModel> dataModels;
Context context;
public RecyclerViewAdapter(Context context, ArrayList<DataModel> dataModels){
this.dataModels = dataModels;
this.context = context;
}
public RecyclerViewAdapter(ArrayList<DataModel> dataModels) {
this.dataModels = dataModels;
}
@Override
public int getItemCount() {
return dataModels.size();
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
Log.d(TAG, "onCreateViewHolder");
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerviewitem, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
Log.d(TAG, "onBindViewHolder");
Log.w(";;;;ppp", "dddd" + dataModels.size());
ViewHolder viewHolder = (ViewHolder)holder;
viewHolder.title.setText(dataModels.get(position).getTitle());
viewHolder.contents.setText(dataModels.get(position).getContents());
viewHolder.time.setText(dataModels.get(position).getTime());
viewHolder.writer.setText(dataModels.get(position).getWriter());
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView title;
private TextView contents;
private TextView time;
private TextView writer;
public ViewHolder(@NonNull View itemView) {
super(itemView);
title = itemView.findViewById(R.id.txt1);
contents = itemView.findViewById(R.id.txt2);
time = itemView.findViewById(R.id.txt3);
writer = itemView.findViewById(R.id.txt4);
}
}
}
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.util.Log;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerViewAdapter adapter;
RecyclerView recyclerView;
ArrayList<DataModel> dataModels = new ArrayList();
dataModels.add(new DataModel("aaaa", "ssss", "0000", "qqqq"));
dataModels.add(new DataModel("zzzz", "wwww", "1111", "oooo"));
dataModels.add(new DataModel("aaaa", "ssss", "0000", "qqqq"));
dataModels.add(new DataModel("aaaa", "ssss", "0000", "qqqq"));
dataModels.add(new DataModel("aaaa", "ssss", "0000", "qqqq"));
dataModels.add(new DataModel("aaaa", "ssss", "0000", "qqqq"));
dataModels.add(new DataModel("aaaa", "ssss", "0000", "qqqq"));
dataModels.add(new DataModel("aaaa", "ssss", "0000", "qqqq"));
dataModels.add(new DataModel("aaaa", "ssss", "0000", "qqqq"));
dataModels.add(new DataModel("aaaa", "ssss", "0000", "qqqq"));
dataModels.add(new DataModel("aaaa", "ssss", "0000", "qqqq"));
dataModels.add(new DataModel("aaaa", "ssss", "0000", "qqqq"));
dataModels.add(new DataModel("aaaa", "ssss", "0000", "qqqq"));
dataModels.add(new DataModel("aaaa", "ssss", "0000", "qqqq"));
dataModels.add(new DataModel("aaaa", "ssss", "0000", "qqqq"));
dataModels.add(new DataModel("aaaa", "ssss", "0000", "qqqq"));
dataModels.add(new DataModel("aaaa", "ssss", "0000", "qqqq"));
dataModels.add(new DataModel("aaaa", "ssss", "0000", "qqqq"));
dataModels.add(new DataModel("aaaa", "ssss", "0000", "qqqq"));
dataModels.add(new DataModel("aaaa", "ssss", "0000", "qqqq"));
dataModels.add(new DataModel("aaaa", "ssss", "0000", "qqqq"));
Log.e("9999999","pp" + dataModels.size());
recyclerView = findViewById(R.id.recyclerview);
adapter = new RecyclerViewAdapter(this, dataModels);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this, RecyclerView.VERTICAL, false));
}
}