๊ด€๋ฆฌ ๋ฉ”๋‰ด

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
    }

}