반응형

이 게시물은 다음 링크를 참조하여 학습했습니다.

 

안드로이드 리사이클러뷰 기본 사용법. (Android RecyclerView)

1. 안드로이드 리사이클러뷰(RecyclerView) 리사이클러뷰(RecyclerView)는, "많은 수의 데이터 집합을, 제한된 영역 내에서 유연하게(flexible) 표시할 수 있도록 만들어주는 위젯"입니다. [안드로이드 개발

recipes4dev.tistory.com

 

[안드로이드 스튜디오 독학#17] Custom ListView

이 게시물은 다음 링크를 참조하여 학습했습니다. LayoutInflater  | Android 개발자  | Android Developers developer.android.com ContextCompat  | Android 개발자  | Android Developers ContextCompat pu..

seminzzang.tistory.com

 

리사이클러뷰를 구현하려고 찾아보는데, 생각보다 복잡해서 자료를 찾는데 고생을 좀했다.....

리스트뷰하고 비슷한 또다른 리스트뷰 느낌일줄 알았는데 아니였다.

지난 시간에 학습한 리스트뷰와 최대한 형태를 맞추려고 노력했다. 

activity_main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:gravity="center_horizontal"
    android:orientation="vertical">
 
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Custom Recycler View"
        android:textSize="30dp"/>
 
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
 
</LinearLayout>
cs

recycler_item.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
 
    <ImageView
        android:id="@+id/iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@mipmap/ic_launcher_round"/>
 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/iv"
        android:orientation="vertical"
        android:padding="5dp">
 
        <TextView
            android:id="@+id/tv_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="semin"
            android:textSize="20dp"/>
        <TextView
            android:id="@+id/tv_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="text"/>
    </LinearLayout>
    
</RelativeLayout>
cs

rvItem.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.example.customrecyclerview;
 
import android.graphics.drawable.Drawable;
 
public class rvItem {
    private Drawable d;
    private String tv_1;
    private String tv_2;
 
    public Drawable getD() {        return d;    }
 
    public void setD(Drawable d) {        this.d = d;    }
 
    public String getTv_1() {        return tv_1;    }
 
    public void setTv_1(String tv_1) {        this.tv_1 = tv_1;    }
 
    public String getTv_2() {        return tv_2;    }
 
    public void setTv_2(String tv_2) {        this.tv_2 = tv_2;    }
}
 
cs

rvAdapter.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package com.example.customrecyclerview;
 
import android.content.Context;
import android.graphics.drawable.Drawable;
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 rvAdapter extends RecyclerView.Adapter<rvAdapter.ViewHolder> {
    private ArrayList<rvItem> lists = new ArrayList<>();
    // 아이템 뷰를 저장하는 뷰홀더 클래스
    public class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView d;
        private TextView tv_1;
        private TextView tv_2;
 
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
 
            tv_1 = itemView.findViewById(R.id.tv_name);
            tv_2 = itemView.findViewById(R.id.tv_text);
            d = itemView.findViewById(R.id.iv);
        }
    }
    //아이템뷰를 위한 뷰홀더 객체 생성하여 리턴
    @Override
    public rvAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = (LayoutInflater)parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 
        View view = inflater.inflate(R.layout.recycler_item,parent,false);
        rvAdapter.ViewHolder vh = new ViewHolder(view);
 
        return vh;
    }
    //position에 해당하는 데이터 뷰홀더의 아이템뷰에 표시
    @Override
    public void onBindViewHolder(@NonNull rvAdapter.ViewHolder holder, int position) {
        rvItem item = lists.get(position);
 
        holder.tv_1.setText(item.getTv_1());
        holder.tv_2.setText(item.getTv_2());
        holder.d.setImageDrawable(item.getD());
    }
    //전체 데이터 갯수 리턴
    @Override
    public int getItemCount() {
        return lists.size();
    }
    //데이터 추가를 위해 만든 custom 함수
    public void addItem(Drawable d, String name, String text){
        rvItem r =new rvItem();
 
        r.setD(d);
        r.setTv_1(name);
        r.setTv_2(text);
 
        lists.add(r);
    }
}
 
cs

MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.example.customrecyclerview;
 
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
 
import android.graphics.drawable.Drawable;
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.rv);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
 
        rvAdapter adapter = new rvAdapter();
        recyclerView.setAdapter(adapter);
 
        adapter.addItem(ContextCompat.getDrawable(this, R.drawable.icon3), "semin""hi");
        adapter.addItem(ContextCompat.getDrawable(this, R.drawable.icon3), "semin""hi");
        adapter.addItem(ContextCompat.getDrawable(this, R.drawable.icon3), "semin""hi");
        adapter.addItem(ContextCompat.getDrawable(this, R.drawable.icon3), "semin""hi");
    }
}
cs

1. ViewHolder

리사이클러뷰의 리스트뷰와 가장 다른 점은 "뷰홀더"이다.

리스트뷰는 BaseAdapter를 상속받아 어댑터를 구현했던것에 반해, 리사이클러뷰는 어댑터를 직접 구현해야 한다.

새로 만드는 어댑터는 RecyclerView.Adapter를 상속받아야 하는데, 이때 오버라이드 되는 메서드는 다음과 같다.

메서드 설명
onCreateViewHolder(ViewGroup parent int viewType) viewType 형태의 아이템 뷰를 위한 뷰홀더 객체 생성
onBindViewHolder(ViewHolder holder, int position) position에 해당하는 데이터를 뷰홀더의 아이템뷰에 표시
getItemCount 전체 아이템 갯수 리턴

 

2. 방향 설정

 리사이클러뷰는 방향을 조정할 수 있는데 MainActivity.java의 setLayoutManager() 를

1
    recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)) ;
cs

로 바꿔주면 수평방향의 뷰를 사용할 수 있다.

Custom RecyclerView

반응형

+ Recent posts