티스토리 뷰
[Android][Kotlin] Nested Fragment (프래그먼트 안에 프래그먼트), 프래그먼트 교체, childFragmentManager
yoo.o 2021. 1. 14. 14:42구조
액티비티(A) 위에 프래그먼트(B)를 얹고, 그 프래그먼트(B) 위에 프래그먼트(C)를 또 얹었다.
자녀 프래그먼트(C)위의 버튼을 눌러서 프래그먼트(C)를 다른 프래그먼트(C')로 교체하는 코드이다.
코드
MyPageFragment.kt (부모 프래그먼트. 프래그먼트 B)
var frontFragment = FrontFragment()
var backFragment = BackFragment()
class MyPageFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_my_page, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
}
// frontFragment로 전환하는 함수
fun front(){
childFragmentManager.beginTransaction()
.replace(R.id.fragment, frontFragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.commit()
}
// backFragment로 전환하는 함수
fun back(){
childFragmentManager.beginTransaction()
.replace(R.id.fragment, backFragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.commit()
}
}
먼저, 부모 프래그먼트의 코드에 자녀 프래그먼트를 frontFragment(프래그먼트 C)나 backFragment(프래그먼트 C')로 바꾸는 front(), back() 함수들을 만들어줬다.
액티비티 위에 그려진 프래그먼트를 교체할때는 supportFragmentManager.begineTransaction()을 사용하지만,
프래그먼트 위에 그린 프래그먼트를 교체할때는 childFragmentManager.beginTransaction()을 사용해야한다.
FrontFragment.kt (자녀 프래그먼트. 프래그먼트 C)
class FrontFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_bodyfront, container, false)
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
BUTTON.setOnClickListener {
(parentFragment as MyPageFragment).back()
}
}
}
그 후, 자녀 프래그먼트에서 버튼클릭시 함수를 호출하는 코드를 써줬다.
중요한건, 액티비티에서 프래그먼트를 그리는 상황이랑 코드가 다르다.
프래그먼트에서 액티비티를 접근할때 사용하는 (activity as ACTIVITY_NAME).FUNCTION_NAME()이 아니라
(parentFragment as FRAGMENT_NAME).FUNCTION_NAME() 으로 사용해야한다.
프래그먼트 만들기
[Android][Kotlin] Fragment 설정, Fragment to Activity 화면 전환
- fragment 적용 탭(tab)형식 UI를 사용하려하는데 fragment가 가장 적합해서 적용해봤다 fragment 2개 먼저 생성 후 import android.content.Context import android.content.Intent import android.os.Bundle i..
yuuj.tistory.com
프래그먼트에서 액티비티 접근하기
[Android][Kotlin] Fragment에서 Activity 접근하기
프래그먼트에서 액티비티 함수 접근하는 법 프래그먼트에 써주기 (activity as ACTIVITYNAME).FUNCTION() 콜백 없어도 되는구나!!!! 이런 좋은게있는줄도 모르고.. 삽질 끝에 알아냈다
yuuj.tistory.com
액티비티에서 프래그먼트 접근하기
[Android][Kotlin] Activity 에서 Fragment 접근하기
Activity에서 Fragment 접근하는법 var userFragment: UserFragment = supportFragmentManager.findFragmentById(R.id.fragment_holder) as UserFragment // 프래그먼트의 editText 접근하기 username = userinfoF..
yuuj.tistory.com
- Total
- Today
- Yesterday
- 파이썬 최대공약수
- 전화번호목록 파이썬
- 데이터바인딩 뷰바인딩 차이
- 백준알고리즘
- 카카오 키해시
- 코틀린 뷰바인딩
- 코틀린 데이터바인딩
- 코틀린
- Kotlin
- 카카오 기출
- 안드로이드
- 코틀린 리스트뷰
- 안드로이드 키해시
- 코틀린 뷰페이저
- flutter simultor
- counting sort
- 안드로이드 카카오톡으로 로그인
- 백준 1644
- 프로그래머스
- 백준
- 백준 1806
- 소수 구하기 파이썬
- 시뮬레이터 키보드
- 투포인터 알고리즘 파이썬
- TextFormField keyboard
- 투포인터 알고리즘
- 백준 2003
- 카카오톡으로 로그인 오류
- kotlin fragment
- 코틀린 바텀네비게이션
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 31 |