Slide 1

Slide 1 text

"OESPJEɾJ04 ΞϓϦ࡞੒ೖ໳ ,.$OPOZMFOF

Slide 2

Slide 2 text

͜ͷεϥΠυʹ͍ͭͯ w य़߹॓ͷ຅εϥΠυ w य़߹॓Ͱ͸J04ฤͷΈΛൃදʢͪ͜Βʣ w ͜͜ʹ͸"OESPJEฤΛऩ࿥ w J04ฤͱҰ෦ॏෳ͍ͯ͠·͢

Slide 3

Slide 3 text

ࣗݾ঺հ w OPOZMFOF ͷʹΕΜ w ,.$ೋճੜSPPU w (JU)VCIBUFOBFUDʜ w QZUIPO KBWB "OESPJE 
 ,PUMJOFUDʜ

Slide 4

Slide 4 text

࿩͢͜ͱ

Slide 5

Slide 5 text

"OESPJEͱJ04ͷجຊ

Slide 6

Slide 6 text

"OESPJEͱJ04
 Ͱ ಉػೳͷΞϓϦΛ࡞੒

Slide 7

Slide 7 text

࡞੒͢ΔΞϓϦ w 4UBDL0WFSqPX"1* w ΈΜͳେ޷͖ɺϓϩάϥϛϯάͷ2"αΠτ w QVCMJD"1*Ͱݕࡧ΍ৄࡉ͕औΕΔ

Slide 8

Slide 8 text

4UBDL0WFSqPX"1* w υΩϡϝϯτ w IUUQTBQJTUBDLFYDIBOHFDPNEPDT w TFBSDI"1* ྫλΠτϧʹBOESPJE w IUUQTBQJTUBDLFYDIBOHFDPNTFBSDI PSEFSEFTDTPSUBDUJWJUZJOUJUMFBOESPJET JUFTUBDLPWFSqPXpMUFSGQO[U%1

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

4UBDLFS

Slide 11

Slide 11 text

"OESPJE

Slide 12

Slide 12 text

໨࣍ w "OESPJEͱ͸ʁ w ηοτΞοϓ w )FMMP8PSMEΛோΊΔ w ΞϓϦ࡞੒

Slide 13

Slide 13 text

"OESPJEͱ͸ w (PPHMFͷ04 w -JOVYΧʔωϧΛ࢖༻ w ༷ʑͳϝʔΧʔ w ೔ຊʹ͓͚ΔγΣΞ͸ w ࠷৽͸ NBSTINBMMPX

Slide 14

Slide 14 text

։ൃ؀ڥ w +BWB w ਖ਼֬ʹ͸"OESPJE+BWB
 ͱݴΘΕɺڍಈ͕ҧͬͨΓ͢Δ w ਖ਼نදݱFUD w "OESPJE/͔Β͸0QFO+%,KBWB

Slide 15

Slide 15 text

։ൃ؀ڥ w "OESPJE4UVEJP w *OUFMMJ+ϕʔε w 8JOEPXTNBD-JOVY w ແྉ

Slide 16

Slide 16 text

ηοτΞοϓ

Slide 17

Slide 17 text

໊শઃఆ w ࠓճ͸4UBDLFS w OFUOPOZMFOFTUBDLFS w ͜Ε͕ΞϓϦͷ*%ʹ

Slide 18

Slide 18 text

04όʔδϣϯ w ݹ͍όʔδϣϯ͕ଟ͘ଘࡏ w ࠷৽ͷ"1*͕࢖͑ͳ͍౳ w Ҏ্ఔ౓͕Φεεϝ

Slide 19

Slide 19 text

"DUJWJUZ࡞੒ w &NQUZ"DUJWJUZΛબ୒
 ʢ#BTJD͸ͪΐͬͱ"EWBODFE͗͢Δʣ

Slide 20

Slide 20 text

)FMMP8PSME w ୺຤Λ઀ଓɺ্ʹ͋Δl⾣z Λԡͯ͠ΞϓϦΛىಈ w ΤϛϡϨʔλͰ΋Մ

Slide 21

Slide 21 text

)FMMP8PSMEΛோΊΔ

Slide 22

Slide 22 text

.BJO"DUJWJUZKBWB public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } } ˣޙͰઆ໌ ˢϨΠΞ΢τΛදࣔ w ը໘੍ޚ

Slide 23

Slide 23 text

BDUJWJUZ@NBJOYNMʢൈਮʣ w ϨΠΞ΢τ

Slide 24

Slide 24 text

CVJMEHSBEF ൈਮ android { defaultConfig { applicationId "net.nonylene.stacker" minSdkVersion 19 targetSdkVersion 23 versionCode 1 versionName "1.0" } } dependencies { compile 'com.android.support:appcompat-v7:23.2.0' } ˡόʔδϣϯ৘ใͳͲ ˢ֎෦ϥΠϒϥϦ 4VQQPSU-JCSBSZ w Ϗϧυઃఆ౳ HSPPWZ

Slide 25

Slide 25 text

"OESPJE.BOJGFTUYNMʢൈਮʣ w ΞϓϦͷ৘ใ͕ॻ͔ΕͨϑΝΠϧ ˡΞϓϦ໊ɺΞΠίϯ౳ ˢ"DUJWJUZͷ৘ใ

Slide 26

Slide 26 text

ΞϓϦ࡞੒

Slide 27

Slide 27 text

ϑϩʔΛߟ͑Δ ᶃݕࡧϫʔυΛೖྗͤ͞Δ ᶅ"1*Λୟ͍ͯ݁ՌΛऔಘ ᶅ+40/Λॲཧ ᶆॲཧͨ݁͠ՌΛදࣔ͢Δ

Slide 28

Slide 28 text

ᶃݕࡧϫʔυΛೖྗ

Slide 29

Slide 29 text

ϨΠΞ΢τ࡞੒

Slide 30

Slide 30 text

ը໘Λ૊ΈཱͯΔ w ը໘ύʔπͷ͜ͱΛViewͱ͍͏ w "OESPJEͰ͸ը໘ύʔπ͸YNMͰද͞ΕΔ w ೖྗཝͱϦετදࣔͷύʔπΛ্Լʹฒ΂Δ

Slide 31

Slide 31 text

BDUJWJUZNBJOYNMʢൈਮʣ ɾ-JOFBS-BZPVU &EJU5FYU #VUUPO 3FDZDMFS7JFX

Slide 32

Slide 32 text

-JOFBS-BZPVU w ෳ਺ͷViewΛ্ԼPSࠨӈʹฒ΂Δ໾ׂΛ࣋ͭ • layout_width=“match_parent”
 layout_height=“match_parent”
 
 ɹɹॎԣ͍ͬͺ͍ʹ޿͛Δ • orientation="vertical"
 
 ɹɹॎ޲͖ʹฒ΂Δ

Slide 33

Slide 33 text

&EJU5FYU w จࣈೖྗ༻ͷView • id="@+id/edit_text"
 
 ɹɹϓϩάϥϜ͔Βࢀর͢ΔͨΊͷ*% • hint=“Please input search"
 
 ɹɹ༧Ίදࣔ͢Δώϯτ • lines="1"
 
 ɹɹҰߦදࣔ

Slide 34

Slide 34 text

&EJU5FYU • layout_width=“match_parent”
 layout_height=“wrap_content”
 
 ɹɹԣ෯͸͍ͬͺ͍ʹ޿͛Δ
 ɹɹॎ෯͸σϑΥϧτͷߴ͞ʹ͢Δ • ࣗ෼Ͱߴ͞Λઃఆ͢Δ৔߹͸ dp Λ࢖͏ • 1dp ͸୺຤ʹΑͬͯ 2px ͩͬͨΓ 3px ͩͬͨΓ

Slide 35

Slide 35 text

#VUUPO w Ϙλϯͷ7JFX • text=“Search!"
 
 ɹɹϘλϯʹදࣔ͢ΔςΩετ

Slide 36

Slide 36 text

w 4VQQPSU-JCSBSZʹೖ͍ͬͯΔ w 4VQQPSU-JCSBSZͱ͸ɾɾɾʁ 3FDZDMFS7JFX

Slide 37

Slide 37 text

w ৽͍͠04ͷػೳͷҰ෦Λݹ͍04Ͱ΋࢖͑ΔΑ͏ʹ ͢ΔɺHPPHMFͷެࣜϥΠϒϥϦ w ྫ.BUFSJBM%FTJHO 'SBHNFOU FUD w ઌ΄ͲͷAppCompatActivity΋ͦͷҰͭ w ࠷৽ͷσβΠϯͷҰ෦΍ػೳ͕ఏڙ͞ΕΔ 4VQQPSU-JCSBSZ

Slide 38

Slide 38 text

w ଞʹ΋04ʹ͸ೖͬͯͳ͍ศརػೳΛఏڙ w ྫ3FDZDMFS7JFX 1BMFUUF 4VQQPSU-JCSBSZ

Slide 39

Slide 39 text

w ಉ͡ViewΛ܁Γฦ͠Ϧετදࣔ͢ΔView w ৄ͘͠͸ޙͰઆ໌ 3FDZDMFS7JFX

Slide 40

Slide 40 text

w ϥΠϒϥϦͱͯ͠഑෍͞Ε͍ͯΔͷͰಋೖ w CVJMEHSBEMF 3FDZDMFS7JFX dependencies { compile ‘com.android.support:appcompat-v7:23.2.0’ compile ‘com.android.support:recyclerview-v7:23.2.0' }

Slide 41

Slide 41 text

3FDZDMFS7JFX w BDUJWJUZ@NBJOYNM

Slide 42

Slide 42 text

࣮ػͰ֬ೝ w Ϧετ෦෼͸ۭന w ԿΛදࣔͤ͞Δ͔Λ·ܾͩ Ί͍ͯͳ͍

Slide 43

Slide 43 text

ΫϦοΫˠจࣈྻऔಘ

Slide 44

Slide 44 text

ϑϩʔ w Ϣʔβʔ͕ϘλϯΛԡ͢
 ˠEditText͔ΒݕࡧΩʔϫʔυΛऔಘ w ͦΕʹԠͯ͡(&53FRVFTUΛඈ͹͢ ᶄ

Slide 45

Slide 45 text

7JFXΛ"DUJWJUZ಺Ͱೝࣝ • View View#findViewById(int id) • YNMͰࢦఆͨ͠*%ͷ View Λऔಘ͢Δ • ྫ: id=“@+id/view_id” ͱࢦఆ
 → View view =
 findViewById(R.layout.view_id); • R.layout.~~ ͸YNMͰ*%Λࢦఆ͢Δͱ
 ࣗಈతʹ࡞ΒΕΔɺ*%ͷू߹Ϋϥε

Slide 46

Slide 46 text

Ωʔϫʔυऔಘ • ಘΒΕͨ View Λ EditText ʹΩϟετ
 → EditText ͱͯ͠ϓϩάϥϜͰѻ͑Δ public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); EditText editText = (EditText) findViewById(R.id.edit_text); Button button = (Button) findViewById(R.id.button); } }

Slide 47

Slide 47 text

ϘλϯͷΫϦοΫ • View View#setOnClickListener
 (OnClickListener listener) • Click ͞Εͨ࣌ͷϦεφʔΛઃఆ • ΫϦοΫ͞ΕΔ
 → Ϧεφʔʹ͋Δࢦఆͷؔ਺͕ݺ͹ΕΔ • java7 ʹϥϜμࣜ͸ແ͍
 → ແ໊ΦϒδΣΫτΛ࡞੒͢Δ

Slide 48

Slide 48 text

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final EditText editText = (EditText)
 findViewById(R.id.edit_text); Button button = (Button) findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String searchText = 
 editText.getText().toString(); } }); } • ΫϦοΫ͞ΕͨΒ OnClick ͕ݺ͹Εɺ
 ɹEditText ͔ΒΩʔϫʔυऔಘ͢Δ

Slide 49

Slide 49 text

ᶄ"1*Ͱ+40/औಘ

Slide 50

Slide 50 text

ωοτϫʔΫ w ωοτϫʔΫΛඈ͹͢ʹ͸ݖݶ͕͍Δ w ΞϓϦͷݖݶ͸Πϯετʔϧ࣌ʹ֬ೝ͞ΕΔ w "OESPJE͔Β͸Ұ෦ͷݖݶ͕ڐՄ੍ʹ w "OESPJE.BOJGFTUYNMʹهड़

Slide 51

Slide 51 text

ωοτϫʔΫݖݶͷ௥Ճ w "OESPJE.BOJGFTUYNM

Slide 52

Slide 52 text

ωοτϫʔΫ w "OESPJEͷσϑΥϧτͰ͸%FGBVMU)UUQ$MJFOU ͱ"TZOD5BTL -PBEFS Λ࢖͏ w গ͠೉͍͠ͷͰࠓճ͸0L)UUQΛ࢖͏ w 4RVBSFࣾͷKBWB޲͚௨৴ϥΠϒϥϦ w "OESPJE಺෦Ͱ΋࢖ΘΕ͍ͯΔ

Slide 53

Slide 53 text

0L)UUQ w build.gradleʹ௥Ճ dependencies { compile 'com.android.support:appcompat-v7:23.2.0' compile ‘com.android.support:recyclerview-v7:23.2.0' compile 'com.squareup.okhttp3:okhttp:3.2.0' }

Slide 54

Slide 54 text

ϦΫΤετΛ࡞੒ String encodedText = null; try { encodedText = URLEncoder.encode(text, "utf-8"); } catch (UnsupportedEncodingException ignore) {} String url = "https://api.stackexchange.com/2.2/search? order=desc&sort=activity&intitle=" + encodedText + "&site=stackoverflow&filter=!-*f(6pnztD5P"; Request request = new Request.Builder() .url(url) .build(); w ϦΫΤετ͢Δ63-Λγϡοͱ࡞੒ ˡ0L)UUQʹ౉͢ϦΫΤετͷ৘ใ

Slide 55

Slide 55 text

ϦΫΤετΛ౤͛Δ w "OESPJEͰ͸6*ͷߋ৽ʹ࢖͏εϨου͸Ұͭ w ͜ͷεϨουҎ֎Ͱ6*ؔ܎Λ࿔ΔͱΤϥʔ w ωοτϫʔΫͷϦΫΤετ͸ɺ6*εϨουͰߦͬ ͯ͸͍͚ͳ͍ w ϦΫΤετʹ͕͔͔࣌ؒΔͱը໘͕ݻ·ΔͨΊ

Slide 56

Slide 56 text

0L)UUQΛ࢖ͬͨϦΫΤετ

Slide 57

Slide 57 text

new OkHttpClient().newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { e.printStackTrace(); } @Override public void onResponse(Call call, final Response response)
 throws IOException { final String body = response.body().string(); runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this, body, 
 Toast.LENGTH_LONG).show(); } }); } }); • enqueue ʹ Callback Λ౉͢ͱ
 ผεϨουͰ Request ͞ΕΔ

Slide 58

Slide 58 text

new OkHttpClient().newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { e.printStackTrace(); } @Override public void onResponse(Call call, final Response response)
 throws IOException { final String body = response.body().string(); runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this, body, 
 Toast.LENGTH_LONG).show(); } }); } }); • runOnUiThread() ʹ Runnable Λ౉͢ͱ
 6*εϨουͰ run() ͕࣮ߦ͞ΕΔ

Slide 59

Slide 59 text

new OkHttpClient().newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { e.printStackTrace(); } @Override public void onResponse(Call call, final Response response)
 throws IOException { final String body = response.body().string(); runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this, body, 
 Toast.LENGTH_LONG).show(); } }); } }); • ड͚औͬͨ body Λͦͷ··τʔετʹग़͢

Slide 60

Slide 60 text

࣮ػͰ֬ೝ

Slide 61

Slide 61 text

ᶅ+40/Λॲཧ

Slide 62

Slide 62 text

+40/Λύʔε • JsonObject(String json) • String Λύʔε͠ Json ͱͯ͠ѻ͏ • ྫ: String JsonObject#getString
 (String name) • json ͷ name ͱ͍͏߲໨ͷจࣈྻΛऔಘ

Slide 63

Slide 63 text

• ฦ͖ͬͯͨ Response Λ 
 JsonObject ʹม׵ɺͦͷதͷ “items” Λऔಘ @Override public void onResponse(Call call, final Response response) throws … { final String body = response.body().string(); try { JSONObject json = new JSONObject(body); JSONArray questionsJson = json.getJSONArray("items"); } catch (JSONException ignore) {} }

Slide 64

Slide 64 text

σʔλอ࣋Ϋϥε

Slide 65

Slide 65 text

σʔλอ࣋Ϋϥε w ฦ͖ͬͯͨ+40/ΛಡΈࠐΈɺΫϥεʹ֨ೲ͢Δ w σʔλΛѻ͍΍͘͢͢ΔͨΊ

Slide 66

Slide 66 text

σʔλอ࣋Ϋϥε • Question ΫϥεΛ࡞Δ • Question.java Λ࡞੒

Slide 67

Slide 67 text

public class Question { public final boolean isAnswered; public final String ownerName; public final int upVoteCount; public final Date creationDate; public final String title; public Question(JSONObject json) throws ɹɹɹɹɹɹɹɹɹɹɹɹɹɹɹɹɹɹJSONException { isAnswered = ɹɹɹ ɹɹɹɹɹɹɹjson.getBoolean(“is_answered"); upVoteCount = 
 ɹɹɹɹɹɹɹjson.getInt(“up_vote_count"); creationDate = new Date(
 ɹɹɹɹɹɹɹjson.getInt("creation_date")
 ɹɹɹɹɹɹɹ * 1000); title = json.getString(“title"); JSONObject owner = 
 ɹɹɹɹɹɹɹjson.getJSONObject("owner"); ownerName = 
 ɹɹɹɹɹɹɹowner.getString("display_name"); } }

Slide 68

Slide 68 text

public class Question { public final boolean isAnswered; public final String ownerName; public final int upVoteCount; public final Date creationDate; public final String title; public Question(JSONObject json) throws ɹɹɹɹɹɹɹɹɹɹɹɹɹɹɹɹɹɹJSONException { isAnswered = ɹɹɹ ɹɹɹɹɹɹɹjson.getBoolean(“is_answered"); upVoteCount = 
 ɹɹɹɹɹɹɹjson.getInt(“up_vote_count"); creationDate = new Date(
 ɹɹɹɹɹɹɹjson.getInt("creation_date")
 ɹɹɹɹɹɹɹ * 1000); title = json.getString(“title"); JSONObject owner = 
 ɹɹɹɹɹɹɹjson.getJSONObject("owner"); ownerName = 
 ɹɹɹɹɹɹɹowner.getString("display_name"); } }

Slide 69

Slide 69 text

public class Question { public final boolean isAnswered; public final String ownerName; public final int upVoteCount; public final Date creationDate; public final String title; public Question(JSONObject json) throws ɹɹɹɹɹɹɹɹɹɹɹɹɹɹɹɹɹɹJSONException { isAnswered = ɹɹɹ ɹɹɹɹɹɹɹjson.getBoolean(“is_answered"); upVoteCount = 
 ɹɹɹɹɹɹɹjson.getInt(“up_vote_count"); creationDate = new Date(
 ɹɹɹɹɹɹɹjson.getInt("creation_date")
 ɹɹɹɹɹɹɹ * 1000); title = json.getString(“title"); JSONObject owner = 
 ɹɹɹɹɹɹɹjson.getJSONObject("owner"); ownerName = 
 ɹɹɹɹɹɹɹowner.getString("display_name"); } }

Slide 70

Slide 70 text

public class Question { public final boolean isAnswered; public final String ownerName; public final int upVoteCount; public final Date creationDate; public final String title; public Question(JSONObject json) throws ɹɹɹɹɹɹɹɹɹɹɹɹɹɹɹɹɹɹJSONException { isAnswered = ɹɹɹ ɹɹɹɹɹɹɹjson.getBoolean(“is_answered"); upVoteCount = 
 ɹɹɹɹɹɹɹjson.getInt(“up_vote_count"); creationDate = new Date(
 ɹɹɹɹɹɹɹjson.getInt("creation_date")
 ɹɹɹɹɹɹɹ * 1000); title = json.getString(“title"); JSONObject owner = 
 ɹɹɹɹɹɹɹjson.getJSONObject("owner"); ownerName = 
 ɹɹɹɹɹɹɹowner.getString("display_name"); } }

Slide 71

Slide 71 text

KTPOΛ2VFTUJPOʹม׵

Slide 72

Slide 72 text

• json ͷ഑ྻͷཁૉ͢΂ͯΛ Question ʹม׵ @Override public void onResponse(Call call, final Response response) throws … { final String body = response.body().string(); try { JSONObject json = new JSONObject(body); JSONArray questionsJson = json.getJSONArray("items"); final List questions = new ArrayList<>(); for (int i = 0; i < questionsJson.length(); i++) { questions.add(new Question(questionsJson.getJSONObject(i))); } runOnUiThread(new Runnable() { … Toast.makeText(MainActivity.this, questions.get(0).title, 
 Toast.LENGTH_LONG).show(); }); } catch (JSONException ignore) {} }

Slide 73

Slide 73 text

• Ұ൪໨ͷ Question ͷλΠτϧΛτʔετදࣔ @Override public void onResponse(Call call, final Response response) throws … { final String body = response.body().string(); try { JSONObject json = new JSONObject(body); JSONArray questionsJson = json.getJSONArray("items"); final List questions = new ArrayList<>(); for (int i = 0; i < questionsJson.length(); i++) { questions.add(new Question(questionsJson.getJSONObject(i))); } runOnUiThread(new Runnable() { … Toast.makeText(MainActivity.this, questions.get(0).title, 
 Toast.LENGTH_LONG).show(); }); } catch (JSONException ignore) {} }

Slide 74

Slide 74 text

࣮ػͰ֬ೝ

Slide 75

Slide 75 text

ᶆॲཧͨ݁͠ՌΛදࣔ

Slide 76

Slide 76 text

3FDZDMFS7JFX ಉ͡7JFXΛ܁Γฦ͠Ϧετදࣔ͢Δ7JFX

Slide 77

Slide 77 text

3FDZDMFS7JFX ී௨ʹ্͔ΒViewΛஔ͚͹͍͍͡ΌΜʁ

Slide 78

Slide 78 text

w ⚠ཁૉ͕ݸҎ্ʹͳΔ͜ͱ΋ w ྫ5XJUUFS w ͜ͷ࣌ݸಉ࣌ʹViewΛอ࣋ɾ؅ཧ͢Δʁ
 ˠ๲େͳϝϞϦɾॲཧΛফඅͯ͠͠·͏ 3FDZDMFS7JFX

Slide 79

Slide 79 text

w Ϧετදࣔ͸ಉ͡ߏ଄ͷView͕࿈ͳΔ w จࣈྻ౳ͷ಺༰͕ҧ͏͚ͩ ˠɹอ࣋ɾ؅ཧ͸ը໘ʹදࣔ͞ΕΔViewͷΈ ˠɹҰ౓࡞ͬͨ7JFX͸࠶ར༻͠ɺ
 දࣔ࣌ʹ಺༰Λมߋ͢Δ 3FDZDMFS7JFX

Slide 80

Slide 80 text

w ͢΂ͯΛอ࣋ɾ؅ཧ ී௨ͷ7JFX View 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ࣮ࡍͷදࣔྖҬ →

Slide 81

Slide 81 text

w อ࣋ɾ؅ཧ͸ը໘্ͷΈ 3FDZDMFS7JFX View 3 4 5 6 7 8 9 10 11 ະੜ੒ ࣮ࡍͷදࣔྖҬ →

Slide 82

Slide 82 text

εΫϩʔϧ͞ΕΔͱʜ View 3 4 5 6 7 8 9 10 11 ະੜ੒ View ͷ࠶ར༻ දࣔྖҬ →

Slide 83

Slide 83 text

εΫϩʔϧ͞ΕΔͱʜ View 5 6 7 8 9 10 11 3 ->12 4 ->13 ະੜ੒ ← ৽͍͠஋Λઃఆ දࣔྖҬ →

Slide 84

Slide 84 text

3FDZDMFS7JFX ࠶ར༻

Slide 85

Slide 85 text

w ListView͕ݩʑ͋ͬͨʢ͜Ε΋࠶ར༻Մೳʣ w ͦͷڧԽɾΧελϚΠζՄೳ൛ 3FDZDMFS7JFX

Slide 86

Slide 86 text

Ϧετදࣔ ߲໨ͷViewΛ࡞੒ ˣ RecyclerViewʹදࣔ

Slide 87

Slide 87 text

߲໨ͷ7JFXʢলུʣ

Slide 88

Slide 88 text

w ཁૉΛදࣔ͢Δͱ͖ʹViewHolder͕߱ͬͯ͘Δ w ੜ੒PS࠶ར༻͞ΕͨViewΛؚΉΫϥε w 7JFX)PMEFSࣗମ΋࠶ར༻͞Εͨ΋ͷ w ͜ΕΛ༻͍ͯ಺༰Λઃఆ͢Δ 3FDZDMFS7JFXʹදࣔ

Slide 89

Slide 89 text

w ViewͷछྨผʹͦΕͧΕViewHolderΛ࡞Δ w ࠓճ͸Ұ͚ͭͩ 7JFX)PMEFS࡞੒

Slide 90

Slide 90 text

w ࠷ॳʹView͕ੜ੒͞ΕΔͱ͖ʹݺ͹ΕΔ w findViewById΋ੜ੒࣌ʹҰ౓͚ͩߦ͏ private class QuestionViewHolder extends RecyclerView.ViewHolder { private final TextView mCountView; private final TextView mTitleView; … public QuestionViewHolder(View itemView) { super(itemView); mCountView = (TextView) itemView.findViewById(R.id.count); mTitleView = (TextView) itemView.findViewById(R.id.title); … } public void bindView(Question question) { mCountView.setText(String.valueOf(question.upVoteCount)); mTitleView.setText(question.title); … itemView.setOnClickListener(new View.OnClickListener(){ … Intent intent = new Intent(Intent.ACTION_VIEW,
 Uri.parse(question.link)); v.getContext().startActivity(intent); }); } } }

Slide 91

Slide 91 text

private class QuestionViewHolder extends RecyclerView.ViewHolder { private final TextView mCountView; private final TextView mTitleView; … public QuestionViewHolder(View itemView) { super(itemView); mCountView = (TextView) itemView.findViewById(R.id.count); mTitleView = (TextView) itemView.findViewById(R.id.title); … } public void bindView(Question question) { mCountView.setText(String.valueOf(question.upVoteCount)); mTitleView.setText(question.title); … itemView.setOnClickListener(new View.OnClickListener(){ … Intent intent = new Intent(Intent.ACTION_VIEW,
 Uri.parse(question.link)); v.getContext().startActivity(intent); }); } } } w View಺෦ͷ஋Λઃఆ

Slide 92

Slide 92 text

private class QuestionViewHolder extends RecyclerView.ViewHolder { private final TextView mCountView; private final TextView mTitleView; … public QuestionViewHolder(View itemView) { super(itemView); mCountView = (TextView) itemView.findViewById(R.id.count); mTitleView = (TextView) itemView.findViewById(R.id.title); … } public void bindView(Question question) { mCountView.setText(String.valueOf(question.upVoteCount)); mTitleView.setText(question.title); … itemView.setOnClickListener(new View.OnClickListener(){ … Intent intent = new Intent(Intent.ACTION_VIEW,
 Uri.parse(question.link)); v.getContext().startActivity(intent); }); } } } w ΫϦοΫͨ͠Β࣭໰ϖʔδΛ։͘Α͏ʹ͢Δ
 ҉໧తIntentΛ౤͛Δ

Slide 93

Slide 93 text

w RecyclerViewΛૢ࡞͢ΔΫϥε 3FDZDMFS"EBQUFS࡞੒

Slide 94

Slide 94 text

w දࣔ͢ΔQuestionͷϦετ public class QuestionsRecyclerAdapter extends
 RecyclerView.Adapter { private List mQuestionList = new ArrayList<>(); @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View questionView = LayoutInflater.from(parent.getContext())
 .inflate(R.layout.question_item, parent, false); return new QuestionViewHolder(questionView); } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { ((QuestionViewHolder) holder).bindView(mQuestionList.get(position)); } public void setQuestionList(List questionList) { mQuestionList = questionList; notifyDataSetChanged(); } @Override public int getItemCount() { return mQuestionList.size(); } }

Slide 95

Slide 95 text

w ͜͜ͰViewHolderΛ࡞੒͢Δ w ϨΠΞ΢τΛੜ੒ˠViewHolderΛ࡞੒ public class QuestionsRecyclerAdapter extends
 RecyclerView.Adapter { private List mQuestionList = new ArrayList<>(); @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View questionView = LayoutInflater.from(parent.getContext())
 .inflate(R.layout.question_item, parent, false); return new QuestionViewHolder(questionView); } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { ((QuestionViewHolder) holder).bindView(mQuestionList.get(position)); } public void setQuestionList(List questionList) { mQuestionList = questionList; notifyDataSetChanged(); } @Override public int getItemCount() { return mQuestionList.size(); } }

Slide 96

Slide 96 text

w position൪໨ͷViewͷ಺༰Λઃఆ͢Δ w ͜͜ͰViewHolderͷbindViewΛݺΜͰ͍Δ public class QuestionsRecyclerAdapter extends
 RecyclerView.Adapter { private List mQuestionList = new ArrayList<>(); @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View questionView = LayoutInflater.from(parent.getContext())
 .inflate(R.layout.question_item, parent, false); return new QuestionViewHolder(questionView); } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { ((QuestionViewHolder) holder).bindView(mQuestionList.get(position)); } public void setQuestionList(List questionList) { mQuestionList = questionList; notifyDataSetChanged(); } @Override public int getItemCount() { return mQuestionList.size(); } }

Slide 97

Slide 97 text

w notifyDataSetChanged() Λߦ͏͜ͱͰ
 RecyclerViewʹมߋΛ௨஌͢Δ public class QuestionsRecyclerAdapter extends
 RecyclerView.Adapter { private List mQuestionList = new ArrayList<>(); @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View questionView = LayoutInflater.from(parent.getContext())
 .inflate(R.layout.question_item, parent, false); return new QuestionViewHolder(questionView); } @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { ((QuestionViewHolder) holder).bindView(mQuestionList.get(position)); } public void setQuestionList(List questionList) { mQuestionList = questionList; notifyDataSetChanged(); } @Override public int getItemCount() { return mQuestionList.size(); } }

Slide 98

Slide 98 text

w RecyclerViewʹRecyclerAdapterΛηοτ 3FDZDMFS7JFX΁ઃఆ public class MainActivity extends AppCompatActivity { private QuestionsRecyclerAdapter mQuestionsRecyclerAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); … RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view); recyclerView.setLayoutManager(new LinearLayoutManager(this)); mQuestionsRecyclerAdapter = new QuestionsRecyclerAdapter(); recyclerView.setAdapter(mQuestionsRecyclerAdapter); } }

Slide 99

Slide 99 text

w RecyclerAdapterʹQuestionΛηοτ 2VFTUJPOΛઃఆ @Override public void onResponse(Call call, final Response response) throws IOException { final String body = response.body().string(); try { JSONObject json = new JSONObject(body); ɹɹɹɹɹJSONArray questionsJson = json.getJSONArray("items"); final List questions = new ArrayList<>(); ɹɹɹɹɹfor (int i = 0; i < questionsJson.length(); i++) { questions.add(new Question(questionsJson.getJSONObject(i))); } runOnUiThread(new Runnable() … mQuestionsRecyclerAdapter.setQuestionList(questions); ); } catch (JSONException ignore) {} }

Slide 100

Slide 100 text

࣮ػͰ֬ೝ

Slide 101

Slide 101 text

J04ฤ͸ͪ͜Β