1. GridView


그리드뷰는 격자 형태로 아이템이 보이는 선택위젯이다.


그리드뷰 또한 선택위젯이기 때문에 어댑터를 사용하며, 사용 방법은 리스트뷰와 유사하다.

리스트뷰와 다른 점은 그리드뷰는 하나의 칼럼이 아니라 여러 개의 칼럼을 사용할 수 있기 때문에

numColumns 속성을 사용한다.



2. 실행 결과 화면




3. 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
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.tistory.qlyh8.pracitice.MainActivity"
    android:layout_margin="8dp"
    android:orientation="vertical">
 
   <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="horizontal">
      <EditText
          android:id="@+id/edit_text_name"
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:hint="이름"/>
      <EditText
          android:id="@+id/edit_text_number"
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:hint="전화번호"
          android:layout_marginLeft="8dp"/>
   </LinearLayout>
 
   <Button
       android:id="@+id/btn_add"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:text="추가하기"/>
 
   <GridView
       android:id="@+id/grid_view"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_marginTop="8dp"
       android:numColumns="2"/>
 
</LinearLayout>
 
cs



4. 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
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
package com.tistory.qlyh8.pracitice;
 
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.Toast;
 
import java.util.ArrayList;
 
public class MainActivity extends AppCompatActivity {
 
    GridView gridView;
    ListAdapter listAdapter;
    EditText editTextName, editTextNumber;
    Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        gridView = findViewById(R.id.grid_view);
        editTextName = findViewById(R.id.edit_text_name);
        editTextNumber = findViewById(R.id.edit_text_number);
        button = findViewById(R.id.btn_add);
 
        listAdapter = new ListAdapter();
        listAdapter.addItem(new ListItem("김철수""010-1111-1111", R.drawable.list_item1));
        listAdapter.addItem(new ListItem("이나라""010-2222-2222", R.drawable.list_item2));
        listAdapter.addItem(new ListItem("신원호""010-3333-3333", R.drawable.list_item3));
        listAdapter.addItem(new ListItem("정나정""010-4444-4444", R.drawable.list_item4));
        listAdapter.addItem(new ListItem("이지윤""010-5555-5555", R.drawable.list_item5));
        gridView.setAdapter(listAdapter);
 
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                ListItem item = (ListItem) listAdapter.getItem(position);
                Toast.makeText(getApplication(), item.getName(), Toast.LENGTH_SHORT).show();
            }
        });
 
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String name = editTextName.getText().toString();
                String number = editTextNumber.getText().toString();
 
                listAdapter.addItem(new ListItem(name, number, R.drawable.list_item1));
                listAdapter.notifyDataSetChanged();
            }
        });
    }
 
    class ListAdapter extends BaseAdapter {
 
        ArrayList<ListItem> listItem = new ArrayList<>();
 
        public void addItem(ListItem item){
            listItem.add(item);
        }
 
        @Override
        public int getCount() {
            return listItem.size();
        }
 
        @Override
        public Object getItem(int position) {
            return listItem.get(position);
        }
 
        @Override
        public long getItemId(int position) {
            return position;
        }
 
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // 화면에 보이지 않는 뷰를 다시 재사용할 수 있게 하여 데이터를 모두 생성할 필요 없게 한다.
            ListItemView listItemView = null;
            if(convertView == null)
                listItemView = new ListItemView(getApplicationContext());
            else
                listItemView = (ListItemView) convertView;  // convertView: 이전에 썼던 뷰
 
            ListItem item = listItem.get(position);
 
            listItemView.setName(item.getName());
            listItemView.setNumber(item.getNumber());
            listItemView.setImageView(item.getImgId());
 
            return listItemView;
        }
    }
}
 
cs



5. listItem.xml, ListItem.java, ListItemView.java

리스트뷰 코드와 동일





출처: https://www.edwith.org/boostcourse-android/lecture/17059/

'Android' 카테고리의 다른 글

애플리케이션 구성요소와 액티비티 전환 (1)  (0) 2018.05.19
Android Studio와 Git 연동하기  (0) 2018.05.18
Spinner (스피너)  (0) 2018.05.07
ListView (리스트뷰) - 예제  (0) 2018.05.06
ListView (리스트뷰)  (0) 2018.05.06

+ Recent posts