<tfoot id='wBfou'></tfoot>

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

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

        如何正确使用 ViewPager 的片段?

        How to properly use fragments with ViewPager?(如何正确使用 ViewPager 的片段?)

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

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

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

                  <legend id='KqdJt'><style id='KqdJt'><dir id='KqdJt'><q id='KqdJt'></q></dir></style></legend><tfoot id='KqdJt'></tfoot>
                  本文介绍了如何正确使用 ViewPager 的片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我知道许多应用程序在 ViewPager 中使用片段.我的应用程序中也需要它.我没有找到任何指南,我该怎么做才能排除 这个.这是我的代码:

                  I know many applications using fragments in ViewPager. I need it in my application too. I have no found any guide how I can to do it exclude this. And here is my code:

                  public class MainActivity extends FragmentActivity {
                  
                      Vector<Fragment> fragments;
                      ViewPager viewPager;
                  
                      @Override
                      public void onCreate(Bundle savedInstanceState) {
                          super.onCreate(savedInstanceState);
                          setContentView(R.layout.main);
                  
                          viewPager = (ViewPager) findViewById(R.id.pager);
                  
                          fragments = new Vector<Fragment>();
                          fragments.add(Fragment.instantiate(this, Fragment1.class.getName()));
                          fragments.add(Fragment.instantiate(this, Fragment2.class.getName()));
                  
                          viewPager.setOffscreenPageLimit(fragments.size());
                          PagerAdapter pagerAdapter = new PagerAdapter(super.getSupportFragmentManager(), fragments);
                  
                          viewPager.setAdapter(pagerAdapter);
                  
                          viewPager.setCurrentItem(0);
                      }
                  
                      class PagerAdapter extends FragmentPagerAdapter {
                  
                          private List<Fragment> fragments;
                  
                          public PagerAdapter(FragmentManager fm, List<Fragment> fragments) {
                              super(fm);
                              this.fragments = fragments;
                          }
                  
                          @Override
                          public Fragment getItem(int position) {
                              return this.fragments.get(position);
                          }
                  
                          @Override
                          public int getCount() {
                              return this.fragments.size();
                          }
                  
                      }
                  
                  }
                  

                  但是这段代码不能很好地工作.有时我在 Fragment1 中收到关于 getActivity() 返回 null 的错误.我从该站点和其他有关此错误的帖子中阅读了许多帖子,现在我知道这是不好的方法-使用 Vector 来存储片段.但我仍然不知道,如何正确使用片段进行 ViewPager.请帮忙.

                  But this code doesn't working well. Sometimes I get error in my Fragment1 about getActivity() return null. I read many posts from this site and other about this error and now I know that is bad way - use Vector for storing fragments. But I still don't know, how to do ViewPager with fragments properly. Please, help.

                  推荐答案

                  即使我一开始也很难理解这个概念,我不会说我完全理解它.但是,

                  Even I had trouble understanding the concept at first and I won't say that I fully understood it. But,

                  这是我在示例应用程序中使用片段的方式:

                  Here's how I am using fragment in my sample application:

                  第 1 步:创建我的适配器:

                  Step 1: Creating my Adapter:

                  public class AllPagesAdapter extends FragmentStatePagerAdapter {
                  
                  
                  
                              public AllPagesAdapter(FragmentManager fm) {
                  
                                  super(fm);
                  
                              }
                  
                              @Override
                              public Fragment getItem(int index) {
                  
                          switch (index) {
                              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();
                  
                  
                          }
                          return null;
                  
                  
                      }
                  
                      @Override
                      public int getCount() {
                          return 5;
                      }
                  
                  }
                  

                  第 2 步:在主要活动中,我正在扩展片段活动并实现 ActionBar.TabListener.这是我的做法:

                  Step 2: In the main activity, I am extending fragment activity and implementing an ActionBar.TabListener. Here's how I am doing it:

                  public class SomeActivity extends FragmentActivity implements ActionBar.TabListener {
                  
                      public ViewPager viewPager;
                      private AllPagesAdapter mAdapter;
                      private ActionBar actionBar;
                      private String [] tabs = {"Fragment1","Fragment2","Fragment3","Fragment4","Fragment5"};
                  
                      @Override
                      protected void onCreate(Bundle savedInstanceState) {
                          super.onCreate(savedInstanceState);
                          setContentView(R.layout.activity_main);
                  
                          //Initializing all stuff
                          viewPager = (ViewPager)findViewById(R.id.pager);
                  
                          actionBar = getActionBar();
                          mAdapter = new AllPagesAdapter(getSupportFragmentManager());
                          viewPager.setAdapter(mAdapter);
                          actionBar.setHomeButtonEnabled(true);
                          actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
                  
                  
                  
                          //Add the tabs here
                          for(String tab_name:tabs){
                              actionBar.addTab(actionBar.newTab().setText(tab_name).setTabListener(this));
                          }
                  
                          viewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
                  
                              @Override
                          public void onPageSelected(int position){
                  
                                  //on Page change, that particular page should be selected
                                  actionBar.setSelectedNavigationItem(position);
                              }
                  
                              @Override
                                  public void onPageScrolled(int arg0,float arg1,int arg2){
                  
                              }
                              @Override
                          public void onPageScrollStateChanged(int position){
                  
                              }
                  
                          });
                      }
                  
                  
                  
                      @Override
                      public void onTabSelected(Tab tab, FragmentTransaction fragmentTransaction) {
                  
                          viewPager.setCurrentItem(tab.getPosition());
                      }
                  
                      @Override
                      public void onTabUnselected(Tab tab, FragmentTransaction fragmentTransaction) {
                  
                  
                  
                      }
                  
                      @Override
                      public void onTabReselected(Tab tab, FragmentTransaction fragmentTransaction) {
                  
                          viewPager.setCurrentItem(tab.getPosition());
                  
                      }
                  }
                  

                  第 3 步:我只有一个片段用于此示例:

                  Step 3: I have just one fragment for this example:

                  代码如下:

                  public class Fragment1 extends Fragment {
                  
                      @Override
                      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                               Bundle savedInstanceState) {
                  
                          //Can be layout you want it to be.
                          View databaseview = inflater.inflate(R.layout.database, container, false);
                  
                          return databaseview;
                  
                      }
                  
                  }
                  

                  第 4 步:最后一部分是布局:这将是起点.

                  Step 4: Last part is the layout: This will be starting point.

                  <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
                      android:id="@+id/pager"
                      android:layout_width="match_parent"
                      android:layout_height="match_parent"></android.support.v4.view.ViewPager>
                  

                  最后,一件重要的事情是,片段的数量应该等于主活动中定义的字符串数组.如果没有,应用程序会崩溃.

                  Lastly, one important thing is that, number of fragments should be equal to the string array defined in the main activity. If not, the app crashes.

                  希望我解释清楚..:)

                  Hope I explained it well..:)

                  我知道这是一篇很老的帖子,但这个答案可供未来的观众使用.

                  I know this is a very old post but this answer can be used for future viewers.

                  这篇关于如何正确使用 ViewPager 的片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How can I use CClistview in COCOS2d Android?(如何在 COCOS2d Android 中使用 CClistview?)
                  cocos2d-android: how to display score(cocos2d-android:如何显示分数)
                  Sqlite database not copied from asset folder Android(Sqlite 数据库未从资产文件夹 Android 复制)
                  SQLite Database Copy Appears Corrupted When Generated by Device and not Emulator(SQLite 数据库副本在由设备而不是模拟器生成时出现损坏)
                  Android file copy(安卓文件拷贝)
                  Android how to detect Copy event of Edittext in android(Android如何在android中检测Edittext的Copy事件)

                      <tbody id='48Op1'></tbody>

                    <small id='48Op1'></small><noframes id='48Op1'>

                    <tfoot id='48Op1'></tfoot>
                  1. <legend id='48Op1'><style id='48Op1'><dir id='48Op1'><q id='48Op1'></q></dir></style></legend>
                            <bdo id='48Op1'></bdo><ul id='48Op1'></ul>

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