Notice
Recent Posts
Recent Comments
Link
์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
Tags
- ์นดํ๋๊ฐ
- ๊ฐ๋ฐ
- ์ ๋ฆฌํธ๋ฆฌํธ
- ํ๋ก๊ทธ๋๋ฐ
- ๋ฐ์ดํฐ๋ฒ ์ด์ค
- Java
- css
- dmz๋
- dmz๋คํธ์ํฌ
- ํ์ฒ์ ๋ฆฌํธ๋ฆฌํธ
- ์ํ
- ์นํผ๋ธ๋ฆฌ์ฑ
- ๋ ์
- ๊ฐ์์ ์
- ์๋ฐ์คํฌ๋ฆฝํธ
- ์๋ฐ
- JavaScript
- html
- ํ์ด์ฌ
- ๋คํธ์ํฌdmz
- database
- ์ฝ๋ฉ
- ๋๊ฐ
- ์ฑ
- ํฐ์คํ ๋ฆฌ์ฑ๋ฆฐ์ง
- Python
- ๋ฐฉํ๋ฒฝdmz
- dmz๊ตฌ์ฑ
- ์ค๋ผํด
- ์ค๋ธ์
Archives
- Today
- Total
JiYoung Dev ๐ฅ
[Android] ํ๋๊ทธ๋จผํธ(Fragment) (2023.07.25) ๋ณธ๋ฌธ
full stack/์๋๋ก์ด๋
[Android] ํ๋๊ทธ๋จผํธ(Fragment) (2023.07.25)
Shinjio 2023. 7. 25. 14:16๋ฐ์ํ
ํ๋๊ทธ๋จผํธ(Fragment)
https://developer.android.com/guide/components/fragments?hl=ko
ํ๋๊ทธ๋จผํธ | Android ๊ฐ๋ฐ์ | Android Developers
A Fragment represents a behavior or a portion of user interface in an Activity. You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities. You can think of a fragment as a modular section
developer.android.com

ํ๋๊ทธ๋จผํธ๋ ๋ ๋ฆฝ์ ์ผ ์ ์๋ค. ๋ฌด์กฐ๊ฑด Activity์ ์ฐ๊ฒฐ๋์ด ์์ด์ผ ํจ



MainActivity
package com.sjy.fragment
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.FrameLayout
import com.google.android.material.bottomnavigation.BottomNavigationView
class MainActivity : AppCompatActivity() {
lateinit var bnv : BottomNavigationView
lateinit var fl : FrameLayout
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
bnv = findViewById(R.id.bnv)
fl = findViewById(R.id.fl)
//bnv์์ ์ ํํ ๋ฉ๋ด์ ๋ฐ๋ผ fl์ ํ์ํ Fragment ๊ฐ์ ๋ผ์ฐ๊ธฐ
//setOnItemSelectedListener : item ์ ํ
bnv.setOnItemSelectedListener {
//it : ์ ํํ ์์ดํ
๊ฐ์ ธ์ฌ ์ ์์
Log.d("id", it.itemId.toString())
//์ ํํ ์์ดํ
์ ์์ด๋๊ฐ ํญ์ id์ ๊ฐ์ ๋ fl(ํ๋ ์๋ ์ด์์)์ Fragment๋ก ๊ต์ฒดํจ
when(it.itemId){
R.id.tab1 -> {
supportFragmentManager.beginTransaction().replace(
R.id.fl,
Fragment1()
).commit()
}
R.id.tab2 -> {
supportFragmentManager.beginTransaction().replace(
R.id.fl,
Fragment2()
).commit()
}
R.id.tab3 -> {
supportFragmentManager.beginTransaction().replace(
R.id.fl,
Fragment3()
).commit()
}
R.id.tab4 -> {
supportFragmentManager.beginTransaction().replace(
R.id.fl,
Fragment4()
).commit()
}
}
//boolean : true/false => false ์ด๋ฒคํธ์ธ์์ ์ ๋ชปํจ, true => ์ด๋ฒคํธ ์ธ์์ ํจ์จ์ด ๋ ๋์
true
}
}
}
Fragment1
package com.sjy.fragment
import android.content.Context
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.webkit.WebView
import android.webkit.WebViewClient
class Fragment1 : Fragment() {
//View์์ฑ(**)
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
var view = inflater.inflate(R.layout.fragment_1, container, false)
//WebView component ๊ฐ์ ธ์ค๊ธฐ
var wv : WebView = view.findViewById(R.id.wv)
//SharedPreference ๊ฐ์ ธ์ค๊ธฐ
val spf = requireActivity().getSharedPreferences(
"mySPF",
Context.MODE_PRIVATE
)
//SharedPreference ์์ ์ ์ฅ๋ ๊ฐ ๊บผ๋ด์ ์ฌ์ฉํ๊ธฐ
//getString(ํค๊ฐ, ๊ธฐ๋ณธ๊ฐ)
var url : String? = spf.getString("url", "http://www.google.com")
//๋ณด์ฌ์ฃผ๊ณ ์ถ์ web url ์ง์
// val url : String = "https://www.google.com"
//web setting
//1. javascript ์ฌ์ฉ ๊ฐ๋ฅํ๋๋ก ํ์ฉ
val ws = wv.settings
ws.javaScriptEnabled = true
//2. WebView์ ํด๋ผ์ด์ธํธ ์ค์
wv.webViewClient = WebViewClient()
//3. WebView์ url ์ ์ฉ
//url? -> null๊ฐ ํ์ฉ
//url!! -> null๊ฐ ํ์ฉX
wv.loadUrl(url!!)
return view
}
}
Fragment2
package com.sjy.fragment
import android.content.Context
import android.content.SharedPreferences
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
class Fragment2 : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
var view = inflater.inflate(R.layout.fragment_2, container, false)
var btnUrl : Button = view.findViewById(R.id.btnUrl)
var etUrl : EditText = view.findViewById(R.id.etUrl)
lateinit var url : String
//๋ฒํผ์ ํด๋ฆญํ๋ฉด ์ฌ์ฉ์๊ฐ ์์ฑํ url๊ฐ ๊ฐ์ ธ์ค๊ธฐ
btnUrl.setOnClickListener{
url = etUrl.text.toString()
//url๊ฐ ์ ์ฅํ๊ธฐ (SharedPreference -> ๋ด๋ถ ๋ฉ๋ชจ๋ฆฌ์ ์ ์ฅ -> ๋ชจ๋ fragment์์ ์ฌ์ฉ๊ฐ๋ฅ)
// - editor ์ฌ์ฉ
val spf = requireActivity().getSharedPreferences("mySPF",Context.MODE_PRIVATE)
//MODE_PRIVATE : ๋ด๋ถ ์บ์์ ์ ์ฅ -> ์ ์ฅ๋ ๊ฐ์ด ๋
ธ์ถ๋์ง ์๋๋ก
val editor = spf.edit() //์๋ํฐ ์์ฑ
editor.putString("url", url) //ํค-์์ผ๋ก ์ ์ฅ
editor.commit()
}
return view
}
}
๋ฐ์ํ
'full stack > ์๋๋ก์ด๋' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Android] android studio ๋ก๋ฉ ํ๋ฉด ์ค์ (2023.07.26) (0) | 2023.07.26 |
---|---|
[Android] android studio ํฐํธ ์ ์ฉ (2023.07.26) (0) | 2023.07.26 |
[Android] ํฌ์ผ๋ชฌ ๋๊ฐ ๋ง๋ค๊ธฐ (Volley, RecyclerView) (2023.07.24~25) (0) | 2023.07.25 |
[Android] RecyclerView (2023.07.20, 24) (0) | 2023.07.20 |
[Android] visibility & enabled ์์ฑ (2023.07.19) (0) | 2023.07.19 |