<i id='V2XRr'><tr id='V2XRr'><dt id='V2XRr'><q id='V2XRr'><span id='V2XRr'><b id='V2XRr'><form id='V2XRr'><ins id='V2XRr'></ins><ul id='V2XRr'></ul><sub id='V2XRr'></sub></form><legend id='V2XRr'></legend><bdo id='V2XRr'><pre id='V2XRr'><center id='V2XRr'></center></pre></bdo></b><th id='V2XRr'></th></span></q></dt></tr></i><div id='V2XRr'><tfoot id='V2XRr'></tfoot><dl id='V2XRr'><fieldset id='V2XRr'></fieldset></dl></div>
  • <tfoot id='V2XRr'></tfoot>
  • <legend id='V2XRr'><style id='V2XRr'><dir id='V2XRr'><q id='V2XRr'></q></dir></style></legend>

        <bdo id='V2XRr'></bdo><ul id='V2XRr'></ul>

      1. <small id='V2XRr'></small><noframes id='V2XRr'>

        使用 FragmentPagerAdapter 的圆形 ViewPager

        时间:2023-10-04

      2. <i id='h4Nsl'><tr id='h4Nsl'><dt id='h4Nsl'><q id='h4Nsl'><span id='h4Nsl'><b id='h4Nsl'><form id='h4Nsl'><ins id='h4Nsl'></ins><ul id='h4Nsl'></ul><sub id='h4Nsl'></sub></form><legend id='h4Nsl'></legend><bdo id='h4Nsl'><pre id='h4Nsl'><center id='h4Nsl'></center></pre></bdo></b><th id='h4Nsl'></th></span></q></dt></tr></i><div id='h4Nsl'><tfoot id='h4Nsl'></tfoot><dl id='h4Nsl'><fieldset id='h4Nsl'></fieldset></dl></div>

            <small id='h4Nsl'></small><noframes id='h4Nsl'>

              <bdo id='h4Nsl'></bdo><ul id='h4Nsl'></ul>
            • <tfoot id='h4Nsl'></tfoot>

                  <tbody id='h4Nsl'></tbody>
                • <legend id='h4Nsl'><style id='h4Nsl'><dir id='h4Nsl'><q id='h4Nsl'></q></dir></style></legend>
                • 本文介绍了使用 FragmentPagerAdapter 的圆形 ViewPager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想实现一个 ViewPager,它使用 Fragments 并且可以以曲线运动方式滑动,例如页(A<-->B<-->C<-->A).我已经阅读了几篇关于如何做到这一点的帖子,例如返回有多少元素的假计数,并将位置设置在中间的开头.如何创建圆形viewpager?

                  I would like to implement a ViewPager which uses Fragments and can be swiped in a curcular motion e.g. Page (A<-->B<-->C<-->A). I have read a couple of posts on how this is done, e.g. returning a fake count of how many elements there are and setting the position at the start in the middle. how to create circular viewpager?

                  这些似乎都基于 PagerAdapter.当我在扩展 FragmentPagerAdapter 时尝试做类似的事情时,只要我返回一个 fakeCount 的页面,当我扫过我的片段时就会得到一个异常,我只有 2 个片段.异常:java.lang.IllegalStateException:无法更改片段的标记.

                  These all seem to be based of a PagerAdapter. When I try to do a similar thing while extending FragmentPagerAdapter, as soon as I return a fakeCount of pages I get an exception when I Swipe through my Fragments, I only have 2 Fragments. Exception: java.lang.IllegalStateException: Can't change tag of fragment.

                  我认为这是因为 FragmentManager 认为我在位置 2 但位置 2 指向位置 0 的片段.有谁知道我该如何避免这种情况?我想我应该尝试扩展 Fragmentmanager.任何示例或帮助将不胜感激.

                  I think this is caused as the FragmentManager thinks I am in position 2 but position 2 points to the fragment at position 0. Does anyone know how I can avoid this? I am thinking I should experiment with extending Fragmentmanager. Any examples or help with this would be greatly appreciated.

                  推荐答案

                  我知道这有点晚了,但这对我来说是这样的:

                  I know it is a bit late but this is how it worked for me:

                  我需要在 3 个片段之间进行循环滑动,因此我将这 3 个片段和两个片段设为虚拟以帮助我实现页面循环:

                  I needed a circular swipe between 3 fragments, so I made those 3 and two more virtual to help me implement the page looping:

                  public static class FirstViewFragment extends Fragment {
                      // Empty Constructor
                      public FirstViewFragment() {
                      }
                  
                      @Override
                      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                          return inflater.inflate(R.layout.fragment_landing_1, container, false);
                      }
                  }
                  
                  public static class SecondViewFragment extends Fragment {
                      // Empty Constructor
                      public SecondViewFragment() {
                      }
                  
                      @Override
                      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                          return inflater.inflate(R.layout.fragment_landing_2, container, false);
                      }
                  }
                  
                  public static class ThirdViewFragment extends Fragment {
                      // Empty Constructor
                      public ThirdViewFragment() {
                      }
                  
                      @Override
                      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                          return inflater.inflate(R.layout.fragment_landing_3, container, false);
                      }
                  }
                  

                  还有两个虚拟片段使我能够从第一个向左滑动,从最后一个向右滑动.第一个虚拟膨胀的布局与最后一个实际的布局相同,最后一个虚拟的布局与第一个实际的布局相同:

                  And two more virtual fragments that enabled me to swipe left from the first and right from the last. The first virtual inflates the same layout as the last actual and the last virtual the same layout as the first actual:

                  public static class StartVirtualFragment extends Fragment {
                      public StartVirtualFragment() {}
                  
                      @Override
                      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                          return inflater.inflate(R.layout.fragment_landing_3, container, false);
                      }
                  }
                  
                  public static class EndVirtualFragment extends Fragment {
                      public EndVirtualFragment() {}
                  
                      @Override
                      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                          return inflater.inflate(R.layout.fragment_landing_1, container, false);
                      }
                  }
                  

                  我的适配器:

                  private class ViewPagerAdapter extends FragmentPagerAdapter {
                  
                      public ViewPagerAdapter(FragmentManager fm) {
                          super(fm);
                      }
                  
                      @Override
                      public Fragment getItem(int i) {
                          switch (i) {
                              case 0:
                                  return new StartVirtualFragment();
                              case 1:
                                  if (firstViewFragment == null) {
                                      firstViewFragment = new FirstViewFragment();
                                  }
                                  return firstViewFragment;
                              case 2:
                                  if (secondViewFragment == null) {
                                      secondViewFragment = new SecondViewFragment();
                                  }
                                  return secondViewFragment;
                              case 3:
                                  if (thirdViewFragment == null) {
                                      thirdViewFragment = new ThirdViewFragment();
                                  }
                                  return thirdViewFragment;
                              case 4:
                                  return new EndVirtualFragment();
                          }
                          return null;
                      }
                  
                      @Override
                      public int getCount() {
                          return 5;
                      }
                  }
                  

                  而我的页面监听器我使用了 onPageScrollStateChanged 来设置正确的页面并实现循环:

                  And my page listener I used the onPageScrollStateChanged to set the correct page and implement the loop:

                  viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                          @Override
                          public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                          }
                  
                          @Override
                          public void onPageSelected(int position) {
                          }
                  
                          @Override
                          public void onPageScrollStateChanged(int state) {
                              if (state == ViewPager.SCROLL_STATE_DRAGGING) {
                                  int pageCount = viewPager.getChildCount();
                                  int currentItem = viewPager.getCurrentItem();
                                  if (currentItem == 0) {
                                      viewPager.setCurrentItem(pageCount - 2, false);
                                  } else if (currentItem == pageCount - 1) {
                                      viewPager.setCurrentItem(1, false);
                                  }
                              }
                          }
                      });
                  

                  最后:

                  viewPager.setCurrentItem(1);
                  

                  希望我能帮上忙

                  这篇关于使用 FragmentPagerAdapter 的圆形 ViewPager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:从子片段更改 ViewPager 片段 下一篇:getChildFragmentManager() 方法未定义

                  相关文章

                • <tfoot id='9sNfY'></tfoot>

                    <small id='9sNfY'></small><noframes id='9sNfY'>

                  1. <legend id='9sNfY'><style id='9sNfY'><dir id='9sNfY'><q id='9sNfY'></q></dir></style></legend>
                    <i id='9sNfY'><tr id='9sNfY'><dt id='9sNfY'><q id='9sNfY'><span id='9sNfY'><b id='9sNfY'><form id='9sNfY'><ins id='9sNfY'></ins><ul id='9sNfY'></ul><sub id='9sNfY'></sub></form><legend id='9sNfY'></legend><bdo id='9sNfY'><pre id='9sNfY'><center id='9sNfY'></center></pre></bdo></b><th id='9sNfY'></th></span></q></dt></tr></i><div id='9sNfY'><tfoot id='9sNfY'></tfoot><dl id='9sNfY'><fieldset id='9sNfY'></fieldset></dl></div>
                        <bdo id='9sNfY'></bdo><ul id='9sNfY'></ul>