Hello, ๋‚˜๋‚˜'s world !

RecyclerView ๋ณธ๋ฌธ

๐Ÿ’š Android

RecyclerView

Nana0 2021. 1. 8. 15:18

RecyclerView

 

 

์˜ค๋Š˜์€ Android 5.0์—์„œ ๋“ฑ์žฅํ•œ RecyclerView๋ฅผ ๋งŒ๋“ค์–ด ๋ณด๊ฒ ๋‹ค.์—ฌ๋Ÿฌ๊ฐœ์˜ ์•„์ดํ…œ์ค‘ ํ•˜๋‚˜๋ฅผ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋Š” ๋ฆฌ์ŠคํŠธ๋ชจ์–‘์˜ ์œ„์ ฏ์„ '์„ ํƒ์œ„์ ฏ' ์ด๋ผํ•œ๋‹ค.์„ ํƒ์œ„์ ฏ์€ ์–ด๋Œ‘ํ„ฐ(Adapter)ํŒจํ„ด์„ ์‚ฌ์šฉํ•œ๋‹ค. ์ฆ‰, ๊ฐ๊ฐ ์•„์ดํ…œ์€ ๋ทฐ๊ฐ€์•„๋‹Œ ์–ด๋Œ‘ํ„ฐ์—์„œ ๊ด€๋ฆฌํ•œ๋‹ค.

RecycleView ๋Š” ๊ป๋ฐ๊ธฐ ์—ญํ™œ์„ ํ•˜๊ณ  ์–ด๋Œ‘ํ„ฐ๊ฐ€ ๋ฐ์ดํ„ฐ์™€ ๋ทฐ๊ฐ์ฒด๋ฅผ ๊ด€๋ฆฌํ•œ๋‹ค.

 

๋ฆฌ์ŠคํŠธ๋ฅผ ๊ตฌํ˜„ํ• ๋•Œ RecycleView์™ธ์— ListView๋„ ์žˆ๋‹ค.์š”์ฆ˜ ํœด๋Œ€ํฐ๋“ค์ด ์ข‹์•„์ ธ์„œ ํŒ€์› ํ˜น์‹œ ์ž์‹ ์˜ ํŽธ์˜์—๋”ฐ๋ผ ListView๋ฅผ ์‚ฌ์šฉํ•ด๋„ ๋ฌด๋ฐฉํ•˜์ง€๋งŒ RecycleView์˜ ์ƒํ•˜,์ขŒ์šฐ ์Šคํฌ๋กค๊ธฐ๋Šฅ ๋“ฑ ์œ ์—ฐํ•œ ํ™”๋ฉด๊ตฌ์„ฑ๊ณผ, ์ข€ ๋” ํŽธํ•˜๊ฒŒ ์ปค์Šคํ…€์„ ํ•  ์ˆ˜ ์žˆ์–ด ์•Œ์•„๋‘๋ฉด ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

 

 

  1. RecycleView๋ฅผ ์˜ฌ๋ ค์ค„ xml ๋ ˆ์ด์•„์›ƒ์„ ๋งŒ๋“ ๋‹ค.

  2. ์•„์ดํ…œ๋“ค์ด ๋“ค์–ด๊ฐˆ RecycleView์˜ xml ๋ ˆ์ด์•„์›ƒ์„ ๋งŒ๋“ ๋‹ค.

  3. ์–ด๋Œ‘ํ„ฐ์•ˆ์— ๋“ค์–ด๊ฐˆ ๊ฐ ์•„์ดํ…œ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด์•„์ค„ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ ๋‹ค. 

  4. ์–ดํƒญํ„ฐ๋ฅผ ๋งŒ๋“ ๋‹ค

  5. ๋ฆฌ์ŠคํŠธ๋ฅผ ๋ณด์—ฌ์ค„ ํด๋ž˜์Šค์— ์˜ฌ๋ ค์ค€๋‹ค.

 

 

 

๋งŒ๋“ค๊ธฐ ์ „ 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! ์•ˆ๋“œ๋กœ์ด๋“œ ์•ฑ ํ”„๋กœ๊ทธ๋ž˜๋ฐ (์ •์žฌ๊ณค ์ง€์Œ)

Comments