Hello, ๋๋'s world !
RecyclerView ๋ณธ๋ฌธ
์ค๋์ Android 5.0์์ ๋ฑ์ฅํ RecyclerView๋ฅผ ๋ง๋ค์ด ๋ณด๊ฒ ๋ค.์ฌ๋ฌ๊ฐ์ ์์ดํ ์ค ํ๋๋ฅผ ์ ํํ ์ ์๋ ๋ฆฌ์คํธ๋ชจ์์ ์์ ฏ์ '์ ํ์์ ฏ' ์ด๋ผํ๋ค.์ ํ์์ ฏ์ ์ด๋ํฐ(Adapter)ํจํด์ ์ฌ์ฉํ๋ค. ์ฆ, ๊ฐ๊ฐ ์์ดํ ์ ๋ทฐ๊ฐ์๋ ์ด๋ํฐ์์ ๊ด๋ฆฌํ๋ค.
RecycleView ๋ ๊ป๋ฐ๊ธฐ ์ญํ์ ํ๊ณ ์ด๋ํฐ๊ฐ ๋ฐ์ดํฐ์ ๋ทฐ๊ฐ์ฒด๋ฅผ ๊ด๋ฆฌํ๋ค.
๋ฆฌ์คํธ๋ฅผ ๊ตฌํํ ๋ RecycleView์ธ์ ListView๋ ์๋ค.์์ฆ ํด๋ํฐ๋ค์ด ์ข์์ ธ์ ํ์ ํน์ ์์ ์ ํธ์์๋ฐ๋ผ ListView๋ฅผ ์ฌ์ฉํด๋ ๋ฌด๋ฐฉํ์ง๋ง RecycleView์ ์ํ,์ข์ฐ ์คํฌ๋กค๊ธฐ๋ฅ ๋ฑ ์ ์ฐํ ํ๋ฉด๊ตฌ์ฑ๊ณผ, ์ข ๋ ํธํ๊ฒ ์ปค์คํ ์ ํ ์ ์์ด ์์๋๋ฉด ์ ์ฉํ๊ฒ ์ฌ์ฉํ ์ ์๋ค.
-
RecycleView๋ฅผ ์ฌ๋ ค์ค xml ๋ ์ด์์์ ๋ง๋ ๋ค.
-
์์ดํ ๋ค์ด ๋ค์ด๊ฐ RecycleView์ xml ๋ ์ด์์์ ๋ง๋ ๋ค.
-
์ด๋ํฐ์์ ๋ค์ด๊ฐ ๊ฐ ์์ดํ ์ ๋ฐ์ดํฐ๋ฅผ ๋ด์์ค ํด๋์ค๋ฅผ ๋ง๋ ๋ค.
-
์ดํญํฐ๋ฅผ ๋ง๋ ๋ค
-
๋ฆฌ์คํธ๋ฅผ ๋ณด์ฌ์ค ํด๋์ค์ ์ฌ๋ ค์ค๋ค.
๋ง๋ค๊ธฐ ์ build.gradle์ ๋ค์์ ์ถ๊ฐํด์ฃผ๊ณ ๋๊ธฐํ ์์ผ์ค๋ค.
ํ์๋ CardView๋ก ๋ณด์ฌ์ค๊ฒ์ด๊ธฐ ๋๋ฌธ์ CardView๋ ์ถ๊ฐํด์ฃผ์๋ค.
implementation 'com.android.support:recyclerview-v7:26.1.0'
implementation "androidx.cardview:cardview:1.0.0
1. activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/f_main_recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
2.main_item_list.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
app:cardCornerRadius="6dp"
app:cardElevation="6dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/name_tv"
android:layout_marginLeft="20dp"
android:layout_marginRight="10dp"
android:layout_gravity="center"
android:layout_width="35dp"
android:layout_height="wrap_content"
android:text="๋ฉ๋ชจ1"/>
<TextView
android:id="@+id/contants_tv"
android:layout_marginLeft="20dp"
android:layout_width="140dp"
android:layout_height="80dp"
android:layout_marginTop="20dp"
android:maxLines="2"
android:ellipsize="end"
android:gravity="left"
android:text="๋ด์ฉ์
๋๋ค ๋ธ๋ผ๋ธ๋ผ">
</TextView>
<TextView
android:id="@+id/date_tv"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="2020๋
01์ 07์ผ"
android:textSize="10dp"
android:gravity="right"
/>
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
3. Memo.java
public class Memo {
String mTitle;
String mContants;
String mDate;
public Memo(){}
public Memo(String mTitle, String mContants, String mDate) {
this.mTitle = mTitle;
this.mContants = mContants;
this.mDate = mDate;
}
public String getmTitle() { return mTitle; }
public void setmTitle(String mTitle) { this.mTitle = mTitle; }
public String getmContants() { return mContants; }
public void setmContants(String mContants) { this.mContants = mContants;}
public String getmDate() { return mDate; }
public void setmDate(String mDate) { this.mDate = mDate; }
}
4. MemoAdapter.java
public class MemoAdapter extends RecyclerView.Adapter<MemoAdapter.ViewHolder> {
ArrayList<Memo> mMemoList;
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.main_item_list,parent,false);
ViewHolder viewHolder=new ViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull MemoAdapter.ViewHolder holder, int position) {
Memo memo = mMemoList.get(position);
holder.title.setText(memo.getmTitle());
holder.contants.setText(memo.getmContants());
holder.date.setText(memo.getmDate());
}
@Override
public int getItemCount() { return mMemoList.size(); }
public void addItem(Memo memo) {;
mMemoList.add(memo);
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView title;
public TextView contants;
public TextView date;
public ViewHolder(@NonNull View itemView) {
super(itemView);
title = itemView.findViewById(R.id.name_tv);
contants = itemView.findViewById(R.id.contants_tv);
date = itemView.findViewById(R.id.date_tv);
}
}
}
5. MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = findViewById(R.id.main_recycler);
LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
recyclerView.setLayoutManager(layoutManager);
MemoAdapter mAdapter = new MemoAdapter();
mAdapter.addItem(new Memo("์ ๋ชฉ1","๋ด์ฉ1","๋ ์ง1"));
mAdapter.addItem(new Memo("์ ๋ชฉ2","๋ด์ฉ2","๋ ์ง2"));
mAdapter.addItem(new Memo("์ ๋ชฉ3","๋ด์ฉ3","๋ ์ง3"));
mAdapter.addItem(new Memo("์ ๋ชฉ4","๋ด์ฉ4","๋ ์ง4"));
mAdapter.addItem(new Memo("์ ๋ชฉ5","๋ด์ฉ5","๋ ์ง5"));
mAdapter.addItem(new Memo("์ ๋ชฉ6","๋ด์ฉ6","๋ ์ง6"));
mAdapter.addItem(new Memo("์ ๋ชฉ7","๋ด์ฉ7","๋ ์ง7"));
}
}
MemoAdapter์์ onBindViewHolder() ๋ถ๋ถ์ด ์๊พธ ์๋ฌ๋์ ์๊ทธ๋ฐ๊ฐ ํ๋๋ฐ...
๊ธฐ์กด์ ์๋ฌ์ฝ๋๋
holder.title.setText(mMemoList.get(position).getmTitle());
holder.contants.setText(mMemoList.get(position).getmContants());
holder.date.setText(mMemoList.get(position).getmDate());
์ด์๋ค .
์์ธํ๋ณด๋ mMemoList.get(position)์ ๋ฆฌํด๊ฐ์ String์ด ์๋๋ผ Memo์๋ ๊ฒ์ด๋ค.
ํ์ง๋ง ์ setText() ๋ฉ์๋์ ๋งค๊ฐ๋ณ์๋ String์ด์ด์ผ ํ๋ค.
๊ทธ๋ฌ๋ฏ๋ก
Memo memo = mMemoList.get(position);
holder.title.setText(memo.getmTitle());
holder.contants.setText(memo.getmContants());
holder.date.setText(memo.getmDate())
๊ฐ ์๋ง๋ ์ฝ๋์๋๊ฒ์ด๋ค :)
<๊ฐ๋ฐํ๊ฒฝ>
java version "1.8.0_271"
android API 10.0 (Q)
android studio "4.0.1"
<์ฐธ๊ณ >
Do in! ์๋๋ก์ด๋ ์ฑ ํ๋ก๊ทธ๋๋ฐ (์ ์ฌ๊ณค ์ง์)
'๐ Android' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Android Waveํจ๊ณผ ๋ฃ๊ธฐ WaveView (0) | 2021.01.20 |
---|---|
Fragment์ RecyclerView ์์ Fragment๋ก ๋ฐ์ดํฐ ์ ๋ฌํ๊ธฐ (0) | 2021.01.20 |
dialogFragment ์์ Fragment ๋ก ๋ฐ์ดํฐ ์ ๋ฌ (0) | 2021.01.18 |
onDraw() ๋ฅผ ํ์ฉํ์ฌ ๊ทธ๋ฆผ๊ทธ๋ฆฌ๊ธฐ (0) | 2021.01.08 |
DialogFragment (0) | 2021.01.07 |