在FragmentPagerAdapter片段正确交互 [英] Properly Interacting with fragments in FragmentPagerAdapter

查看:170
本文介绍了在FragmentPagerAdapter片段正确交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是FragmentPagerAdapter一个活动,第3页片段之间,除了ViewPager。根据从第一个分片输入(谷歌地图上的标记),我在做API请求当用户切换到片段2或3,然后更新这些片段的看法。

I'm using a FragmentPagerAdapter for an activity, to page between 3 fragments, in addition to a ViewPager. Based on input from the first fragment (markers on a google map), I'm making API requests when the user switches to fragment 2 or 3, and then updating those fragment views.

总的想法是,我管理的FragmentPagerAdapter内SparseArray,然后创建了一个viewPager onPageChangeListener:

The general idea is that I manage a SparseArray within the FragmentPagerAdapter, and then create an onPageChangeListener for the viewPager:

pager.setAdapter(pagerAdapter);
pager.setOnPageChangeListener(new OnPageChangeListener() {

    @Override
    public void onPageSelected(int position) {
        if (position == 1) {
            SecondFragment fragment = (SecondFragment) pagerAdapter.getFragment(1);
            fragment.update(currentMarker);
        } else if (position == 2) {
            ThirdFragment fragment = (ThirdFragment) pagerAdapter.getFragment(2);
            fragment.update(currentMarker);
        }
    }
});

我也overrided的instantiateItem和destroyItem在FragmentPagerAdapter,正常删除的,在我的sparseArray重新添加片段的引用。

I also overrided the instantiateItem and destroyItem in the FragmentPagerAdapter, to properly remove and re-add fragment references in my sparseArray.

这工作pretty很好,但问题是,当我转动我的设备,当我第二次或第三片段页面上。这让我想呼吁片段的方法(在该活动的使用onPageSelected)当一个NullPointerException异常 - 我猜,因为活动是重建,那些片段尚未创建使用onPageSelected触发时

This works pretty well, but the issue is when I rotate my device when I'm on the second or third fragment page. It gives me a NullPointerException when trying to call a method on the fragment (in the activity's onPageSelected) - I'm guessing because the activity is recreated, those fragments haven't been created yet when onPageSelected triggers.

我在想,一个更好的方法可能是想出了一个办法让这些片段只是使API调用时,他们看到,而不是由活动触发它(这可能是清洁 - 少通信/从片段通常是更好),但是任何的建议是AP preciated。

I'm thinking that a better approach might be to come up with a way to have the fragments just make the API call when they're visible, instead of having the activity trigger it (which might be cleaner - less communication to/from fragments is typically better), but any advice would be appreciated.

推荐答案

ONSELECT(第2页)当你做API调用,让你的API调用的结果,应该有更新的API供的onSaveInstanceState引用的对象回调

OnSelect(page2 ) when you do the API calls and get your API call result , there should be a callback to update an object referenced by the API for 'OnSaveInstanceState'.

如果你这样做,那么IMO,这里是什么ü从框架中得到:

If you do that then IMO , here is what u get from the framework:

API返回结果。

结果更新片段
   该片段被任何调用处理为'的onSaveInstanceState

results update a fragment that fragment gets processed by any call to 'OnSaveInstanceState'

OnOrientationChange ::与条件是发生之后的API返回结果
   片段保存状态反映了API的结果。

OnOrientationChange :: with condition that it occurs AFTER the API returns results Fragment is saved with state reflecting the API results

活性是由于方向改变重建
   在SavedInstanceState!= null,则重新实例片段,包括从早期的API调用的残留状况....

Activity is recreated due to Orientation change on SavedInstanceState != null it reinstantiates the fragment from the state including the residue of the earlier api call....

看到接受<一个href=\"http://stackoverflow.com/questions/6787071/android-fragment-how-to-save-states-of-views-in-a-fragment-when-another-fragmen\">answer这里更详细的内容。

see accepted answer here for additional detail

这篇关于在FragmentPagerAdapter片段正确交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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