ListView에 이어서 RecyclerView를 만들어보자
이제부터는 프로젝트 만드는건 생략함.
물론 이론 설명도 생략.
- ListView는 세로로 리스트가 나타나지만 RecyclerView는 가로로 리스트를 나타낼 수도 있다!
레이아웃 구현
- activity_main.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=".MainActivity">
<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>
- activity_recycler_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>
객체 및 기능 구현
- RecyclerViewItem.java
package com.example.example_recyclerview;
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;
}
}
- RecyclerViewAdpater.java
: RecyclerView에도 어댑터가 필요함
package com.example.example_recyclerview;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
public class ViewHolder extends RecyclerView.ViewHolder {
ImageView imgView_item;
TextView txt_main;
TextView txt_sub;
public ViewHolder(@NonNull View itemView) {
super(itemView);
imgView_item = (ImageView) itemView.findViewById(R.id.imgView_item);
txt_main = (TextView) itemView.findViewById(R.id.txt_main);
txt_sub = (TextView) itemView.findViewById(R.id.txt_sub);
}
}
private ArrayList<RecyclerViewItem> mList = null;
public RecyclerViewAdapter(ArrayList<RecyclerViewItem> mList) {
this.mList = mList;
}
// 아이템 뷰를 위한 뷰홀더 객체를 생성하여 리턴
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.activity_recycler_item, parent, false);
RecyclerViewAdapter.ViewHolder vh = new RecyclerViewAdapter.ViewHolder(view);
return vh;
}
// position에 해당하는 데이터를 뷰홀더의 아이템뷰에 표시
@Override
public void onBindViewHolder(@NonNull RecyclerViewAdapter.ViewHolder holder, int position) {
RecyclerViewItem item = mList.get(position);
holder.imgView_item.setImageResource(R.drawable.ic_launcher_background); // 사진 없어서 기본 파일로 이미지 띄움
holder.txt_main.setText(item.getMainText());
holder.txt_sub.setText(item.getSubText());
}
@Override
public int getItemCount() {
return mList.size();
}
}
- MainActivity.java
package com.example.example_recyclerview;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private ArrayList<RecyclerViewItem> mList;
private RecyclerViewAdapter mRecyclerViewAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firstInit();
for(int i=0;i<5;i++){
addItem("iconName", "Taek" + i, "taek2.tistory.com");
}
mRecyclerViewAdapter = new RecyclerViewAdapter(mList);
mRecyclerView.setAdapter(mRecyclerViewAdapter);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
// mRecyclerView.setLayoutManager(new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false));
}
public void firstInit(){
mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
mList = new ArrayList<>();
}
public void addItem(String imgName, String mainText, String subText){
RecyclerViewItem item = new RecyclerViewItem();
item.setImgName(imgName);
item.setMainText(mainText);
item.setSubText(subText);
mList.add(item);
}
}
실행 결과
: 잘 나온다
+ 참고
mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); // 세로
mRecyclerView.setLayoutManager(new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false)); // 가로
- setLayoutManager 에서 위와같이 변경해주면 가로로 리스트가 생성된다.
'앱 개발 > 안드로이드(Java)' 카테고리의 다른 글
RecyclerView ScrollBar 설정 (0) | 2021.07.10 |
---|---|
RecyclerView 아이템 클릭 리스너 구현하기 (0) | 2021.06.17 |
ListView를 커스텀해서 구현하기 (0) | 2021.06.11 |
Retrofit2를 사용하여 서버(node.js)와 Post/Get/Put/Delete 통신하기 (2/2) (0) | 2021.06.10 |
Retrofit2를 사용하여 서버(node.js)와 Post/Get/Put/Delete 통신하기 (1/2) (1) | 2021.06.09 |