본문 바로가기
안드로이드

[Android] Fragment

by 엘딘 2022. 3. 22.

화면(Screen) 변환 없이 새로운 View 위젯으로 화면에 일부에만 표시

 

<layout>

1. activiti_fragment_menu.xml

   Fragment변환 버튼 생성용 xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <Button
            android:text="First"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/button1"
            android:onClick="onClick"
            android:layout_weight="1"/>
    <Button
            android:text="Second"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/button2"
            android:onClick="onClick"
            android:layout_weight="1"/>
    <Button
            android:text="Third"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/button3"
            android:onClick="onClick"
            android:layout_weight="1"/>
</LinearLayout>

 

 

2. 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">
	
    <fragment
            android:id="@+id/fragment_menu"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:name="com.example.sample23.FragmentMenu"
            tools:layout="@layout/activity_fragment_menu"
            tools:ignore="MissingConstraints"/>

    // 버튼 클릭시 변환되는 화면 표출
    <FrameLayout
            android:id="@+id/content"
            android:layout_width="598dp"
            android:layout_height="910dp"
            app:layout_constraintTop_toBottomOf="@+id/fragment_menu"/>

</androidx.constraintlayout.widget.ConstraintLayout>

 

3. activity_fragment_one(two, three).xml

   화면변환 확인용

   단, tools:context=".MainActivity"는 삭제

<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">

    <TextView
            android:text="First Fragment"
            android:textSize="30dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>


</androidx.constraintlayout.widget.ConstraintLayout>

 

4. FragmentOne/Two/Three 생성(View)
class FragmentOne : Fragment() {
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.activity_fragment_one, container, false)
    }
}

 

5. MainActivity
class MainActivity : AppCompatActivity(), View.OnClickListener {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

		// 앱 실행시 처음 화면 출력
        val fm = supportFragmentManager
        val fragmentTransaction = fm.beginTransaction()
        fragmentTransaction.add(R.id.content, FragmentOne())
        fragmentTransaction.commit()
    }

    override fun onClick(v: View?) {                // v는 매개변수
        Log.d("버튼 클릭!", "버튼 클릭!")

        var fr: Fragment? = null

        if(v?.id == R.id.button1){
            fr = FragmentOne()
        }else if(v?.id == R.id.button2){
            fr = FragmentTwo()
        }else if(v?.id == R.id.button3){
            fr = FragmentThree()
        }

        val fm = supportFragmentManager
        val fragmentTransaction = fm.beginTransaction()
        // fragment 교체
        fragmentTransaction.replace(R.id.content, fr!!)
        fragmentTransaction.commit()
    }
}

'안드로이드' 카테고리의 다른 글

[Android] Button  (0) 2022.03.23
[Android] Intent  (0) 2022.03.22
[Android] RecylerView  (0) 2022.03.21
[Android] 2  (0) 2022.02.20
[Android] 1  (0) 2022.02.17

댓글