在FragmentPagerAdapter更新数据数据、FragmentPagerAdapter

2023-09-07 09:56:00 作者:宠坏万千少女

我主要片段和Viewpager与该片段(1)3页。在主要的片段(1)我选择的城市和acording城市主要片段的值(1)从服务器加载数据,并传递给FragmentPagerAdapter。在第一次一切正常,但如果我只滑动从一个侧面网页到另一个后选择另一个城市(2)viewpager更新的数据。

所以,如果我选择另一种减到起初我看到空白页1,向前滑动到第2页和第3,并返回第1页数据更新1和第3页之后,但在第2页是空的。

所以我不`吨得到哪里是错误?谢谢

这是我的FragmentStatePagerAdapter code

 私有类MyPagerAdapter扩展FragmentStatePagerAdapter {    公共MyPagerAdapter(FragmentManager FM){        超(FM);    }    @覆盖    公众诠释getItemPosition(Object对象){        Log.d(getItemPosition,123);        返回POSITION_NONE;    }    @覆盖    公共CharSequence的getPageTitle(INT位置){        资源解析度= getResources();        的String []标题= res.getStringArray(R.array.title_spa_viewpager_array);        回到标题[位置]    }    @覆盖    公共android.support.v4.app.Fragment的getItem(INT POS){        开关(POS){            情况下0:返回FirstFragment.newInstance(mSpa.getSpaName()+ SpaInfoParserObject.MySpaInfo.getSpaInfoText());            案例1:返回SecondFragment.newInstance(,SpaInfoParserObject.MySpaInfo);            案例2:返回PhotoFragment.newInstance(,SpaInfoParserObject.MySpaInfo.getSpaInfoPic(),MSPA);            默认:返回FirstFragment.newInstance(mSpa.getSpaPhone());        }    }    @覆盖    公众诠释的getCount(){        返回3;    }} 

解决方案

有您的code两个主要问题。

首先,我在你的previous问题之一,一看,我看到你使用:

  FragmentManager F =((FragmentActivity)getActivity())getSupportFragmentManager()。 
工业互联网安全开始走向消费领域

初​​始化 ViewPager 的适配器。这是不正确,如果嵌入 ViewPager (连同其片段而成为嵌套的片段)在另一个片段那么你必须使用 FragmentManager getChildFragmentManager()方法返回(在片段类),而不是上面的行。如果您已经使用 getChildFragmentManager()忽略这部分以其他方式更改code使用它。

其次,更新的片段一个 ViewPager 之前已经讨论过(虽然你使用嵌套的片段其相同的过程),你需要采取在考虑两种情况,即的的碎片可能是 ViewPager

片段或者是当前可见的碎片或左/右侧的可见片段(例如最接近的片段,如果位置0是当前可见的,你会在内存中有两个片段:位置0和位置1)。该片段具有自己的看法建成并随时可以显示给用户,你需要直接更新他们的数据/视图(通过调用例如一些setter方法​​)。以访问该片段最简单的方法是使用在code this问题(这是非常相关的,也请注意,你使用嵌套的片段, getFragmentManager()替换getChildFragmentManager()

除上述之一的任何其他位置。为了处理这种情况下,你应该从 ViewPager 来检索数据(这将反映新数据的变化)在其 onCreateView()每个片段如果用户朝着这一片段挥笔将被调用。一些示例code:

  //在onCreateView()YourParentFragmentClass父=(YourParentFragmentClass)getParentFragment();//父片断拿着ViewPager,您可以使用进一步用它来//获取数据 

这是每一个片段 ViewPager 应该有这片code来处理数据更新。例如,如果你刷到第三片断,然后更新数据,你应该直接更新片段2和3片段1将然而,当 ViewPager 将重新更新了视图(当用户从​​片段3挥笔至片段2),在那一刻上面这段code将获得从父片段中的新数据,这将是最新的一个。

您也不会需要传递的数据作为该的newInstance()方法。

参数

I have main fragment and Viewpager with 3 pages in this fragment(1). In main fragment(1) i choose city and acording to the value of the city main fragment(1) loads data from server and pass in to FragmentPagerAdapter. At first time everything is ok but if i choose another city(2) the data in viewpager updates only after sliding pages from one side to another.

So if i choose another sity at first i see blank page 1, after sliding forward to page 2 and 3 and returning to page 1 the data updates on 1 and 3 page but the 2 page is empty.

So i don`t get where is the error? thanks

here is my FragmentStatePagerAdapter code

 private class MyPagerAdapter extends FragmentStatePagerAdapter {

    public MyPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public int getItemPosition(Object object) {
        Log.d("getItemPosition","123");
        return POSITION_NONE;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        Resources res = getResources();
        String[] title = res.getStringArray(R.array.title_spa_viewpager_array);
        return title[position];
    }

    @Override
    public android.support.v4.app.Fragment getItem(int pos) {
        switch(pos) {
            case 0: return FirstFragment.newInstance(mSpa.getSpaName()+SpaInfoParserObject.MySpaInfo.getSpaInfoText());
            case 1: return SecondFragment.newInstance("",SpaInfoParserObject.MySpaInfo);
            case 2: return PhotoFragment.newInstance("", SpaInfoParserObject.MySpaInfo.getSpaInfoPic(), mSpa);
            default: return FirstFragment.newInstance(mSpa.getSpaPhone());
        }
    }

    @Override
    public int getCount() {
        return 3;
    }
}

解决方案

There are two main problems with your code.

First of all I had a look at one of your previous question and I saw that you use:

FragmentManager f = ((FragmentActivity) getActivity()).getSupportFragmentManager();

to initialize the adapter of the ViewPager. This is incorrect, if you embed the ViewPager(along with its fragments which become nested fragments) in another Fragment then you must use the FragmentManager returned by the getChildFragmentManager() method(of the Fragment class) instead of the line above. If you already use getChildFragmentManager() ignore this part otherwise change the code to use it.

Secondly, updating the fragments of a ViewPager has been discussed before(although you use nested fragment its the same process) and you need to take in consideration two cases in which the ViewPager's fragments might be:

the fragment is either the current visible fragment or the closest fragment on the left/right side of the visible fragment(for example, if position 0 is the current one visible you'll have two fragments in memory: position 0 and position 1). This fragments have their views built and are ready to be shown to the user and you need to update their data/views directly(by calling some setter methods for example). To easiest way to access this fragment is to use the code in this question (which is very related, also note that you're using nested fragments so getFragmentManager() will be replaced by getChildFragmentManager())

any other position than the one above. To handle this case you should make each fragment from the ViewPager to retrieve the data(which will reflect the new data changes) in their onCreateView() which will be called if the user swipes towards that fragment. Some sample code:

// in the onCreateView()
YourParentFragmentClass parent = (YourParentFragmentClass) getParentFragment();
// parent is the Fragment holding the ViewPager and you can use further use it to
// get data

Every fragment from the ViewPager should have this piece of code to handle the data update. For example, if you swipe to the third fragment and then update the data, you should directly update fragments 2 and 3. Fragment 1 however will be updated when the ViewPager will recreate its view(when the user swipes from fragment 3 to fragment 2), at that moment the above piece of code will get the new data from the parent fragment which will be the latest one.

You also will not need to pass the data as arguments in that newInstance() method.