Android Samples
Revision | bc4dfd2e4a7f6f64c6f8edd7a83a38b94b3802ca (tree) |
---|---|
Zeit | 2014-06-13 03:57:37 |
Autor | Masahiko, SAWAI <say@user...> |
Commiter | Masahiko, SAWAI |
Added listview-single-choice-custom-layout module
@@ -0,0 +1,21 @@ | ||
1 | +<?xml version="1.0" encoding="utf-8"?> | |
2 | +<manifest | |
3 | + xmlns:android="http://schemas.android.com/apk/res/android" | |
4 | + package="com.example.hello.android.listview_single_choice_custom_layout" | |
5 | + android:versionCode="1" | |
6 | + android:versionName="1.0" | |
7 | +> | |
8 | + <uses-sdk android:minSdkVersion="3" android:targetSdkVersion="19" /> | |
9 | + | |
10 | + <application android:label="@string/app_name"> | |
11 | + <activity | |
12 | + android:name="MainActivity" | |
13 | + android:label="@string/app_name" | |
14 | + > | |
15 | + <intent-filter> | |
16 | + <action android:name="android.intent.action.MAIN" /> | |
17 | + <category android:name="android.intent.category.LAUNCHER" /> | |
18 | + </intent-filter> | |
19 | + </activity> | |
20 | + </application> | |
21 | +</manifest> |
@@ -0,0 +1,46 @@ | ||
1 | + | |
2 | +all : package | |
3 | + | |
4 | +#################### build | |
5 | + | |
6 | +compile : | |
7 | + mvn $@ | |
8 | + | |
9 | +gen : | |
10 | + mvn android:generate-sources | |
11 | + | |
12 | +package : | |
13 | + mvn $@ | |
14 | + | |
15 | +release : | |
16 | + mvn clean -Prelease package | |
17 | + | |
18 | +install : | |
19 | + mvn $@ | |
20 | + | |
21 | +deploy : | |
22 | + mvn package android:deploy | |
23 | + | |
24 | +undeploy : | |
25 | + mvn android:undeploy | |
26 | + | |
27 | +rebuild : | |
28 | + mvn clean package | |
29 | + | |
30 | +redeploy : | |
31 | + mvn clean package android:deploy | |
32 | + | |
33 | +#################### emulator | |
34 | + | |
35 | +start : | |
36 | + mvn android:emulator-start | |
37 | + | |
38 | +stop : | |
39 | + mvn android:emulator-stop | |
40 | + | |
41 | +#################### | |
42 | +dist : | |
43 | + mvn assembly:assembly | |
44 | + | |
45 | +clean : | |
46 | + mvn $@ |
@@ -0,0 +1,29 @@ | ||
1 | +! listview-single-choice-custom-layout | |
2 | + | |
3 | +要素のうち1つだけを選択できる ListView を作るサンプル。 | |
4 | + | |
5 | +リスト項目のレイアウトは複数の View からなるカスタムレイアウト | |
6 | +を作成して使用する。 | |
7 | + | |
8 | +CHOIDE_MODE_SINGLE を利用するにはリスト項目の View が Checkable である | |
9 | +必要がある。 このサンプルでは独自の Checkable な View として | |
10 | +NameAndEmailView を実装して用いている。これは子要素の一つに CheckedTextView を | |
11 | +置いて、Checkable インターフェースに必要なメソッドをそれに委譲する方法を | |
12 | +用いている。 | |
13 | + | |
14 | + | |
15 | +!! ビルドターゲット | |
16 | + | |
17 | +http://maven-android-plugin-m2site.googlecode.com/svn/plugin-info.html | |
18 | + | |
19 | +* mvn compile - アプリケーションのコンパイル | |
20 | +* mvn package - apk の作成 | |
21 | +* mvn clean - ビルドファイルの削除 | |
22 | +* mvn install - maven の local リポジトリにインストール | |
23 | + | |
24 | +* mvn android:deploy - エミュレータやデバイスにインストール | |
25 | +* mvn android:generate-sources | |
26 | + | |
27 | +* mvn install - アプリケーションのインストール | |
28 | +* mvn uninstall - アプリケーションのアンインストール | |
29 | + |
@@ -0,0 +1,142 @@ | ||
1 | +<?xml version="1.0" encoding="UTF-8" ?> | |
2 | +<project | |
3 | + xmlns="http://maven.apache.org/POM/4.0.0" | |
4 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
5 | + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" | |
6 | +> | |
7 | + <modelVersion>4.0.0</modelVersion> | |
8 | + <groupId>com.example.hello</groupId> | |
9 | + <artifactId>listview-single-choice-custom-layout</artifactId> | |
10 | + <packaging>apk</packaging> | |
11 | + <version>1.0.0</version> | |
12 | + <name>ListView Single Choice with Custom Layout</name> | |
13 | + <url>http://www.example.com/</url> | |
14 | + | |
15 | + <parent> | |
16 | + <groupId>com.example.hello</groupId> | |
17 | + <artifactId>android-samples</artifactId> | |
18 | + <version>1.0.0</version> | |
19 | + </parent> | |
20 | + | |
21 | + <dependencies> | |
22 | + | |
23 | + <dependency> | |
24 | + <groupId>android</groupId> | |
25 | + <artifactId>android</artifactId> | |
26 | + <version>1.5_r4</version> | |
27 | + <!-- | |
28 | + <version>1.5_r3</version> | |
29 | + <version>1.5_r4</version> | |
30 | + <version>1.6_r2</version> | |
31 | + <version>2.1.2</version> | |
32 | + <version>2.1_r1</version> | |
33 | + <version>2.2.1</version> | |
34 | + <version>2.3.1</version> | |
35 | + <version>2.3.3</version> | |
36 | + <version>4.0.1.2</version> | |
37 | + --> | |
38 | + <scope>provided</scope> | |
39 | + </dependency> | |
40 | + | |
41 | + <dependency> | |
42 | + <groupId>junit</groupId> | |
43 | + <artifactId>junit</artifactId> | |
44 | + <scope>test</scope> | |
45 | + </dependency> | |
46 | + </dependencies> | |
47 | + | |
48 | + <build> | |
49 | + <sourceDirectory>src</sourceDirectory> | |
50 | + | |
51 | + <plugins> | |
52 | + <plugin> | |
53 | + <groupId>com.jayway.maven.plugins.android.generation2</groupId> | |
54 | + <artifactId>android-maven-plugin</artifactId> | |
55 | + <configuration> | |
56 | + <sdk> | |
57 | + <!--<path>${env.ANDROID_HOME}</path>--> | |
58 | + <platform>4</platform> | |
59 | + </sdk> | |
60 | + </configuration> | |
61 | + <extensions>true</extensions> | |
62 | + </plugin> | |
63 | + | |
64 | + <!-- mvn compile --> | |
65 | + <plugin> | |
66 | + <groupId>org.apache.maven.plugins</groupId> | |
67 | + <artifactId>maven-compiler-plugin</artifactId> | |
68 | + <configuration> | |
69 | + <source>1.5</source> | |
70 | + <target>1.5</target> | |
71 | + <encoding>UTF-8</encoding> | |
72 | + </configuration> | |
73 | + </plugin> | |
74 | + | |
75 | + <!-- mvn assembly:assembly --> | |
76 | + <plugin> | |
77 | + <groupId>org.apache.maven.plugins</groupId> | |
78 | + <artifactId>maven-assembly-plugin</artifactId> | |
79 | + <configuration> | |
80 | + <descriptorRefs> | |
81 | + <!-- | |
82 | + <descriptorRef>jar-with-dependencies</descriptorRef> | |
83 | + <descriptorRef>bin</descriptorRef> | |
84 | + <descriptorRef>src</descriptorRef> | |
85 | + --> | |
86 | + <descriptorRef>project</descriptorRef> | |
87 | + </descriptorRefs> | |
88 | + </configuration> | |
89 | + </plugin> | |
90 | + | |
91 | + <!-- mvn resources:resources --> | |
92 | + <plugin> | |
93 | + <groupId>org.apache.maven.plugins</groupId> | |
94 | + <artifactId>maven-resources-plugin</artifactId> | |
95 | + <configuration> | |
96 | + <encoding>UTF-8</encoding> | |
97 | + </configuration> | |
98 | + </plugin> | |
99 | + | |
100 | + <!-- mvn site --> | |
101 | + <plugin> | |
102 | + <groupId>org.apache.maven.plugins</groupId> | |
103 | + <artifactId>maven-site-plugin</artifactId> | |
104 | + <configuration> | |
105 | + <locales>en</locales> | |
106 | + <inputEncoding>UTF-8</inputEncoding> | |
107 | + <outputEncoding>UTF-8</outputEncoding> | |
108 | + | |
109 | + <reportPlugins> | |
110 | + <plugin> | |
111 | + <groupId>org.apache.maven.plugins</groupId> | |
112 | + <artifactId>maven-project-info-reports-plugin</artifactId> | |
113 | + </plugin> | |
114 | + | |
115 | + <!-- mvn javadoc:javadoc --> | |
116 | + <plugin> | |
117 | + <groupId>org.apache.maven.plugins</groupId> | |
118 | + <artifactId>maven-javadoc-plugin</artifactId> | |
119 | + <configuration> | |
120 | + <charset>utf-8</charset> | |
121 | + </configuration> | |
122 | + </plugin> | |
123 | + </reportPlugins> | |
124 | + </configuration> | |
125 | + </plugin> | |
126 | + | |
127 | + </plugins> | |
128 | + | |
129 | + <!-- mvn resources:resources --> | |
130 | + <resources> | |
131 | + <resource> | |
132 | + <directory>res</directory> | |
133 | + <filtering>false</filtering> | |
134 | + <includes> | |
135 | + <include>**/*.properties</include> | |
136 | + </includes> | |
137 | + </resource> | |
138 | + </resources> | |
139 | + </build> | |
140 | + | |
141 | + | |
142 | +</project> |
@@ -0,0 +1,21 @@ | ||
1 | +<?xml version="1.0" encoding="utf-8"?> | |
2 | +<FrameLayout | |
3 | + xmlns:android="http://schemas.android.com/apk/res/android" | |
4 | + android:id="@+id/list_frame" | |
5 | + android:layout_width="fill_parent" | |
6 | + android:layout_height="fill_parent" | |
7 | + android:padding="4dip" | |
8 | +> | |
9 | + <ListView | |
10 | + android:id="@android:id/list" | |
11 | + android:layout_width="fill_parent" | |
12 | + android:layout_height="fill_parent" | |
13 | + /> | |
14 | + <TextView | |
15 | + android:id="@android:id/empty" | |
16 | + android:layout_width="wrap_content" | |
17 | + android:layout_height="wrap_content" | |
18 | + android:layout_gravity="center" | |
19 | + android:text="@string/no_data" | |
20 | + /> | |
21 | +</FrameLayout> | |
\ No newline at end of file |
@@ -0,0 +1,39 @@ | ||
1 | +<?xml version="1.0" encoding="utf-8"?> | |
2 | +<LinearLayout | |
3 | + xmlns:android="http://schemas.android.com/apk/res/android" | |
4 | + android:layout_width="fill_parent" | |
5 | + android:layout_height="wrap_content" | |
6 | + android:orientation="horizontal" | |
7 | + android:padding="4dip" | |
8 | +> | |
9 | + <LinearLayout | |
10 | + android:layout_width="wrap_content" | |
11 | + android:layout_height="wrap_content" | |
12 | + android:layout_weight="1" | |
13 | + android:orientation="vertical" | |
14 | + > | |
15 | + <TextView | |
16 | + android:id="@+id/name" | |
17 | + android:layout_width="fill_parent" | |
18 | + android:layout_height="wrap_content" | |
19 | + android:text="<NAME>" | |
20 | + android:textSize="22sp" | |
21 | + /> | |
22 | + <TextView | |
23 | + android:id="@+id/email" | |
24 | + android:layout_width="fill_parent" | |
25 | + android:layout_height="wrap_content" | |
26 | + android:text="<EMAIL>" | |
27 | + android:textSize="14sp" | |
28 | + /> | |
29 | + </LinearLayout> | |
30 | + | |
31 | + <CheckedTextView | |
32 | + android:id="@+id/checkable" | |
33 | + android:layout_width="wrap_content" | |
34 | + android:layout_height="wrap_content" | |
35 | + android:layout_weight="0" | |
36 | + android:layout_gravity="center" | |
37 | + android:checkMark="?android:attr/listChoiceIndicatorSingle" | |
38 | + /> | |
39 | +</LinearLayout> | |
\ No newline at end of file |
@@ -0,0 +1,7 @@ | ||
1 | +<?xml version="1.0" encoding="utf-8"?> | |
2 | +<view | |
3 | + xmlns:android="http://schemas.android.com/apk/res/android" | |
4 | + class="com.example.hello.android.listview_single_choice_custom_layout.NameAndEmailView" | |
5 | + android:layout_width="fill_parent" | |
6 | + android:layout_height="fill_parent" | |
7 | +/> | |
\ No newline at end of file |
@@ -0,0 +1,31 @@ | ||
1 | +<?xml version="1.0" encoding="utf-8"?> | |
2 | +<resources> | |
3 | + <array name="names"> | |
4 | + <item>Alice</item> | |
5 | + <item>Bob</item> | |
6 | + <item>Charlie</item> | |
7 | + <item>David</item> | |
8 | + <item>Eve</item> | |
9 | + <item>Frank</item> | |
10 | + <item>George</item> | |
11 | + <item>Harvey</item> | |
12 | + <item>Ivan</item> | |
13 | + <item>Justin</item> | |
14 | + <item>Kyle</item> | |
15 | + <item>Linda</item> | |
16 | + <item>Matilda</item> | |
17 | + <item>Natalia</item> | |
18 | + <item>Oscar</item> | |
19 | + <item>Pat</item> | |
20 | + <item>Quincy</item> | |
21 | + <item>Rachel</item> | |
22 | + <item>Steave</item> | |
23 | + <item>Trent</item> | |
24 | + <item>Urien</item> | |
25 | + <item>Victor</item> | |
26 | + <item>Walter</item> | |
27 | + <item>Xenia</item> | |
28 | + <item>Yuriya</item> | |
29 | + <item>Zoe</item> | |
30 | + </array> | |
31 | +</resources> |
@@ -0,0 +1,6 @@ | ||
1 | +<?xml version="1.0" encoding="utf-8"?> | |
2 | +<resources> | |
3 | + <string name="app_name">ListView Custom Single Choice</string> | |
4 | + <string name="user_list">User List</string> | |
5 | + <string name="no_data">NO DATA</string> | |
6 | +</resources> |
@@ -0,0 +1,114 @@ | ||
1 | +/* | |
2 | + * The MIT License | |
3 | + * | |
4 | + * Copyright 2014 Masahiko, SAWAI <masahiko.sawai@gmail.com>. | |
5 | + * | |
6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | + * of this software and associated documentation files (the "Software"), to deal | |
8 | + * in the Software without restriction, including without limitation the rights | |
9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | + * copies of the Software, and to permit persons to whom the Software is | |
11 | + * furnished to do so, subject to the following conditions: | |
12 | + * | |
13 | + * The above copyright notice and this permission notice shall be included in | |
14 | + * all copies or substantial portions of the Software. | |
15 | + * | |
16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | + * THE SOFTWARE. | |
23 | + */ | |
24 | +package com.example.hello.android.listview_single_choice_custom_layout; | |
25 | + | |
26 | +import android.app.ListActivity; | |
27 | +import android.os.Bundle; | |
28 | +import android.util.Log; | |
29 | +import android.widget.ListAdapter; | |
30 | +import android.widget.ListView; | |
31 | +import android.widget.SimpleAdapter; | |
32 | +import android.widget.Toast; | |
33 | +import java.util.ArrayList; | |
34 | +import java.util.HashMap; | |
35 | +import java.util.List; | |
36 | +import java.util.Map; | |
37 | + | |
38 | +public class MainActivity extends ListActivity | |
39 | +{ | |
40 | + | |
41 | + private static final String LOG_TAG = "XXX"; | |
42 | + private static final String MAPPING_KEY_NAME = "name"; | |
43 | + private static final String MAPPING_KEY_EMAIL = "email"; | |
44 | + private static final String[] MAPPING_FROM = | |
45 | + { | |
46 | + MAPPING_KEY_NAME, | |
47 | + MAPPING_KEY_EMAIL, | |
48 | + }; | |
49 | + private static final int[] MAPPING_TO = | |
50 | + { | |
51 | + R.id.name, | |
52 | + R.id.email, | |
53 | + }; | |
54 | + private List<Map<String, String>> userList; | |
55 | + | |
56 | + @Override | |
57 | + protected void onCreate(Bundle savedInstanceState) | |
58 | + { | |
59 | + super.onCreate(savedInstanceState); | |
60 | + setContentView(R.layout.main_activity); | |
61 | + | |
62 | + // Init ListAdapter | |
63 | + ListAdapter listAdapter = new SimpleAdapter(this, getUserList(), | |
64 | + R.layout.user_list_item, | |
65 | + MAPPING_FROM, MAPPING_TO); | |
66 | + setListAdapter(listAdapter); | |
67 | + | |
68 | + // Init ListView choice mode | |
69 | + ListView listView = getListView(); | |
70 | + listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); | |
71 | + // Init default checked item | |
72 | + listView.setItemChecked(2, true); | |
73 | + } | |
74 | + | |
75 | + @Override | |
76 | + protected void onPause() | |
77 | + { | |
78 | + int checkedItemPosition = getListView().getCheckedItemPosition(); | |
79 | + Log.d(LOG_TAG, "checkedItemPosition => " + checkedItemPosition); | |
80 | + if (checkedItemPosition >= 0 && checkedItemPosition < userList.size()) | |
81 | + { | |
82 | + List<Map<String, String>> list = getUserList(); | |
83 | + Map<String, String> userData = list.get(checkedItemPosition); | |
84 | + String name = userData.get(MAPPING_KEY_NAME); | |
85 | + String email = userData.get(MAPPING_KEY_EMAIL); | |
86 | + String message = String.format("Checked User : name => %s, email => %s", name, email); | |
87 | + if (message != null) | |
88 | + { | |
89 | + Log.d(LOG_TAG, message); | |
90 | + Toast.makeText(this, message, Toast.LENGTH_SHORT).show(); | |
91 | + } | |
92 | + } | |
93 | + | |
94 | + super.onPause(); | |
95 | + } | |
96 | + | |
97 | + private List<Map<String, String>> getUserList() | |
98 | + { | |
99 | + if (userList == null) | |
100 | + { | |
101 | + userList = new ArrayList<Map<String, String>>(); | |
102 | + String[] userNames = getResources().getStringArray(R.array.names); | |
103 | + for (String name : userNames) | |
104 | + { | |
105 | + String email = name.toLowerCase() + "@example.com"; | |
106 | + Map<String, String> userData = new HashMap<String, String>(); | |
107 | + userData.put(MAPPING_KEY_NAME, name); | |
108 | + userData.put(MAPPING_KEY_EMAIL, email); | |
109 | + userList.add(userData); | |
110 | + } | |
111 | + } | |
112 | + return userList; | |
113 | + } | |
114 | +} |
@@ -0,0 +1,81 @@ | ||
1 | +/* | |
2 | + * The MIT License | |
3 | + * | |
4 | + * Copyright 2014 Masahiko, SAWAI <masahiko.sawai@gmail.com>. | |
5 | + * | |
6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | + * of this software and associated documentation files (the "Software"), to deal | |
8 | + * in the Software without restriction, including without limitation the rights | |
9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | + * copies of the Software, and to permit persons to whom the Software is | |
11 | + * furnished to do so, subject to the following conditions: | |
12 | + * | |
13 | + * The above copyright notice and this permission notice shall be included in | |
14 | + * all copies or substantial portions of the Software. | |
15 | + * | |
16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | + * THE SOFTWARE. | |
23 | + */ | |
24 | +package com.example.hello.android.listview_single_choice_custom_layout; | |
25 | + | |
26 | +import android.content.Context; | |
27 | +import android.util.AttributeSet; | |
28 | +import android.view.LayoutInflater; | |
29 | +import android.view.View; | |
30 | +import android.widget.Checkable; | |
31 | +import android.widget.FrameLayout; | |
32 | + | |
33 | +public class NameAndEmailView extends FrameLayout implements Checkable | |
34 | +{ | |
35 | + | |
36 | + private Checkable checkableView; | |
37 | + | |
38 | + public NameAndEmailView(Context context, AttributeSet attrs, int defStyle) | |
39 | + { | |
40 | + super(context, attrs, defStyle); | |
41 | + initChildViews(); | |
42 | + } | |
43 | + | |
44 | + public NameAndEmailView(Context context, AttributeSet attrs) | |
45 | + { | |
46 | + super(context, attrs); | |
47 | + initChildViews(); | |
48 | + } | |
49 | + | |
50 | + public NameAndEmailView(Context context) | |
51 | + { | |
52 | + super(context); | |
53 | + initChildViews(); | |
54 | + } | |
55 | + | |
56 | + private void initChildViews() | |
57 | + { | |
58 | + LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); | |
59 | + View view = inflater.inflate(R.layout.name_and_email, null, false); | |
60 | + checkableView = (Checkable) view.findViewById(R.id.checkable); | |
61 | + addView(view); | |
62 | + } | |
63 | + | |
64 | + @Override | |
65 | + public void toggle() | |
66 | + { | |
67 | + checkableView.toggle(); | |
68 | + } | |
69 | + | |
70 | + @Override | |
71 | + public void setChecked(boolean checked) | |
72 | + { | |
73 | + checkableView.setChecked(checked); | |
74 | + } | |
75 | + | |
76 | + @Override | |
77 | + public boolean isChecked() | |
78 | + { | |
79 | + return checkableView.isChecked(); | |
80 | + } | |
81 | +} |
@@ -90,6 +90,7 @@ | ||
90 | 90 | <module>listview-simple-adapter-with-viewbinder</module> |
91 | 91 | <module>listview-simple-cursor-adapter</module> |
92 | 92 | <module>listview-single-choice</module> |
93 | + <module>listview-single-choice-custom-layout</module> | |
93 | 94 | |
94 | 95 | <module>markdown-viewer</module> |
95 | 96 |