1. <dd id="erndk"></dd>
                1. Android強化——FragmentManger和Fragment

                  互聯網 2022/4/30 6:14:48

                  Fragment受FragmentManger控制。 通過FragmentManger獲取FragmentTransaction。ft.replace(containerView,fragment)替換fragment使用fragmentTransaction = supportFragmentManager.beginTransaction();fragmentTransaction.replace(R.id.main_page_container,baseFragm…

                  Fragment受FragmentManger控制。

                  通過FragmentManger獲取FragmentTransaction。ft.replace(containerView,fragment)替換fragment

                   

                  使用

                  fragmentTransaction = supportFragmentManager.beginTransaction();
                          fragmentTransaction.replace(R.id.main_page_container,baseFragment);
                          fragmentTransaction.commit();

                   

                  而且fragment內嵌套fragment需要getChildFragmentManger(); 

                   

                   

                  public class HomeFragment extends BaseFragment {
                      private TabLayout tabLayout;
                      private ViewPager viewPager;
                      private HomeViewPagerAdapter homeViewPagerAdapter;
                      private HomePresenter homePresenter;
                  
                      @Override
                      protected int getRootViewResId() {
                          return R.layout.fragment_home;
                      }
                  
                      @Override
                      public void initView(View view) {
                          tabLayout = view.findViewById(R.id.tabLayout);
                          viewPager = view.findViewById(R.id.viewPager);
                          homeViewPagerAdapter = new HomeViewPagerAdapter(getChildFragmentManager());
                          viewPager.setAdapter(homeViewPagerAdapter);
                          tabLayout.setupWithViewPager(viewPager);
                      }
                  
                      @Override
                      public void initData() {
                  
                      }
                  
                      @Override
                      public void initEvent() {
                  
                      }
                  
                      @Override
                      public void initPresenter() {
                          homePresenter = new HomePresenter();
                          homePresenter.setHomeFragment(HomeFragment.this);
                      }
                  
                      @Override
                      public void loadData() {
                          homePresenter.getCategories();
                      }
                  
                      public void loadCategories(Categories categories){
                          homeViewPagerAdapter.setCategories(categories);
                      }
                  }

                   

                  HomeViewPagerAdapter

                  public class HomeViewPagerAdapter extends FragmentPagerAdapter {
                      private ArrayList<Categories.Data> dataArray = new ArrayList<>();
                      
                      public HomeViewPagerAdapter(@NonNull FragmentManager fm) {
                          super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
                      }
                  
                      @Nullable
                      @Override
                      public CharSequence getPageTitle(int position) {
                          String title = dataArray.get(position).getTitle();
                          return title;
                      }
                  
                      @NonNull
                      @Override
                      public Fragment getItem(int position) {
                          HomePagerFragment homePagerFragment = new HomePagerFragment();
                          return homePagerFragment;
                      }
                  
                      @Override
                      public int getCount() {
                          return dataArray.size();
                      }
                  
                      public void setCategories(Categories categories){
                          dataArray.clear();
                          List<Categories.Data> data = categories.getData();
                          dataArray.addAll(data);
                          notifyDataSetChanged();
                      }
                  
                  }

                   

                  隨時隨地學軟件編程-關注百度小程序和微信小程序
                  關于找一找教程網

                  本站文章僅代表作者觀點,不代表本站立場,所有文章非營利性免費分享。
                  本站提供了軟件編程、網站開發技術、服務器運維、人工智能等等IT技術文章,希望廣大程序員努力學習,讓我們用科技改變世界。
                  [Android強化——FragmentManger和Fragment]http://www.yachtsalesaustralia.com/tech/detail-318173.html

                  贊(0)
                  關注微信小程序
                  程序員編程王-隨時隨地學編程

                  掃描二維碼或查找【程序員編程王】

                  可以隨時隨地學編程啦!

                  技術文章導航 更多>
                  掃一掃關注最新編程教程
                  国产在线拍揄自揄视频菠萝

                        1. <dd id="erndk"></dd>