简体   繁体   English

Android ViewPager FragmentStatePagerAdapter问题

[英]Android ViewPager FragmentStatePagerAdapter issue

I'm building an app that uses a Viewpager with a FragmentStatePagerAdapter. 我正在构建一个将Viewpager与FragmentStatePagerAdapter一起使用的应用程序。 The FragmentStatePagerAdapter gets an array of objects witch is later used to map each object data to a fragment. FragmentStatePagerAdapter获取对象数组,稍后将其用于将每个对象数据映射到一个片段。

The adapter loads 3 fragments into memory (onCreateView is called 3 times when I'm on a page), it loads the current fragment and the next two. 适配器将3个片段加载到内存中(当我在页面上时,onCreateView被调用3次),它将加载当前片段和接下来的两个片段。

I have the following issue: 我有以下问题:

I must change the content of the next fragment based on content change in the current fragment I'm in. 我必须根据当前所在片段的内容更改来更改下一个片段的内容。

I've tried to modify the array in the FragmentStatePagerAdapter and then call notifydatasetchanged on the adapter, but the adapter doesn't load again the next page. 我试图修改FragmentStatePagerAdapter中的数组,然后在适配器上调用notifydatasetchanged,但是适配器不会在下一页再次加载。

what is the best way to pull this off? 做到这一点的最佳方法是什么?

some code and scenario: the current fragment I am on contains users data, the logged user can follow him or unfollow. 一些代码和方案:我当前所在的片段包含用户数据,登录的用户可以关注他或取消关注。

here is the onClick code: 这是onClick代码:

if(mListener!=null)
                        mListener.onTweetUserFollowingStatusChanged(tweet.getUser());

the callback from listener in the activity with the viewpage: 来自活动的带有视图页的侦听器的回调:

@Override
    public void onTweetUserFollowingStatusChanged(User user) {
        DataManager.getInstance().onTweetUserFollowStatusChanged(user);
        List<Tweet> affectedTweets = mPagerAdapter.getTweetsWithUser(user);
        for (Tweet affectedTweet: affectedTweets
                ) {
            boolean affectedTweetCurrentUserFollowStats = affectedTweet.isFollowingTweetUser();
            affectedTweet.setFollowingTweetUser(!affectedTweetCurrentUserFollowStats);
        }
        mPagerAdapter.notifyDataSetChanged();
    }

The next page containes the same user. 下一页包含同一用户。

So if I follow him on the curent page I want the follow button from the next page to display "UNFOLLOW" 因此,如果我在当前页面上关注他,我希望下一页上的关注按钮显示“ UNFOLLOW”

I have found the solution; 我找到了解决方案;

Original problem: 原始问题:

after updating the array in the FragmentStateAdapter I needed a way to update the UI of the already instantiated fragments in the Adapter. 在FragmentStateAdapter中更新数组之后,我需要一种方法来更新Adapter中已实例化的片段的UI。

So I after updating the array in the FragmentStateAdapter just call notifyDataSetChange() and add this code to your adapter. 因此,在更新FragmentStateAdapter中的数组之后,我只需调用notifyDataSetChange()并将此代码添加到您的适配器即可。

  mPagerAdapter.registerDataSetObserver(new DataSetObserver() {
            @Override
            public void onChanged() {
                super.onChanged();

                FragmentStatePagerAdapter fragmentPagerAdapter = (FragmentStatePagerAdapter) VP_tweets.getAdapter();
                for(int i = 0; i < fragmentPagerAdapter.getCount(); i++) {
                    TweetFragment viewPagerFragment = (TweetFragment) VP_tweets.getAdapter().instantiateItem(VP_tweets, i);
                    if(viewPagerFragment != null && viewPagerFragment.isAdded()) {
                        viewPagerFragment.updateUI();
                    }
                }
            }

        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android:ViewPager FragmentStatePagerAdapter获取当前视图 - Android: ViewPager FragmentStatePagerAdapter get current View Android ViewPager和FragmentStatePagerAdapter保存视觉元素值 - Android ViewPager and FragmentStatePagerAdapter save visual elements values 添加/删除页面时,Android FragmentStatePagerAdapter无法正确更新ViewPager - Android FragmentStatePagerAdapter not updating ViewPager correctly when adding/removing pages Android-使用ViewPager和FragmentStatePagerAdapter为ActionBar创建多级选项卡 - Android - Creating Multiple Levels of Tabs for ActionBar Using ViewPager and FragmentStatePagerAdapter ViewPager,FragmentStatePagerAdapter和无限滚动 - ViewPager, FragmentStatePagerAdapter and Infinite Scrolling 使用FragmentStatePagerAdapter的ViewPager滞后 - ViewPager Lag using FragmentStatePagerAdapter ViewPager + FragmentStatePagerAdapter未按预期工作 - ViewPager + FragmentStatePagerAdapter not working as intended 重新启动ViewPager + FragmentStatePagerAdapter到项目0 - Restarting ViewPager + FragmentStatePagerAdapter to item 0 ViewPager + FragmentStatePagerAdapter +方向更改 - ViewPager + FragmentStatePagerAdapter + orientation change 无法在ViewPager中设置FragmentStatePagerAdapter - unable to set the FragmentStatePagerAdapter in ViewPager
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM