1. 실행 결과 화면


  



2. 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
<?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="추가하기"/>
 
   <ListView
       android:id="@+id/list_view"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_marginTop="8dp"/>
 
</LinearLayout>
 
cs



3. list_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
<?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"
    android:orientation="horizontal">
 
    <ImageView
        android:id="@+id/item_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/list_item1"/>
 
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:orientation="vertical">
        <TextView
            android:id="@+id/item_name"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:textSize="20sp"
            android:textStyle="bold"/>
        <TextView
            android:id="@+id/item_number"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:textSize="18sp"/>
    </LinearLayout>
</LinearLayout>
 
cs



4. ListItem.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
package com.tistory.qlyh8.pracitice;
 
public class ListItem {
 
    private String name, number;
    private int imgId;
 
    public ListItem(String name, String number, int imgId) {
        this.name  = name;
        this.number = number;
        this.imgId = imgId;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public String getNumber() {
        return number;
    }
 
    public void setNumber(String number) {
        this.number = number;
    }
 
    public int getImgId() {
        return imgId;
    }
 
    public void setImgId(int imgId) {
        this.imgId = imgId;
    }
}
 
cs



5. ListItemView.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
package com.tistory.qlyh8.pracitice;
 
 
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
 
public class ListItemView extends LinearLayout {
 
    ImageView imageView;
    TextView textViewName, textViewNumber;
 
    public ListItemView(Context context) {
        super(context);
        init(context);
    }
 
    public ListItemView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }
 
    public void init(Context context){
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        layoutInflater.inflate(R.layout.list_item, thistrue);
 
        textViewName = findViewById(R.id.item_name);
        textViewNumber = findViewById(R.id.item_number);
        imageView = findViewById(R.id.item_image);
    }
 
    public void setName(String str){
        textViewName.setText(str);
    }
 
    public void setNumber(String str){
        textViewNumber.setText(str);
    }
 
    public void setImageView(int imgId){
        imageView.setImageResource(imgId);
    }
}
 
cs



6. 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.ListView;
import android.widget.Toast;
 
import java.util.ArrayList;
 
public class MainActivity extends AppCompatActivity {
 
    ListView listView;
    ListAdapter listAdapter;
    EditText editTextName, editTextNumber;
    Button button;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = findViewById(R.id.list_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));
        listView.setAdapter(listAdapter);
 
        listView.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



** 참고


리스트에 이미지가 있는 경우에 부드럽게 처리하는 방법

https://developer.android.com/training/improving-layouts/smooth-scrolling


안드로이드 공식 리스트뷰 소개

https://developer.android.com/guide/topics/ui/layout/listview?hl=ko





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

'Android' 카테고리의 다른 글

GridView (그리드뷰)  (0) 2018.05.07
Spinner (스피너)  (0) 2018.05.07
ListView (리스트뷰)  (0) 2018.05.06
Inflation (인플레이션)  (0) 2018.05.01
Bitmap Button 만들기  (0) 2018.05.01

+ Recent posts