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

        <small id='8q9mU'></small><noframes id='8q9mU'>

      1. <tfoot id='8q9mU'></tfoot>

        • <bdo id='8q9mU'></bdo><ul id='8q9mU'></ul>
        <legend id='8q9mU'><style id='8q9mU'><dir id='8q9mU'><q id='8q9mU'></q></dir></style></legend>

      2. 通过按钮单击更改 viewpager 片段

        时间:2023-10-04
          <tbody id='xU4T3'></tbody>
      3. <tfoot id='xU4T3'></tfoot>
      4. <legend id='xU4T3'><style id='xU4T3'><dir id='xU4T3'><q id='xU4T3'></q></dir></style></legend>

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

            • <small id='xU4T3'></small><noframes id='xU4T3'>

                <bdo id='xU4T3'></bdo><ul id='xU4T3'></ul>
                  本文介绍了通过按钮单击更改 viewpager 片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试通过单击一个按钮来更改 viewpager 片段.我有 5 个片段,每个片段都有自己的 xml 文件(frag1.xml、frag2.xml 等).每个片段都有 5 个按钮,它们应该转到 viewpager 的其他页面.但问题是如何在 FragmentPageAdapter 中检查单击了哪个按钮以及如何到达那里?

                  I'm trying to change the viewpager fragment by clicking on a button. I have 5 fragments, each fragment has it's own xml file (frag1.xml, frag2.xml, and so on). Every fragment has it's 5 buttons that should go to other pages of the viewpager. But the problem is how do I check in the FragmentPageAdapter which button is clicked and how to get there?

                  我将展示我拥有的代码,然后我认为应该很清楚.可以把它想象成一个底部有点的主屏幕,我点击某个点,就会转到相应的屏幕.

                  I'll show the code I have then it should be clear I think. Think of it like a homescreen that has dots at the bottom and I you click a certain dot, you'll go to the corresponding screen.

                  FragmentPagerAdapter

                  public class MyFragmentPagerAdapter extends FragmentPagerAdapter{
                  
                      final int PAGE_COUNT = 6;
                  
                      /** Constructor of the class */
                      public MyFragmentPagerAdapter(FragmentManager fm) {
                          super(fm);
                      }
                  
                      /** This method will be invoked when a page is requested to create */
                      @Override
                      public Fragment getItem(int arg0) {
                  
                          switch(arg0){
                  
                          case 0:
                  
                              return new Fragment1();
                  
                          case 1:
                              return new Fragment2();
                  
                          case 2:
                              return new Fragment3();
                  
                          case 3:
                              return new Fragment4();
                  
                          case 4:
                              return new Fragment5();
                  
                          default:
                              return null;
                  
                          }       
                      }
                  
                      /** Returns the number of pages */
                      @Override
                      public int getCount() {
                          return PAGE_COUNT;
                      }
                  }
                  

                  Frag1.java

                  public class Frag1 extends Fragment {
                  
                      @Override
                      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                              Bundle savedInstanceState) {
                          View v = inflater.inflate(R.layout.frag_one, container, false);
                  
                  
                          OnClickListener changeFrag = new OnClickListener() {            
                              @Override
                              public void onClick(View v) {
                              /*When I click this button in my fragment, I'd like it to go to fragment 3 for example*/
                              }
                          };
                  
                          ImageButton btnT = (ImageButton) v.findViewById(R.id.frag3);
                          btnT.setOnClickListener(changeFrag);
                  
                          return v;
                      }
                  }
                  

                  MainActivity

                  公共类 MainActivity 扩展 FragmentActivity {

                  public class MainActivity extends FragmentActivity {

                      @Override
                      protected void onCreate(Bundle savedInstanceState) {
                          super.onCreate(savedInstanceState);
                          setContentView(R.layout.activity_main);/** Getting a reference to the ViewPager defined the layout file */
                          ViewPager pager = (ViewPager) findViewById(R.id.pager);
                  
                          /** Getting fragment manager */
                          FragmentManager fm = getSupportFragmentManager();
                  
                          /** Instantiating FragmentPagerAdapter */
                          MyFragmentPagerAdapter pagerAdapter = new MyFragmentPagerAdapter(fm);
                  
                          /** Setting the pagerAdapter to the pager object */
                          pager.setAdapter(pagerAdapter);
                  
                         //pager.setPageTransformer(true, new ZoomOutPageTransformer());
                      }
                  
                      @Override
                      public boolean onCreateOptionsMenu(Menu menu) {
                          getMenuInflater().inflate(R.menu.activity_main, menu);
                          return true;
                      }
                  }
                  

                  这样的事情可能吗?有人可以帮助我朝正确的方向前进吗?

                  Is something like this possible? Can someone help me in the right direction please?

                  推荐答案

                  我不确定你说的改变"到底是什么.

                  I'm not sure what exactly you said about "Change".

                  如果你问的是改变页面显示索引,你可以试试

                  If you're asking about change page display index, you can try

                  ViewPager.setCurrentItem(int pageIndex, boolean isSmoothScroll);
                  

                  或者,如果您要询问更改内容,这就是我所做的

                  Or, if you're asking about change content, here's what I did

                  public class MyPagerAdapter extends FragmentStatePagerAdapter
                  {
                    private FragmentManager fragMan;
                    private ArrayList<Fragment> fragments=new ArrayList<Fragment>();
                  
                    public void clearAll() //You can clear any specified page if you want...
                    {
                      for(int i=0; i<fragments.size(); i++)
                      fragMan.beginTransaction().remove(fragments.get(i)).commit();
                      fragments.clear();
                      fragments=new ArrayList<Fragment>();
                      notifyDataSetChanged();
                    }
                  
                    public void addList() //Add some new fragment...
                    {
                      listFrag=new ListFragment();
                      fragments.add(list);
                    }
                  }
                  

                  希望对你有帮助~

                  这篇关于通过按钮单击更改 viewpager 片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:TabLayout 的选项卡未显示 下一篇:'IllegalStateException: Activity 已被销毁'当 Activity 重启后

                  相关文章

                    <bdo id='UwFON'></bdo><ul id='UwFON'></ul>
                  <tfoot id='UwFON'></tfoot>

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