[챕터 2-6-4] 리싸이클러 뷰 ★
2020. 2. 18. 18:10ㆍAndroid/Android 챕터 2-6
반응형
결과화면
리스트 항목 중 한개의 항목을 클릭 시
하단에 해당하는 토스트 메세지 출력
프로젝트명 : MyList
컨테이너 > 리사이클러뷰 다운로드
다운로드 후 안드 재실행
Person.java
더보기
package org.minokuma.mylist;
public class Person {
String name;
String mobile;
public Person(String name, String mobile) {
this.name = name;
this.mobile = mobile;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
}
PersonAdapter.java
더보기
package org.minokuma.mylist;
import android.text.Layout;
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 PersonAdapter extends RecyclerView.Adapter<PersonAdapter.ViewHolder> {
ArrayList<Person> persons = new ArrayList<Person>();
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View itemView = layoutInflater.inflate(R.layout.person_item, parent, false);
return new ViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Person person = persons.get(position);
holder.setItem(person);
}
@Override
public int getItemCount() {
return persons.size();
}
static class ViewHolder extends RecyclerView.ViewHolder {
TextView textView;
TextView textView2;
public ViewHolder(@NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.textView);
textView2 = itemView.findViewById(R.id.textView2);
}
public void setItem(Person persion){
textView.setText(persion.getName());
textView2.setText(persion.getMobile());
}
}
}
person_item.xml
리니어 레이아웃 : 가로
컨테이너 > 카드 뷰 다운로드
다운로드 후 안스 재실행
person_item.xml
더보기
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="#FFFFFF"
app:cardCornerRadius="10dp"
app:cardElevation="5dp"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageView"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="10dp"
app:srcCompat="@mipmap/ic_launcher" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_margin="5dp">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="이름"
android:textColor="#0000FF"
android:textSize="30sp"/>
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="전화번호"
android:textColor="#00FFFF"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
PersonAdapter.java
더보기
package org.minokuma.mylist;
import android.text.Layout;
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.lang.reflect.Array;
import java.util.ArrayList;
public class PersonAdapter extends RecyclerView.Adapter<PersonAdapter.ViewHolder> {
ArrayList<Person> persons = new ArrayList<Person>();
public void addItem(Person person){
persons.add(person);
}
public void setItems(ArrayList<Person> persons){
this.persons = persons;
}
public Person getItem(int position){
return persons.get(position);
}
public void setItem(int position, Person person){
persons.set(position, person);
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View itemView = layoutInflater.inflate(R.layout.person_item, parent, false);
return new ViewHolder(itemView);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Person person = persons.get(position);
holder.setItem(person);
}
@Override
public int getItemCount() {
return persons.size();
}
static class ViewHolder extends RecyclerView.ViewHolder {
TextView textView;
TextView textView2;
public ViewHolder(@NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.textView);
textView2 = itemView.findViewById(R.id.textView2);
}
public void setItem(Person persion){
textView.setText(persion.getName());
textView2.setText(persion.getMobile());
}
}
}
그리드 레이아웃 전환 (이 부분은 건너뛰어도 됨)
MainActivity.java
더보기
package org.minokuma.mylist;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = findViewById(R.id.recycler_view);
//LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
//recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setLayoutManager(gridLayoutManager);
PersonAdapter personAdapter = new PersonAdapter();
personAdapter.addItem(new Person("미노쿠마", "010-2020-3030"));
personAdapter.addItem(new Person("키타무라", "010-1010-4040"));
personAdapter.addItem(new Person("세나", "010-3030-55050"));
recyclerView.setAdapter(personAdapter);
}
}
클래스명 : OnPersonItemClickListener
더보기
package org.minokuma.mylist;
import android.view.View;
public interface OnPersonItemClickListener {
public void onItemClick(PersonAdapter.ViewHolder holder, View view, int position);
}
PersonAdapter.java
더보기
package org.minokuma.mylist;
import android.text.Layout;
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.lang.reflect.Array;
import java.util.ArrayList;
public class PersonAdapter extends RecyclerView.Adapter<PersonAdapter.ViewHolder> {
ArrayList<Person> persons = new ArrayList<Person>();
OnPersonItemClickListener onPersonItemClickListener;
public void addItem(Person person){
persons.add(person);
}
public void setItems(ArrayList<Person> persons){
this.persons = persons;
}
public Person getItem(int position){
return persons.get(position);
}
public void setItem(int position, Person person){
persons.set(position, person);
}
public void setOnPersonItemClickListener(OnPersonItemClickListener onPersonItemClickListener){
this.onPersonItemClickListener = onPersonItemClickListener;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View itemView = layoutInflater.inflate(R.layout.person_item, parent, false);
return new ViewHolder(itemView, onPersonItemClickListener);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
Person person = persons.get(position);
holder.setItem(person);
}
@Override
public int getItemCount() {
return persons.size();
}
static class ViewHolder extends RecyclerView.ViewHolder {
TextView textView;
TextView textView2;
public ViewHolder(@NonNull final View itemView, final OnPersonItemClickListener onPersonItemClickListener) {
super(itemView);
textView = itemView.findViewById(R.id.textView);
textView2 = itemView.findViewById(R.id.textView2);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = getAdapterPosition();
if(onPersonItemClickListener != null){
onPersonItemClickListener.onItemClick(ViewHolder.this, v, position);
}
}
});
}
public void setItem(Person persion){
textView.setText(persion.getName());
textView2.setText(persion.getMobile());
}
}
}
MainActivity.java
더보기
package org.minokuma.mylist;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
PersonAdapter personAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = findViewById(R.id.recycler_view);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
//GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
recyclerView.setLayoutManager(linearLayoutManager);
//recyclerView.setLayoutManager(gridLayoutManager);
personAdapter = new PersonAdapter();
personAdapter.addItem(new Person("미노쿠마", "010-2020-3030"));
personAdapter.addItem(new Person("키타무라", "010-1010-4040"));
personAdapter.addItem(new Person("세나", "010-3030-55050"));
recyclerView.setAdapter(personAdapter);
personAdapter.setOnPersonItemClickListener(new OnPersonItemClickListener() {
@Override
public void onItemClick(PersonAdapter.ViewHolder holder, View view, int position) {
Person person = personAdapter.getItem(position);
showToToast("아이템 선택됨 : " + person.getName());
}
});
}
public void showToToast(String data){
Toast.makeText(this, data, Toast.LENGTH_SHORT).show();
}
}
반응형
'Android > Android 챕터 2-6' 카테고리의 다른 글
[챕터 2-6-5] 스피너 뷰 (0) | 2020.02.18 |
---|---|
[챕터 2-6-3] 레이아웃 커스터마이징 (0) | 2020.02.18 |
[챕터 2-6-2] 버튼 커스터마이징 (0) | 2020.02.18 |
[챕터 2-6-1] 나인패치 이미지 (0) | 2020.02.18 |