Bläddra i källkod

ListView Adapter 추가 ListViewItem 추가 MainActivity ListView 추가

ListView Item Layout 추가
june9152 6 år sedan
förälder
incheckning
00a4a232c8

+ 2 - 1
app/src/main/AndroidManifest.xml

@@ -9,7 +9,7 @@
9 9
         android:roundIcon="@mipmap/ic_launcher_round"
10 10
         android:supportsRtl="true"
11 11
         android:theme="@style/Theme.AppCompat.Light.NoActionBar">
12
-        <activity android:name=".MainActivity"></activity>
12
+        <activity android:name=".MainActivity" />
13 13
         <activity android:name=".IntroActivity">
14 14
             <intent-filter>
15 15
                 <action android:name="android.intent.action.MAIN" />
@@ -18,4 +18,5 @@
18 18
             </intent-filter>
19 19
         </activity>
20 20
     </application>
21
+
21 22
 </manifest>

+ 76 - 0
app/src/main/java/com/example/repeater/ListViewAdapter.java

@@ -0,0 +1,76 @@
1
+package com.example.repeater;
2
+
3
+import android.content.Context;
4
+import android.graphics.drawable.Drawable;
5
+import android.view.LayoutInflater;
6
+import android.view.View;
7
+import android.view.ViewGroup;
8
+import android.widget.BaseAdapter;
9
+import android.widget.ImageView;
10
+import android.widget.TextView;
11
+
12
+import java.util.ArrayList;
13
+
14
+public class ListViewAdapter extends BaseAdapter {
15
+    // Adapter에 추가된 데이터를 저장하기 위한 ArrayList
16
+    private ArrayList<ListViewItem> listViewItemList = new ArrayList<ListViewItem>() ;
17
+
18
+    // ListViewAdapter의 생성자
19
+    public ListViewAdapter() {
20
+
21
+    }
22
+
23
+    // Adapter에 사용되는 데이터의 개수를 리턴. : 필수 구현
24
+    @Override
25
+    public int getCount() {
26
+        return listViewItemList.size() ;
27
+    }
28
+
29
+    // position에 위치한 데이터를 화면에 출력하는데 사용될 View를 리턴. : 필수 구현
30
+    @Override
31
+    public View getView(int position, View convertView, ViewGroup parent) {
32
+        final int pos = position;
33
+        final Context context = parent.getContext();
34
+
35
+        // "listview_item" Layout을 inflate하여 convertView 참조 획득.
36
+        if (convertView == null) {
37
+            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
38
+            convertView = inflater.inflate(R.layout.activity_list_view_item, parent, false);
39
+        }
40
+
41
+        // 화면에 표시될 View(Layout이 inflate된)으로부터 위젯에 대한 참조 획득
42
+        TextView descTextView = (TextView) convertView.findViewById(R.id.textView13) ;
43
+        TextView valTextView = (TextView) convertView.findViewById(R.id.textView14) ;
44
+
45
+        // Data Set(listViewItemList)에서 position에 위치한 데이터 참조 획득
46
+        ListViewItem listViewItem = listViewItemList.get(position);
47
+
48
+        // 아이템 내 각 위젯에 데이터 반영
49
+        descTextView.setText(listViewItem.getDesc());
50
+        valTextView.setText(listViewItem.getVal());
51
+
52
+        return convertView;
53
+    }
54
+
55
+    // 지정한 위치(position)에 있는 데이터와 관계된 아이템(row)의 ID를 리턴. : 필수 구현
56
+    @Override
57
+    public long getItemId(int position) {
58
+        return position ;
59
+    }
60
+
61
+    // 지정한 위치(position)에 있는 데이터 리턴 : 필수 구현
62
+    @Override
63
+    public Object getItem(int position) {
64
+        return listViewItemList.get(position) ;
65
+    }
66
+
67
+    // 아이템 데이터 추가를 위한 함수. 개발자가 원하는대로 작성 가능.
68
+    public void addItem(String desc,String val) {
69
+        ListViewItem item = new ListViewItem();
70
+
71
+        item.setDesc(desc);
72
+        item.setVal(val);
73
+
74
+        listViewItemList.add(item);
75
+    }
76
+}

+ 23 - 0
app/src/main/java/com/example/repeater/ListViewItem.java

@@ -0,0 +1,23 @@
1
+package com.example.repeater;
2
+
3
+import android.graphics.drawable.Drawable;
4
+import android.support.v7.app.AppCompatActivity;
5
+import android.os.Bundle;
6
+
7
+public class ListViewItem {
8
+    private String descStr ;
9
+    private String valStr ;
10
+
11
+    public void setDesc(String desc) {
12
+        descStr = desc ;
13
+    }
14
+    public String getDesc() {
15
+        return this.descStr ;
16
+    }
17
+    public void setVal(String val) {
18
+        valStr = val ;
19
+    }
20
+    public String getVal() {
21
+        return this.valStr ;
22
+    }
23
+}

+ 49 - 1
app/src/main/java/com/example/repeater/MainActivity.java

@@ -1,8 +1,11 @@
1 1
 package com.example.repeater;
2 2
 
3
+import android.support.v4.content.ContextCompat;
3 4
 import android.support.v7.app.AppCompatActivity;
4 5
 import android.os.Bundle;
6
+import android.view.View;
5 7
 import android.widget.ArrayAdapter;
8
+import android.widget.ImageButton;
6 9
 import android.widget.ListView;
7 10
 import android.widget.SimpleAdapter;
8 11
 
@@ -23,16 +26,48 @@ public class MainActivity extends AppCompatActivity {
23 26
     private HashMap<String,String> InputData10 = new HashMap<>();
24 27
     private HashMap<String,String> InputData11 = new HashMap<>();
25 28
     private HashMap<String,String> InputData12 = new HashMap<>();
26
-
27 29
     private ListView listview_info;
30
+    private ListView listview_info01;
28 31
     static final String[] LIST_MENU = {"LIST1", "LIST2", "LIST3"} ;
29 32
 
33
+
34
+    private ArrayList<ListViewItem> listViewItemList = new ArrayList<ListViewItem>() ;
35
+
36
+
37
+    ImageButton imageButton01;
38
+    ImageButton imageButton02;
30 39
     @Override
31 40
     protected void onCreate(Bundle savedInstanceState) {
32 41
 
33 42
         super.onCreate(savedInstanceState);
34 43
         setContentView(R.layout.activity_main);
35 44
 
45
+
46
+        imageButton01 = findViewById(R.id.imageButton01);
47
+        imageButton02 = findViewById(R.id.imageButton02);
48
+        listview_info = findViewById(R.id.listview_info);
49
+        listview_info01 = findViewById(R.id.listview_info01);
50
+        imageButton02 = findViewById(R.id.imageButton02);
51
+
52
+        ListView listview ;
53
+        ListViewAdapter adapter;
54
+
55
+        // Adapter 생성
56
+        adapter = new ListViewAdapter() ;
57
+
58
+        // 리스트뷰 참조 및 Adapter달기
59
+        listview = (ListView) findViewById(R.id.listview_info);
60
+        listview.setAdapter(adapter);
61
+
62
+        // 첫 번째 아이템 추가.
63
+        adapter.addItem("firm info.","0.1Ver") ;
64
+        // 두 번째 아이템 추가.
65
+        adapter.addItem("FPGA Ver.","0.2Ver") ;
66
+        // 세 번째 아이템 추가.
67
+        adapter.addItem("F/W Ver.","0.3Ver") ;
68
+/*
69
+
70
+
36 71
         listview_info =(ListView)findViewById(R.id.listview_info);
37 72
 
38 73
         //데이터 초기화
@@ -66,6 +101,19 @@ public class MainActivity extends AppCompatActivity {
66 101
         listview_info.setAdapter(simpleAdapter);
67 102
 //        ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_2, LIST_MENU) ;
68 103
 //        listview_info.setAdapter(adapter);
104
+*/
105
+    }
69 106
 
107
+    public void onClick(View view) {
108
+        switch(view.getId()) {
109
+            case R.id.imageButton01:
110
+                listview_info.setVisibility(view.VISIBLE);
111
+                listview_info01.setVisibility(view.INVISIBLE);
112
+                break;
113
+            case R.id.imageButton02:
114
+                listview_info.setVisibility(view.INVISIBLE);
115
+                listview_info01.setVisibility(view.VISIBLE);
116
+                break;
117
+        }
70 118
     }
71 119
 }

+ 31 - 0
app/src/main/res/layout/activity_list_view_item.xml

@@ -0,0 +1,31 @@
1
+<?xml version="1.0" encoding="utf-8"?>
2
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3
+    android:orientation="horizontal" android:layout_width="match_parent"
4
+    android:layout_height="match_parent">
5
+
6
+        <LinearLayout
7
+            android:layout_width="match_parent"
8
+            android:layout_height="match_parent"
9
+            android:layout_weight="2"
10
+            android:orientation="horizontal">
11
+
12
+                <TextView
13
+                    android:id="@+id/textView13"
14
+                    android:layout_width="match_parent"
15
+                    android:layout_height="match_parent"
16
+                    android:layout_weight="2"
17
+                    android:gravity="center_vertical"
18
+                    android:text="New Text"
19
+                    android:textColor="#000000"
20
+                    android:textSize="24dp" />
21
+
22
+                <TextView
23
+                    android:id="@+id/textView14"
24
+                    android:layout_marginLeft="20dp"
25
+                    android:layout_width="match_parent"
26
+                    android:layout_height="match_parent"
27
+                    android:layout_weight="2"
28
+                    android:text="TextView" />
29
+        </LinearLayout>
30
+
31
+</LinearLayout>

+ 30 - 5
app/src/main/res/layout/activity_main.xml

@@ -8,7 +8,6 @@
8 8
     android:orientation="vertical">
9 9
 
10 10
 
11
-
12 11
     <LinearLayout
13 12
         android:layout_width="match_parent"
14 13
         android:layout_height="wrap_content"
@@ -93,17 +92,43 @@
93 92
             android:contentDescription="TODO"
94 93
             tools:ignore="ContentDescription,HardcodedText" />
95 94
     </LinearLayout>
96
-
97 95
     <LinearLayout
98 96
         android:layout_width="match_parent"
99 97
         android:layout_height="wrap_content"
100
-        android:orientation="vertical">
98
+        android:orientation="horizontal">
99
+
100
+        <ImageButton
101
+            android:id="@+id/imageButton01"
102
+            android:layout_width="wrap_content"
103
+            android:layout_height="wrap_content"
104
+            android:layout_weight="1"
105
+            android:onClick="onClick"
106
+            app:srcCompat="@android:drawable/dialog_holo_dark_frame" />
107
+        <ImageButton
108
+            android:id="@+id/imageButton02"
109
+            android:layout_width="wrap_content"
110
+            android:layout_height="wrap_content"
111
+            android:layout_weight="1"
112
+            android:onClick="onClick"
113
+            app:srcCompat="@android:drawable/dialog_holo_dark_frame" />
114
+    </LinearLayout>
115
+
116
+    <FrameLayout
117
+        android:layout_width="match_parent"
118
+        android:layout_height="match_parent">
101 119
 
102 120
         <ListView
103 121
             android:id="@+id/listview_info"
104 122
             android:layout_width="wrap_content"
105 123
             android:layout_height="wrap_content"
124
+            android:visibility="invisible"
106 125
             />
107
-    </LinearLayout>
108
-
126
+        <ListView
127
+            android:id="@+id/listview_info01"
128
+            android:layout_width="wrap_content"
129
+            android:layout_height="wrap_content"
130
+            android:visibility="visible"
131
+            />
132
+    </FrameLayout>
133
+     
109 134
 </LinearLayout>