본문 바로가기

프로그래밍/Kotlin

Fragment Kotlin Android 프래그먼트 코틀린 안드로이드

반응형

Kotlin으로 Fragment 기능 구현

 

구조 : main Activity 가 있고, 일부 영역에 Fragment 화면을 보여줌

구성 : MainActivity, Fragment1, Fragment2 와 각각의 layout 3개.

버튼을 누르면 해당 Fragment가 나옴.

 

MainActivity.kt

package kr.co.enigmah2k.myfragment

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
import kr.co.enigmah2k.myfragment.fg.ImageFragment
import kr.co.enigmah2k.myfragment.fg.TextFragment

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Set a click listener for first button widget
        button1.setOnClickListener {
            // Get the text fragment instance
            val textFragment = TextFragment()

            // Get the support fragment manager instance
            val manager = supportFragmentManager

            // Begin the fragment transition using support fragment manager
            val transaction = manager.beginTransaction()

            // Replace the fragment on container
            transaction.replace(R.id.fragment_container,textFragment)
            transaction.addToBackStack(null)

            // Finishing the transition
            transaction.commit()
        }


        // Set a click listener for second button widget
        button2.setOnClickListener {
            // Get the text fragment instance
            val imageFragment = ImageFragment()

            // Get the support fragment manager instance
            val manager = supportFragmentManager

            // Begin the fragment transition using support fragment manager
            val transaction = manager.beginTransaction()

            // Replace the fragment on container
            transaction.replace(R.id.fragment_container,imageFragment)
            transaction.addToBackStack(null)

            // Finishing the transition
            transaction.commit()
        }

    }
}

activity_main.xml


<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    < Button
            android:text="button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/button1" android:layout_marginTop="8dp"
            app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent"
            android:layout_marginStart="64dp"/>
    < Button
            android:text="Button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" tools:layout_editor_absoluteY="8dp"
            android:id="@+id/button2" app:layout_constraintStart_toEndOf="@+id/button1"
            android:layout_marginStart="64dp" app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="8dp"/>
    < LinearLayout
            android:id="@+id/fragment_container"
            android:orientation="vertical"
            android:layout_width="401dp"
            android:layout_height="539dp"
            android:background="#c1cfba"
            tools:layout_editor_absoluteX="5dp" android:layout_marginTop="96dp"
            app:layout_constraintTop_toTopOf="parent" android:layout_marginBottom="8dp"
            app:layout_constraintBottom_toBottomOf="parent">
    
</androidx.constraintlayout.widget.ConstraintLayout>

 

TextFragment.kt

package kr.co.enigmah2k.myfragment.fg

import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
import androidx.fragment.app.Fragment
import kr.co.enigmah2k.myfragment.R

class TextFragment : Fragment(){
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        // Get the custom view for this fragment layout
        val view = inflater!!.inflate(R.layout.layout_text_fragment,container,false)

        // Get the text view widget reference from custom layout
        val tv = view.findViewById(R.id.text_view)

        // Set a click listener for text view object
        tv.setOnClickListener{
            // Change the text color
            tv.setTextColor(Color.RED)

            // Show click confirmation
            Toast.makeText(view.context,"TextView clicked.",Toast.LENGTH_SHORT).show()
        }

        // Return the fragment view/layout
        return view
    }

    override fun onPause() {
        super.onPause()
    }
/*
    override fun onAttach(context: Context?) {
        super.onAttach(context)
    }
*/
    override fun onDestroy() {
        super.onDestroy()
    }

    override fun onDetach() {
        super.onDetach()
    }

    override fun onStart() {
        super.onStart()
    }

    override fun onStop() {
        super.onStop()
    }
}

layout_text_fragment.xml

< ?xml version="1.0" encoding="utf-8"?>
< LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    < TextView
            android:id="@+id/text_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="This is a sample text from Text Fragment"
            android:textSize="50sp"
    />

 

ImageFragment.kt

package kr.co.enigmah2k.myfragment.fg

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import kr.co.enigmah2k.myfragment.R

class ImageFragment : Fragment(){
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        // Return the fragment view/layout
        return inflater!!.inflate(R.layout.layout_image_fragment,container,false)
    }

    override fun onPause() {
        super.onPause()
    }
/*
    override fun onAttach(context: Context?) {
        super.onAttach(context)
    }
*/
    override fun onDestroy() {
        super.onDestroy()
    }

    override fun onDetach() {
        super.onDetach()
    }

    override fun onStart() {
        super.onStart()
    }

    override fun onStop() {
        super.onStop()
    }
}

 

layout_image_fragment.xml

< ?xml version="1.0" encoding="utf-8"?>
< LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    < ImageView
            android:id="@+id/image_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/flower"
    />

 

참고

https://android--code.blogspot.com/2018/02/android-kotlin-fragment-example.html

 

 

 

반응형