DBILITY

독거 가능성 100% 노후에 라면값이라도 하게 센스를 발휘합시다!😅
Please click on the ad so that I can pay for ramen in my old age!
点击一下广告,让老后吃个泡面钱吧!
老後にラーメン代だけでもするように広告を一回クリックしてください。

precaution when android fragment programmatic creation 본문

android

precaution when android fragment programmatic creation

DBILITY 2024. 3. 18. 12:06
반응형

RecyclerView의 position이 화면 회전시 처음으로 돌아가버려서 테스트 함

Activity > FrameLayout > Fragment >  RecyclerView 상태에서

Activity onCreate시 savedInstanceState가 null일때만 Fragment를 Programmatic 생성해야함

ListAdapter에 onAttachedToRecyclerView override해서 확인해보니 두번 실행되어 발견함...역시..초보는 어려워

Activity > FragmentContainerView일때는 별도 생성을 안하니 이상이 없었음.

Activity > ViewPager2 > Fragment > RecyclerView인 경우는 어떻게 해야하지? Fragment가 사라짐

ViewPager2의 adapter를보니  RecyclerView기반이라는데 그것때문인가

Set a new adapter to provide page views on demand.
If you're planning to use Fragments as pages, implement FragmentStateAdapter. If your pages are Views, implement RecyclerView.Adapter as usual.
If your pages contain LayoutTransitions, then those LayoutTransitions must have animateParentHierarchy set to false. Note that if you have a ViewGroup with animateLayoutChanges="true" in your layout xml file, a LayoutTransition is added automatically to that ViewGroup. You will need to manually call getLayoutTransition().setAnimateParentHierarchy(false) on that ViewGroup after you inflated the xml layout, like this:
  View view = layoutInflater.inflate(R.layout.page, parent, false);
  ViewGroup viewGroup = view.findViewById(R.id.animated_viewgroup);
  viewGroup.getLayoutTransition().setAnimateParentHierarchy(false);
  
Params:
adapter – The adapter to use, or null to remove the current adapter
See Also:
androidx.viewpager2.adapter.FragmentStateAdapter, RecyclerView.setAdapter(RecyclerView.Adapter)

savedInstanceState 체크를 제외하니 됨. lifecycle을 확인해봐야 겠다. 우선은 된다.

반응형
Comments