MainActivity

...더보기
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
68
69
70
71
72
package com.example.boostcoursepractice;
 
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
 
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
 
import androidx.annotation.Nullable;
 
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
 
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
 
import java.io.File;
import java.net.URL;
import java.util.HashMap;
 
public class MainActivity extends AppCompatActivity {
 
    RecyclerView recyclerView;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
 
        LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
        recyclerView.setLayoutManager(layoutManager);
 
        final SingerAdapter adapter = new SingerAdapter(getApplicationContext());
 
        adapter.addItem(new SingerItem("소녀시대""010-2131-5133"));
        adapter.addItem(new SingerItem("티아라""010-2221-4353"));
        adapter.addItem(new SingerItem("여자친구""010-2134-2222"));
 
        adapter.setOnItemClickListener(new SingerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(SingerAdapter.ViewHolder holder, View view, int position) {
                SingerItem item = adapter.getItem(position);
 
                Toast.makeText(getApplicationContext(), "아이템 선택됨 : " + item.getName() + ", " + holder.textView2.getText().toString(), Toast.LENGTH_LONG).show();
            }
        });
 
        recyclerView.setAdapter(adapter);
 
 
    }
}
cs

 

SingerAdapter2(이게 더 맞는거 같음)

...더보기
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package com.example.boostcoursepractice;
 
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
 
import androidx.annotation.LongDef;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
 
import java.util.ArrayList;
 
public class SingerAdapter extends RecyclerView.Adapter<SingerAdapter.ViewHolder> {
 
    Context context;
 
    ArrayList<SingerItem> items = new ArrayList<SingerItem>();
 
    OnItemClickListener listener;
 
    public interface OnItemClickListener {
        public void onItemClick(ViewHolder holder, View view, int position);
    }
 
    public SingerAdapter(Context context) {
        this.context = context;
    }
 
    @Override
    public int getItemCount() {
        return items.size();
    }
 
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View itemView = inflater.inflate(R.layout.singer_item, parent, false);
 
        return new ViewHolder(itemView);
    }
 
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        SingerItem item = items.get(position);
        holder.setItem(item);
 
        holder.position = position;
    }
 
    public void addItem(SingerItem item) {
        items.add(item);
    }
 
    public void addItems(ArrayList<SingerItem> items) {
        this.items = items;
    }
 
    public SingerItem getItem(int position) {
        return items.get(position);
    }
 
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }
 
    class ViewHolder extends RecyclerView.ViewHolder {
 
        TextView textView;
        TextView textView2;
        int position;
 
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.textView);
            textView2 = itemView.findViewById(R.id.textView2);
 
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (listener != null) {
                        Log.d("jjh""포지션 : " + position);
                        listener.onItemClick(ViewHolder.this, view, position);
                    }
                }
            });
        }
 
        public void setItem(SingerItem item) {
            textView.setText(item.getName());
            textView2.setText(item.getMobile());
        }
    }
}
 
cs

 

SingerAdapter

...더보기
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package com.example.boostcoursepractice;
 
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
 
import androidx.annotation.LongDef;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
 
import java.util.ArrayList;
 
public class SingerAdapter extends RecyclerView.Adapter<SingerAdapter.ViewHolder>{
 
    Context context ;
 
    ArrayList<SingerItem> items = new ArrayList<SingerItem>();
 
    OnItemClickListener listener;
 
    int num = 0 ;
 
    public static interface OnItemClickListener{
        public void onItemClick(ViewHolder holder , View view, int position);
    }
 
    public SingerAdapter(Context context){
        this.context = context ;
    }
 
    @Override
    public int getItemCount() {
        return items.size();
    }
 
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View itemView = inflater.inflate(R.layout.singer_item, parent, false);
 
        return new ViewHolder(itemView, num++);
    }
 
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        SingerItem item = items.get(position);
        holder.setItem(item);
 
        holder.setOnItemClickListener(listener);
    }
 
    public void addItem(SingerItem item){
        items.add(item);
    }
 
    public void addItems(ArrayList<SingerItem> items){
        this.items = items ;
    }
 
    public SingerItem getItem(int position){
        return items.get(position);
    }
 
    public void setOnItemClickListener(OnItemClickListener listener){
        this.listener = listener;
    }
 
    static class ViewHolder extends RecyclerView.ViewHolder{
 
        TextView textView;
        TextView textView2;
 
        OnItemClickListener listener ;
 
        public ViewHolder(@NonNull View itemView, final int position) {
            super(itemView);
            textView = itemView.findViewById(R.id.textView);
            textView2 = itemView.findViewById(R.id.textView2);
 
            itemView.setOnClickListener(new View.OnClickListener() {
 
                @Override
                public void onClick(View view) {
                    if(listener != null){
                        Log.d("jjh","포지션 : " + position);
                        listener.onItemClick(ViewHolder.this, view, position);
                    }
                }
            });
        }
 
        public void setItem(SingerItem item){
            textView.setText(item.getName());
            textView2.setText(item.getMobile());
        }
 
        public void setOnItemClickListener(OnItemClickListener listener){
            this.listener = listener;
        }
 
    }
}
 
cs

 

SingerItem

...더보기
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.boostcoursepractice;
 
public class SingerItem {
 
    private String name ;
    private String mobile;
 
    public SingerItem(String name, String mobile) {
        this.name = name;
        this.mobile = mobile;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public String getMobile() {
        return mobile;
    }
 
    public void setMobile(String mobile) {
        this.mobile = mobile;
    }
}
 
cs

 

activity_main.xml

...더보기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?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"
    android:orientation="vertical">
 
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />
 
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></androidx.recyclerview.widget.RecyclerView>
 
</LinearLayout>
cs

 

singer_item.xml

...더보기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="이름"
        android:textColor="@color/colorAccent"
        android:textSize="30dp" />
 
    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="전화번호"
        android:textColor="@color/design_default_color_primary"
        android:textSize="20dp" />
</LinearLayout>
cs

 

우선 리싸이클러뷰는 리스트뷰를 만드는 것과 비슷하다. 하지만 여기에 뷰홀더라는 개념이 추가가 된다. 뷰홀더는 그냥 아이템 하나를 가지고 있는 클래스라고 생각하면 된다. 그리고 그 아이템을 가지고 있는 뷰홀더가 모여 어댑터가 되고 그 어댑터를 리싸이클뷰에 추가하면 완료된다. 우선 아이템의 레이아웃인 singer_item.xml 만들고 아이템의 데이터인 SingerItem 자바 클래스를 만들어 준다.(리스트뷰와 동일) 그 다음은 리싸이클뷰 어댑터를 만들어 준다 여기서는 SingerAdapter라고 만들어 주었다. 어댑터의 코드를 보면 

 

 @NonNull

    @Override

    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View itemView = inflater.inflate(R.layout.singer_item, parent, false);

 

        return new ViewHolder(itemView, num++);

    }

 

    @Override

    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {

        SingerItem item = items.get(position);

        holder.setItem(item);

 

        holder.setOnItemClickListener(listener);

    }

 

이런 코드가 있는데 onCreateViewHolder라는 콜백함수는 뷰홀더가 만들어 질 때 호출되는데 여기서 아까 만들어준 singer_item.xml을 인플레이션해주고 홀더 인스턴스 안에 넣어 리턴해준다. onBindViewHolder는 뷰와 홀더가 바인드 될 때 실행되는 함수로 여기에서 setItem으로 뷰 안에 여러 뷰들을 설정해준다. 이렇게 하고 다시 MainActivity로 돌아와 리사이클러뷰에 어댑터를 설정해 주면 되는데 그전에 

 

recyclerView = (RecyclerView) findViewById(R.id.recyclerView);

 

        LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);

        recyclerView.setLayoutManager(layoutManager);

 

위 처럼 레이아웃매너저를 만들어 리싸이클러뷰에 설정해 주어야 한다.

 

마지막으로 아이템이 클릭 되었을 때의 리스너는 따로 구현되어 있지 않기 때문에 직접 만들어주어야 한다. 여기에서는 어댑터 클래스에 구현해 주었다. 

 

 

 

결과화면

 

+ Recent posts