build.gradle(Module: app) : 라이브러리 추가하기
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
}
actvivity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F0F0F0"
/>
</LinearLayout>
list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:gravity="center_vertical"
android:layout_marginBottom="8dp"
android:background="@android:color/white">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:gravity="center_vertical">
<TextView
android:id="@+id/title_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:includeFontPadding="false"
android:text="TITLE"
android:textSize="15sp"
android:textColor="@android:color/black"
android:textStyle="bold"/>
<TextView
android:id="@+id/contents_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:includeFontPadding="false"
android:text="content"
android:textSize="13sp"
android:textColor="@android:color/darker_gray"/>
</LinearLayout>
<ImageView
android:id="@+id/imageView"
android:layout_width="70dp"
android:layout_height="70dp"
android:scaleType="centerCrop"
app:srcCompat="@mipmap/ic_launcher"/>
</LinearLayout>
ItemData.java
public class ItemData {
int image;
String title;
String contents;
public ItemData(int image, String title, String contents) {
this.image = image;
this.title = title;
this.contents = contents;
}
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
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;
}
}
MyRecyclerAdapter.java
public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.ViewHolder>{
}
public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.ViewHolder>{
@NonNull
@Override
public MyRecyclerAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return null;
}
@Override
public void onBindViewHolder(@NonNull MyRecyclerAdapter.ViewHolder holder, int position) {
}
@Override
public int getItemCount() {
return 0;
}
}
ViewHolder 클래스가 생성됨
public class ViewHolder {
}
ViewHolder 클래스가 RecyclerView.ViewHolder 를 상속받음
public class ViewHolder extends RecyclerView.ViewHolder {
}
super에 매칭되는 생성자 만듬
public class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(@NonNull View itemView) {
super(itemView);
}
}
MyRecyclerAdapter.java 기본형 생성
public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.ViewHolder>{
@NonNull
@Override
public MyRecyclerAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return null;
}
@Override
public void onBindViewHolder(@NonNull MyRecyclerAdapter.ViewHolder holder, int position) {
}
@Override
public int getItemCount() {
return 0;
}
public class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(@NonNull View itemView) {
super(itemView);
}
}
}
MyRecyclerAdapter.java
- itemData를 생성자를 통해 가져옴
public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.ViewHolder>{
private ArrayList<ItemData> itemData;
public MyRecyclerAdapter(ArrayList<ItemData> itemData) {
this.itemData = itemData;
}
}
MyRecyclerAdapter.java
- ViewHolder 클래스를 통해 연결
public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.ViewHolder>{
public class ViewHolder extends RecyclerView.ViewHolder {
TextView title;
TextView contents;
ImageView imageView;
public ViewHolder(@NonNull View itemView) {
super(itemView);
title = itemView.findViewById(R.id.title_text);
contents = itemView.findViewById(R.id.contents_text);
imageView = itemView.findViewById(R.id.imageView);
}
}
}
MyRecyclerAdapter.java
- onCreateViewHolder()
public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.ViewHolder>{
@NonNull
@Override
public MyRecyclerAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item, parent, false);
return new ViewHolder(view);
}
}
MyRecyclerAdapter.java
- onBindViewHolder()
public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.ViewHolder>{
@Override
public void onBindViewHolder(@NonNull MyRecyclerAdapter.ViewHolder holder, int position) {
ItemData item = itemData.get(position);
holder.title.setText(item.getTitle());
holder.contents.setText(item.getContents());
holder.imageView.setImageResource(item.getImage());
}
@Override
public int getItemCount() {
return itemData.size();
}
}
MyRecyclerAdapter.java (최종)
public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.ViewHolder>{
private ArrayList<ItemData> itemData;
public MyRecyclerAdapter(ArrayList<ItemData> itemData) {
this.itemData = itemData;
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView title;
TextView contents;
ImageView imageView;
public ViewHolder(@NonNull View itemView) {
super(itemView);
title = itemView.findViewById(R.id.title_text);
contents = itemView.findViewById(R.id.contents_text);
imageView = itemView.findViewById(R.id.imageView);
imageView.setBackground(new ShapeDrawable(new OvalShape()));
imageView.setClipToOutline(true);
}
}
@NonNull
@Override
public MyRecyclerAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyRecyclerAdapter.ViewHolder holder, int position) {
ItemData item = itemData.get(position);
holder.title.setText(item.getTitle());
holder.contents.setText(item.getContents());
holder.imageView.setImageResource(item.getImage());
}
@Override
public int getItemCount() {
return itemData.size();
}
}
MainActivity.java : R.mipmap.ic_launcher_round
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = findViewById(R.id.recyclerView);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
ArrayList<ItemData> dataList = new ArrayList<>();
for(int i=0; i<10; i++){
dataList.add(new ItemData(R.mipmap.ic_launcher_round,"TITLE "+i,
String.format("리사이클러뷰 %03d",i)));
}
MyRecyclerAdapter adapter = new MyRecyclerAdapter(dataList);
recyclerView.setAdapter(adapter);
}
}
고양이 그림 가운데 중심으로 자르기(CenterCrop / list_item.xml)
<ImageView
android:id="@+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:scaleType="centerCrop"
app:srcCompat="@mipmap/ic_launcher"/>
고양이 그림 원형으로 표시(MyRecyclerAdapter.java)
public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.ViewHolder>{
public class ViewHolder extends RecyclerView.ViewHolder {
TextView title;
TextView contents;
ImageView imageView;
public ViewHolder(@NonNull View itemView) {
super(itemView);
title = itemView.findViewById(R.id.title_text);
contents = itemView.findViewById(R.id.contents_text);
imageView = itemView.findViewById(R.id.imageView);
imageView.setBackground(new ShapeDrawable(new OvalShape()));
imageView.setClipToOutline(true);
}
}
}
고양이 순차
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = findViewById(R.id.recyclerView);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
int[] cat = {
R.drawable.pic_001, R.drawable.pic_002, R.drawable.pic_003, R.drawable.pic_004,
R.drawable.pic_005, R.drawable.pic_006, R.drawable.pic_007, R.drawable.pic_008,
R.drawable.pic_009,R.drawable.pic_010
};
ArrayList<ItemData> dataList = new ArrayList<>();
for(int i=0; i<10; i++){
dataList.add(new ItemData(cat[i],"TITLE "+i,String.format("리사이클러뷰 %03d",i)));
}
MyRecyclerAdapter adapter = new MyRecyclerAdapter(dataList);
recyclerView.setAdapter(adapter);
}
}
고양이 랜덤
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = findViewById(R.id.recyclerView);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
int[] cat = {
R.drawable.pic_001, R.drawable.pic_002, R.drawable.pic_003,
R.drawable.pic_004, R.drawable.pic_005, R.drawable.pic_006,
R.drawable.pic_007, R.drawable.pic_008, R.drawable.pic_009,
R.drawable.pic_010
};
ArrayList<ItemData> dataList = new ArrayList<>();
Random r = new Random();
for(int i=0; i<10; i++){
int catNum = r.nextInt(10);
dataList.add(new ItemData(cat[catNum],"TITLE "+i, String.format("리사이클러뷰 %03d",i)));
System.out.println(catNum);
}
MyRecyclerAdapter adapter = new MyRecyclerAdapter(dataList);
recyclerView.setAdapter(adapter);
}
}
아이템 클릭 이벤트
public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.ViewHolder>{
public interface MyRecyclerViewClickListener{
void onItemClicked(int position);
}
private MyRecyclerViewClickListener mListener;
public void setOnClickListener(MyRecyclerViewClickListener listener){
this.mListener = listener;
}
}
public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.ViewHolder>{
...
@Override
public void onBindViewHolder(@NonNull MyRecyclerAdapter.ViewHolder holder, int position) {
ItemData item = itemData.get(position);
holder.title.setText(item.getTitle());
holder.contents.setText(item.getContents());
holder.imageView.setImageResource(item.getImage());
if(mListener != null){
final int pos = position;
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mListener.onItemClicked(pos); // pos = holder.getAdapterPosition()
}
});
}
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity implements MyRecyclerAdapter.MyRecyclerViewClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = findViewById(R.id.recyclerView);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
int[] cat = {R.drawable.pic_001, R.drawable.pic_002,R.drawable.pic_003,R.drawable.pic_004,R.drawable.pic_005,
R.drawable.pic_006,R.drawable.pic_007,R.drawable.pic_008,R.drawable.pic_009,R.drawable.pic_010};
ArrayList<ItemData> dataList = new ArrayList<>();
for(int i=0; i<10; i++){
dataList.add(new ItemData(R.mipmap.ic_launcher_round,"TITLE "+i,String.format("리사이클러뷰 %03d",i)));
}
MyRecyclerAdapter adapter = new MyRecyclerAdapter(dataList);
recyclerView.setAdapter(adapter);
adapter.setOnClickListener(this);
}
@Override
public void onItemClicked(int position) {
Toast.makeText(getApplicationContext(),""+position,Toast.LENGTH_SHORT).show();
}
}
그림클릭시 이벤트 발생
1. MyRecyclerAdapter.java
public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.ViewHolder>{
private ArrayList<ItemData> itemData;
public interface MyRecyclerViewClickListener{
void onItemClicked(int position);
void onImageViewClicked(int position);
}
private MyRecyclerViewClickListener mListener;
public void setOnClickListener(MyRecyclerViewClickListener listener){
this.mListener = listener;
}
...
@Override
public void onBindViewHolder(@NonNull MyRecyclerAdapter.ViewHolder holder, int position) {
ItemData item = itemData.get(position);
holder.title.setText(item.getTitle());
holder.contents.setText(item.getContents());
holder.imageView.setImageResource(item.getImage());
if(mListener != null){
final int pos = position;
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mListener.onItemClicked(pos);
}
});
holder.imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mListener.onImageViewClicked(pos);
}
});
}
}
...
}
2. MainActivity.java
public class MainActivity extends AppCompatActivity implements MyRecyclerAdapter.MyRecyclerViewClickListener {
private int[] cat;
private String[] catName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = findViewById(R.id.recyclerView);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
cat = new int[]{
R.drawable.pic_001, R.drawable.pic_002,R.drawable.pic_003,
R.drawable.pic_004,R.drawable.pic_005,R.drawable.pic_006,
R.drawable.pic_007,R.drawable.pic_008,R.drawable.pic_009,
R.drawable.pic_010};
catName = new String[]{
"R.drawable.pic_001", "R.drawable.pic_002","R.drawable.pic_003",
"R.drawable.pic_004","R.drawable.pic_005", "R.drawable.pic_006",
"R.drawable.pic_007","R.drawable.pic_008","R.drawable.pic_009",
"R.drawable.pic_010"};
ArrayList<ItemData> dataList = new ArrayList<>();
for(int i=0; i<10; i++){
dataList.add(new ItemData(cat[i],"TITLE "+i,String.format("리사이클러뷰 %03d",i)));
}
MyRecyclerAdapter adapter = new MyRecyclerAdapter(dataList);
recyclerView.setAdapter(adapter);
adapter.setOnClickListener(this);
}
@Override
public void onItemClicked(int position) {
Toast.makeText(getApplicationContext(),""+position,Toast.LENGTH_SHORT).show();
}
@Override
public void onImageViewClicked(int position) {
Toast.makeText(getApplicationContext(),""+catName[position],Toast.LENGTH_SHORT).show();
}
}
버튼추가
'PROGRAM > Android' 카테고리의 다른 글
firebase db (0) | 2020.05.23 |
---|---|
리사이클러뷰-full (0) | 2020.05.10 |
안드로이드 검색 로그(ver.20-05-08) (0) | 2020.05.09 |
설명문 띄우기 (0) | 2020.05.08 |
Fragment 예제 (0) | 2020.05.08 |