Skip to content

Commit 1d3ab5a

Browse files
committed
詳細画面に遷移してWebViewで閲覧する
1 parent 779b7e1 commit 1d3ab5a

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
package="com.example.kotlin_qiita_client">
44

55
<uses-permission android:name="android.permission.INTERNET" />
6+
67
<application
78
android:allowBackup="true"
89
android:icon="@mipmap/ic_launcher"
910
android:label="@string/app_name"
1011
android:roundIcon="@mipmap/ic_launcher_round"
1112
android:supportsRtl="true"
1213
android:theme="@style/AppTheme">
14+
<activity android:name=".DetailActivity"></activity>
1315
<activity android:name=".MainActivity">
1416
<intent-filter>
1517
<action android:name="android.intent.action.MAIN" />
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.example.kotlin_qiita_client
2+
3+
import androidx.appcompat.app.AppCompatActivity
4+
import android.os.Bundle
5+
import android.webkit.WebView
6+
import android.webkit.WebViewClient
7+
8+
class DetailActivity : AppCompatActivity() {
9+
10+
override fun onCreate(savedInstanceState: Bundle?) {
11+
super.onCreate(savedInstanceState)
12+
setContentView(R.layout.activity_detail)
13+
val webView = findViewById<WebView>(R.id.web_view)
14+
val url = intent.getStringExtra("url")
15+
16+
// リンクをタップしたときに標準ブラウザを起動させない
17+
webView.webViewClient = WebViewClient()
18+
19+
// 最初に投稿を表示
20+
webView.loadUrl(url)
21+
22+
// jacascriptを許可する
23+
webView.settings.javaScriptEnabled = true
24+
}
25+
}

app/src/main/java/com/example/kotlin_qiita_client/MainActivity.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.example.kotlin_qiita_client
22

33
import android.content.Context
4+
import android.content.Intent
45
import androidx.appcompat.app.AppCompatActivity
56
import android.os.Bundle
67
import android.text.TextUtils
@@ -29,6 +30,7 @@ class MainActivity : AppCompatActivity() {
2930

3031
val listView = findViewById<ListView>(R.id.list_view) as ListView
3132
listView.adapter = mAdapter
33+
listView.onItemClickListener = OnItemClickListener()
3234

3335
val editText = findViewById<EditText>(R.id.edit_text) as EditText
3436
editText.setOnKeyListener(OnKeyListener())
@@ -102,4 +104,19 @@ class MainActivity : AppCompatActivity() {
102104
return true
103105
}
104106
}
107+
108+
private inner class OnItemClickListener : AdapterView.OnItemClickListener {
109+
override fun onItemClick(
110+
adapterView: AdapterView<*>?,
111+
view: View?,
112+
position: Int,
113+
id: Long
114+
) {
115+
val intent = Intent(this@MainActivity, DetailActivity::class.java)
116+
// タップされた行番号のデータを取り出す
117+
val result: Item? = mAdapter.getItem(position)
118+
intent.putExtra("url", result?.url)
119+
startActivity(intent)
120+
}
121+
}
105122
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent"
7+
tools:context=".DetailActivity">
8+
9+
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
10+
android:id="@+id/web_view"
11+
android:layout_width="match_parent"
12+
android:layout_height="match_parent" />
13+
14+
</androidx.constraintlayout.widget.ConstraintLayout>

0 commit comments

Comments
 (0)