如何仅在 PagerSlidingTabStrip 中单击其选项卡时加载片段数据

How to load fragment data only when its tab is clicked in PagerSlidingTabStrip(如何仅在 PagerSlidingTabStrip 中单击其选项卡时加载片段数据)

本文介绍了如何仅在 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 中单击其选项卡时加载片段数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何仅在 PagerSlidingTabStrip 中单击其选项卡时加载片段数据