리사이클러뷰 프로젝트.
매인 액티비티(Empty Activity) 생성.
/////////////////////////////////////////////////////////////////////////////////////////
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
/////////////////////////////////////////////////////////////////////////////////////////
Build.gradle(Module:app)에 com.android.support:recyclerview-v7 모듈 추가
/////////////////////////////////////////////////////////////////////////////////////////
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.github.bearkinf:AndroidLogPrintUtil_Java:1.1.1'
//리사이클러뷰 모듈 추가.
implementation 'com.android.support:recyclerview-v7:28.0.0'
}
/////////////////////////////////////////////////////////////////////////////////////////
리사이클러뷰에 사용할 데이터 생성.
간단한 데이터 출력 하기위한 클래스 생성.
/////////////////////////////////////////////////////////////////////////////////////////
public class People {
private String name;
private String age;
private String address;
/**
* 기본 생성자.
*/
public People() {
}
public People(String name) {
this.name = name;
}
public People(String name, String age) {
this.name = name;
this.age = age;
}
public People(String name, String age, String address) {
this.name = name;
this.age = age;
this.address = address;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "name : " + getName() + " , age : " + getAge() + " , address : " + getAddress();
}
}
/////////////////////////////////////////////////////////////////////////////////////////
리사이클러뷰에 보여줄 row layout 생성.
간단히 name, age, address 보여줌.
test_list_row.xml
/////////////////////////////////////////////////////////////////////////////////////////
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="name"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="age"
app:layout_constraintTop_toBottomOf="@+id/name" />
<TextView
android:id="@+id/address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="address"
app:layout_constraintTop_toBottomOf="@+id/age" />
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
/////////////////////////////////////////////////////////////////////////////////////////
뷰홀더 클래스 생성. RecyclerView.ViewHolder 상속
/////////////////////////////////////////////////////////////////////////////////////////
public class TestHolder_1 extends RecyclerView.ViewHolder {
private TextView name;
private TextView age;
private TextView address;
/**
* 팩토리 함수 생성.
* new 하는 것을 줄여준다.
*/
public static TestHolder_1 newInstance(ViewGroup viewGroup) {
// 뷰 생성 후 홀더에 등록 하여 객채 생성.
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.test_list_row, viewGroup, false);
return new TestHolder_1(view);
}
/**
* 생성자 초기화
*/
public TestHolder_1(View itemView) {
super(itemView);
name = itemView.findViewById(R.id.name);
age = itemView.findViewById(R.id.age);
address = itemView.findViewById(R.id.address);
}
public void onBindView(int position, ArrayList<People> list) {
//화면을 그려주는 곳.
name.setText(list.get(position).getName());
age.setText(list.get(position).getAge());
address.setText(list.get(position).getAddress());
}
}
/////////////////////////////////////////////////////////////////////////////////////////
리사이클러뷰 아답터 생성.
/////////////////////////////////////////////////////////////////////////////////////////
public class TestAdapter_1 extends RecyclerView.Adapter<TestHolder_1> {
private ArrayList<People> list;
/**생성자 (어레이리스트를 인자로 받음.(초기화됨))*/
public TestAdapter_1(ArrayList<People> list) {
this.list = list;
}
@NonNull
@Override
public TestHolder_1 onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
//팩토리 함수를 이용한 홀더 객체 생성.
return TestHolder_1.newInstance(viewGroup);
}
@Override
public void onBindViewHolder(@NonNull TestHolder_1 testHolder_1, int i) {
// 홀더에 정의된 함수로 뷰 그리기
testHolder_1.onBindView(i, list);
}
@Override
public int getItemCount() {
//리스트 갯수 반환.
return list.size();
}
}
/////////////////////////////////////////////////////////////////////////////////////////
리사이클러뷰 레이아웃 등록.
activity_main.xml
/////////////////////////////////////////////////////////////////////////////////////////
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">
<!-- 레이아웃 매니저 등록, 수직으로 정렬지정-->
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclereView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layoutManager="android.support.v7.widget.LinearLayoutManager">
</android.support.v7.widget.RecyclerView>
</android.support.constraint.ConstraintLayout>
/////////////////////////////////////////////////////////////////////////////////////////
매인 액티비티 소스 완성
/////////////////////////////////////////////////////////////////////////////////////////
public class MainActivity extends AppCompatActivity {
//리사이클러뷰 등록.
private RecyclerView recyclerView;
// 아답터 등록.
private TestAdapter_1 adapter_1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LogPrintUtil.setDebug(BuildConfig.DEBUG ? LogPrintUtil.All : LogPrintUtil.None);
LogPrintUtil.setTag("bear");
LogPrintUtil.w("start");
//레이아웃 설정.
setupUI();
}
/**
* UI 생성
*/
private void setupUI() {
recyclerView = findViewById(R.id.recyclereView);
adapter_1 = new TestAdapter_1(makeData());
recyclerView.setAdapter(adapter_1);
}
/**
* 보여줄 데이터 생성.
*/
private ArrayList<People> makeData() {
ArrayList<People> list = new ArrayList<>();
for (int i = 0; i < 40; i++) {
People p = new People(i + "a", i + "age", i + "address");
list.add(p);
}
return list;
}
}
/////////////////////////////////////////////////////////////////////////////////////////
리사이클러뷰 기본 예제 끝.