• <legend id='emOMm'><style id='emOMm'><dir id='emOMm'><q id='emOMm'></q></dir></style></legend>
      <bdo id='emOMm'></bdo><ul id='emOMm'></ul>
    <tfoot id='emOMm'></tfoot>

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

    1. <i id='emOMm'><tr id='emOMm'><dt id='emOMm'><q id='emOMm'><span id='emOMm'><b id='emOMm'><form id='emOMm'><ins id='emOMm'></ins><ul id='emOMm'></ul><sub id='emOMm'></sub></form><legend id='emOMm'></legend><bdo id='emOMm'><pre id='emOMm'><center id='emOMm'></center></pre></bdo></b><th id='emOMm'></th></span></q></dt></tr></i><div id='emOMm'><tfoot id='emOMm'></tfoot><dl id='emOMm'><fieldset id='emOMm'></fieldset></dl></div>
      1. 圆形 ViewPager.片段在第一轮后无法正常工作

        时间:2023-10-04
            <tbody id='AsDU6'></tbody>
          <legend id='AsDU6'><style id='AsDU6'><dir id='AsDU6'><q id='AsDU6'></q></dir></style></legend>
              <bdo id='AsDU6'></bdo><ul id='AsDU6'></ul>
                <i id='AsDU6'><tr id='AsDU6'><dt id='AsDU6'><q id='AsDU6'><span id='AsDU6'><b id='AsDU6'><form id='AsDU6'><ins id='AsDU6'></ins><ul id='AsDU6'></ul><sub id='AsDU6'></sub></form><legend id='AsDU6'></legend><bdo id='AsDU6'><pre id='AsDU6'><center id='AsDU6'></center></pre></bdo></b><th id='AsDU6'></th></span></q></dt></tr></i><div id='AsDU6'><tfoot id='AsDU6'></tfoot><dl id='AsDU6'><fieldset id='AsDU6'></fieldset></dl></div>

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

                  <tfoot id='AsDU6'></tfoot>

                1. 本文介绍了圆形 ViewPager.片段在第一轮后无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  好的,所以我需要循环 ViewPager.我很难实施它.现在我已经实现了它并且就圆形滚动而言它工作正常.但我注意到一个问题.即,第一轮滚动片段后不起作用.我有三个片段,里面有一个按钮.按钮在第一轮工作,但当我回到第一页按钮不起作用.p.s 我使用假页面技术使 viewpager 循环.

                  Ok, so i needed circular ViewPager. I was having really hard time to implement it. Now that i have implemented it and it is working fine as far as circular scroll is concern. But i have notice a problem.Which is, after first round of scroll fragments don't work. I have three Fragments with a button inside them. Button works in first round but when i come back to first page button doesn't work. p.s i used fake pages technique to make viewpager circular.

                  这里是代码

                          mViewPager.setAdapter(new PagerAdapter(getSupportFragmentManager(), this));
                      //mViewPager.setOnPageChangeListener(new CircularViewPagerHandler(mViewPager));
                      mViewPager.setCurrentItem(1, false);
                      mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                          @Override
                          public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                  
                          }
                  
                          @Override
                          public void onPageSelected(int position) {
                              if (position == 0) {
                                  mViewPager.setCurrentItem(3, false);
                                  Log.d("TAG",
                                          "Swiped before first page, looping and resetting to last page.");
                              } else if (position == 4) {
                                  mViewPager.setCurrentItem(1, false);
                                  Log.d("TAG",
                                          "Swiped beyond last page, looping and resetting to first page.");
                              }
                          }
                  
                          @Override
                          public void onPageScrollStateChanged(int state) {
                              Log.d("onPageScrollState", "state:" + state);
                              int currentPage = mViewPager.getCurrentItem();
                              if(currentPage==1){
                                  imgCenter.setImageResource(R.drawable.take_ride);
                                  imgLeft.setImageResource(R.drawable.give_ride);
                                  imgRight.setImageResource(R.drawable.setting);
                              }
                              if(currentPage==2){
                                  imgCenter.setImageResource(R.drawable.setting);
                                  imgRight.setImageResource(R.drawable.take_ride);
                                  imgLeft.setImageResource(R.drawable.give_ride);
                              }
                              if (currentPage==3){
                                  imgCenter.setImageResource(R.drawable.give_ride);
                                  imgLeft.setImageResource(R.drawable.take_ride);
                                  imgRight.setImageResource(R.drawable.setting);
                  
                              }
                          }
                      });
                  

                  这里是 PagerAdapter 类

                  here is PagerAdapter class

                    public class PagerAdapter extends FragmentPagerAdapter {
                  
                  Context mcontext;
                  
                  public PagerAdapter(FragmentManager fm, Context context) {
                      super(fm);
                      mcontext = context;
                  }
                  
                  @Override
                  public Fragment getItem(int position) {
                      if (position == 0) {
                          return new HomeFragment2();
                      }
                      else if(position==1){
                          return new HomeFragment();
                      }
                      else if (position == 2) {
                          return new SettingFragment();
                      }
                      else if(position==3){
                          return new HomeFragment2();
                      }
                      else
                          return new HomeFragment();
                  }
                  
                  @Override
                  public int getCount() {
                      return 5;
                  }
                  }
                  

                  推荐答案

                  我和你一起创建了一个简单的测试应用 PagerAdapter 和简单的 Fragments代码>按钮:

                  I've created a simple test-app with you PagerAdapter and simple Fragments with a Button:

                  而且效果很好!

                  我已经上传了源代码这里,所以你可以检查一下,看看和你的有什么不同.

                  I've uploaded the source code here, so you can check it out and see if there's any difference with yours.

                  作为 Fragment 的我使用:

                  public class FragmentA extends android.support.v4.app.Fragment {
                  
                      @Nullable
                      @Override
                      public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
                          View rootView = inflater.inflate(R.layout.fragment_a, container, false);
                          rootView.findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
                              @Override
                              public void onClick(View v) {
                                  Snackbar.make(v, "Hi, fragment A", Snackbar.LENGTH_SHORT).show();
                              }
                          });
                          return rootView;
                      }
                  }
                  

                  那么我可以推荐什么

                  • 用一些虚拟的片段替换你的片段(如上面的那个).如果它有效 - 问题出在 Fragment 代码中,而不是在 Circular ViewPager

                  • replace your fragments with some dummy ones (like the one above). If it works - the issue is in the Fragment code, not in the Circular ViewPager

                  如果它仍然不起作用 - 我会尝试而不是多次重新创建片段 - 创建一次并存储:

                  If it still doesn't work - I'd try to instead of re-creating fragments multiple times - create them once and store:

                   public class PagerAdapter extends FragmentPagerAdapter {
                      Context mcontext;
                      Fragment [] fragments;
                      public PagerAdapter(FragmentManager fm, Context context, Fragment [] fragments) {
                          super(fm);
                          mcontext = context;
                          this.fragments = fragments;
                      }
                  
                      @Override
                      public Fragment getItem(int position) {
                          return fragments[position];
                      }
                  
                      @Override
                      public int getCount() {
                          return fragments.length;
                      }
                  }
                  

                  而在Activity中:

                  Fragment[] fragments = {
                          Fragment.instantiate(this, FragmentC.class.getName()),
                          Fragment.instantiate(this, FragmentA.class.getName()),
                          Fragment.instantiate(this, FragmentB.class.getName()),
                          Fragment.instantiate(this, FragmentC.class.getName()),
                          Fragment.instantiate(this, FragmentA.class.getName()),
                  };
                  .....
                  viewPager.setAdapter(new PagerAdapter(getSupportFragmentManager(), this, fragments));
                  

                  如果有帮助,请告诉我!

                  Let me know, if it helps!

                  这篇关于圆形 ViewPager.片段在第一轮后无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何知道片段何时在 viewpager 中实际可见 下一篇:尝试在空对象引用上调用虚拟方法 'void android.support.v4.view.ViewPager.

                  相关文章

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

                    <tfoot id='M40em'></tfoot>

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