如何加载片段数据只有当它的标签被点击PagerSlidingTabStrip [英] How to load fragment data only when its tab is clicked in PagerSlidingTabStrip

查看:143
本文介绍了如何加载片段数据只有当它的标签被点击PagerSlidingTabStrip的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的项目中使用 PagerSlidingTabStrip 和我表示片段数据。它是一个伟大的图书馆伟大的工程。每个片段由这是从web服务加载的学者的讲座。在code来访问Web服务是在片段的 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.

目前的code是pretty的标准,我从提供的示例[Android解决方案]了帮助。 1

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

寻呼机适配器如下:

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;
    }

}

它返回两个片段,信息和讲座。讲座段调用Web服务来得到一个学者的讲座。

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

活动code是:

@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(本); 并在最后不得不 onPageSelected(INT为arg0)我活性。我觉得现在很容易。然后我修改公开片段的getItem(INT为arg0) ScholarDetailPagerAdapter 上面并返回空。但是,引发异常。我认为这将使得寻呼机空的观点。但当然,我错了。

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.

要做出相应的选项卡时是可见的,我也试图重写了以下事件,并在其中写了数据加载调用确保片段加载数据:

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.

在此先感谢

解决

感谢@YaqubAhmad指着我在正确的方向。它现在解决了,必要的工作。我想将它张贴在这里,所以别人找相同的解决方案也许能有所帮助。

Thanks to @YaqubAhmad for pointing me in the right direction. Its solved now and works as required. I thought to post it here so someone else looking for same solution may find it helpful.

  1. 让我感动的实际服务电话/网格人口code另一种方法 loadLectures()
  2. 添加一个公共静态的newInstance(INT scholarId)方法,设置 scholarId 价值片段实例的参数。我把这种方法从适配器获得这个片段的新实例。

  1. I moved the actual service call/grid population code to another method loadLectures()
  2. added a public static newInstance(int scholarId) method which sets the scholarId value as Fragment instance's arguments. I call this method from the Adapter to get new instance of this fragment.

public static ScholarLecturesFragment newInstance(int scholarId){

    ScholarLecturesFragment f = new ScholarLecturesFragment();

    // Supply index input as an argument.
    Bundle args = new Bundle();

    args.putInt("ScholarId", scholarId);

    f.setArguments(args);

    return f;
}

  • onCreateView 方法仅返回充气后的基本视图。电网这里没有人口(这将确保一个空的片段被加载),因此没有服务电话

  • onCreateView method just returns the base view after inflation. No population of Grid here (This will make sure an empty fragment is loaded) hence no service call

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    
        return inflater.inflate(R.layout.lectures_layout, null);
    
    }
    

  • 被通知
  • 覆盖 setUserVisibleHint 当片段可见。这种情况发生在用户点击其相应的选项卡或刷卡吧。在这里,我加载数据和填充网格。

  • override setUserVisibleHint to be notified when the Fragment is visible. This happens when the user clicks its respective tab or swipes to it. Here I load the data and populate the grid.

    布尔_areLecturesLoaded = FALSE;

    boolean _areLecturesLoaded = false;

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

  • loadScholars()检索scolarId从参数( getArguments()调用getInt(ScholarId)) ,将此参数传递到服务和负载讲座。完美。

    loadScholars() retrieves the scolarId from the arguments (getArguments().getInt("ScholarId")), passes this parameter to the service and loads lectures. Works perfectly.

    推荐答案

    您可以覆盖 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆