<legend id='HlIrQ'><style id='HlIrQ'><dir id='HlIrQ'><q id='HlIrQ'></q></dir></style></legend>

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

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

        <bdo id='HlIrQ'></bdo><ul id='HlIrQ'></ul>
      1. 如何仅在 PagerSlidingTabStrip 中单击其选项卡时加载片段数据

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

      3. <small id='lOrOC'></small><noframes id='lOrOC'>

        <tfoot id='lOrOC'></tfoot>

            <tbody id='lOrOC'></tbody>

        1. <legend id='lOrOC'><style id='lOrOC'><dir id='lOrOC'><q id='lOrOC'></q></dir></style></legend>

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

                  本文介绍了如何仅在 PagerSlidingTabStrip 中单击其选项卡时加载片段数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我在我的项目中使用 PagerSlidingTabStrip 并以片段的形式显示数据.它是一个很棒的图书馆,效果很好.每个片段都包含从 Web 服务加载的学者讲座.访问 Web 服务的代码在 Fragment 的 onCreateView 方法中.目前所有片段的数据都是在添加到ViewPager的同时加载的.我想知道是否有任何方式仅在用户单击/滑动到特定选项卡时才加载片段的数据.不是一次全部.

                  I am using PagerSlidingTabStrip in my project and am showing data in fragments. Its a great library which works great. Each Fragment consists of a scholar's lectures which are loaded from a web service. The code to access the web service is in onCreateView method of the Fragment. Currently all the fragment's data is loaded at the same time when they are added to ViewPager. I was wondering if there is any way of loading the Fragment's data only when a user clicks/swipes to a specific tab. Not everything at a time.

                  当前代码非常标准,我从提供的示例 [android 解决方案] 中获得了帮助.1

                  Current code is pretty standard, I took help from the provided example [android solution].1

                  Pager Adapter 如下:

                  Pager Adapter is as follows:

                  public class ScholarDetailPagerAdapter extends FragmentPagerAdapter {
                  
                      private final String[] TITLES = { "Scholar Info", "Lectures"};
                      Scholar _scholar;
                  
                      public ScholarDetailPagerAdapter(FragmentManager fm, Scholar scholar) {
                          super(fm);
                          _scholar = scholar;
                      }
                  
                      @Override
                      public CharSequence getPageTitle(int position) {
                          return TITLES[position];
                      }
                  
                      @Override
                      public Fragment getItem(int arg0) {
                          if(arg0 == 0)
                              return ScholarsInfoFragment.netInstance(_scholar.Name, _scholar.Information, _scholar.ThumbnailUrl);
                          else
                              return ScholarLecturesFragment.newInstance(_scholar.ScholarId);
                      }
                  
                      @Override
                      public int getCount() {
                          return TITLES.length;
                      }
                  
                  }
                  

                  它返回两个片段,Info 和 Lectures.Lectures Fragment 调用 Web 服务来获取学者的讲座.

                  It returns TWO fragments, Info and Lectures. Lectures fragment calls a web service to get a scholar's lectures.

                  活动代码为:

                  @Override
                  public void onCreate(Bundle savedInstanceState) {
                      super.onCreate(savedInstanceState);
                  
                      // set the Above View
                      setContentView(R.layout.tabbed_view);
                      Scholar scholar = getScholar(getIntent());      
                  
                      _tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
                      _pager = (ViewPager) findViewById(R.id.pager);
                      _adapter = new ScholarDetailPagerAdapter(getSupportFragmentManager(), scholar);
                  
                      _pager.setAdapter(_adapter);
                  
                      final int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources()
                              .getDisplayMetrics());
                      _pager.setPageMargin(pageMargin);
                  
                      _tabs.setViewPager(_pager);
                  } 
                  

                  我尝试添加 _tabs.setOnPageChangeListener(this); 并最终在我的活动中添加了 onPageSelected(int arg0).我以为现在很容易.然后我在上面的ScholarDetailPagerAdapter中修改了public Fragment getItem(int arg0),返回null而不是返回两个片段.但这引发了一个例外.我以为它会在寻呼机中呈现空视图..但我当然错了.

                  I tried adding _tabs.setOnPageChangeListener(this); and ended up having onPageSelected(int arg0) in my activity as well. I thought It would be easy now. Then I modified public Fragment getItem(int arg0) in ScholarDetailPagerAdapter above and returned null instead of returning two fragments. But that raised an exception. I thought It would render empty views in pager .. but of course I was wrong.

                  为了确保 Fragment 在其各自的选项卡被单击且可见时加载数据,我还尝试覆盖其以下事件并在其中编写数据加载调用:

                  To make sure Fragment loads data when its respective tab is clicked and is visible, I also have tried overriding its following events and wrote the data loading call in them:

                  1. onResume() 没用.在活动中添加时仍然加载数据

                  1. onResume() Not worked. Still loads data when added in activity

                  尝试覆盖 onHiddenChanged

                  Tried overriding onHiddenChanged

                  @Override   
                  public void onHiddenChanged(boolean hidden) {
                  
                      super.onHiddenChanged(hidden);
                  
                      if(!hidden){
                          loadLectures();
                      }
                  

                  }

                  这也不起作用.没有加载数据,就好像根本没有触发此事件一样.

                  This did not work too. No data loaded as if this event is not fired at all.

                  我该怎么做?我只是想在用户单击选项卡时加载一个片段.

                  How do I do that? I simply want to load a fragment when user clicks on a tab.

                  提前致谢

                  推荐答案

                  您可以覆盖片段的 setUserVisibleHint 事件以了解其是否对用户可见,然后加载您的数据.类似于以下内容:

                  You could override setUserVisibleHint event of the fragment to know if its visible to the user and then load your data. something like following:

                  boolean _areLecturesLoaded = false;
                  
                  @Override
                   public void setUserVisibleHint(boolean isVisibleToUser) {
                       super.setUserVisibleHint(isVisibleToUser);
                       if (isVisibleToUser && !_areLecturesLoaded ) {
                        loadLectures(); 
                        _areLecturesLoaded = true;
                       }
                   }
                  

                  这篇关于如何仅在 PagerSlidingTabStrip 中单击其选项卡时加载片段数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:TabFragment 中的 ViewPager 未再次加载 下一篇:我在 viewpager 选项卡中的片段不刷新

                  相关文章

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

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

                  1. <legend id='xvsLT'><style id='xvsLT'><dir id='xvsLT'><q id='xvsLT'></q></dir></style></legend>
                      • <bdo id='xvsLT'></bdo><ul id='xvsLT'></ul>
                    1. <tfoot id='xvsLT'></tfoot>