简体   繁体   中英

FragmentPagerAdapter - Inserting Fragment at beginning doesn't refresh

I am tasked with creating a date slider, which initially shows the dates of the current week, starting from Sunday and going to Saturday. When the user swipes either way, it needs to "snap" to the next/previous week.

目标设计

My problem is that I can't get the Adapter to update when the user scrolls from left-to-right, which should show a previous week. The backing data is updated to show a new fragment, but the view doesn't seem to update. Scrolling from right-to-left to show weeks into the future works perfectly fine.

So down to the basics, my issue is really getting a FragmentPagerAdapter to refresh to be able to view dynamically added fragments that may be appended to the beginning of the backing data list.

While I am using Xamarin, I don't really care if the solution is in Xamarin or not. If somebody has this working in Java, I can just convert the code. So please don't limit the answers to just Xamarin Android.

For the design, I am using a ViewPager since it handles the "snapping" automatically. I originally tried using a RecyclerView, but couldn't get the snapping to work reliably. So I decided to use the ViewPager and a FragmentPagerAdapter.

For the ViewPager's child fragments, I am using a WeekFragment that creates 7 child fragments for each day( DayFragment ). The child fragments are resized to make sure 7 show for the week.

Initially, I create WeekFragment s for 3 weeks: the current week, one previous week, and one future week. These are stored in a generic list in the FragmentPagerAdapter .

I am also handling the OnPageSelected listener event of the ViewPager . When the user swipes to go to a new page, I check to see if that page is either the last page or the first page. Since scrolling to the last page works fine, this is the code for swiping to the first page...

    public void OnPageSelected(int position)
    {
        var isAtBeginning = position == 0;
        if (isAtBeginning)
        {
            // we are at the beginning of the date list. Append another week to beginning
            _pagerAdapter.AddWeekToBeginning();
        }
    }

The AddWeekToBeginning method is ..

    public void AddWeekToBeginning()
    {
        _earliestDate = _earliestDate.AddDays(-DaysInWeek);
        var frag = new WeekFragment(_earliestDate);
        frag.SelectedDate = this.SelectedDate;
        frag.DateSelected += DaySelected;
        _fragments.Insert(0, frag);

        NotifyDataSetChanged();
    }

I have also overridden the two main FragmentPagerAdapter methods/properties: GetItems and Count

    public override int Count
    {
        get
        {
            Console.WriteLine($"Fragment Count: {_fragments.Count}");
            return _fragments.Count;
        }
    }

    public override V4.Fragment GetItem(int position)
    {
        return _fragments[position];
    }

Again, the backing list does get updated. After the first left-to-right swipe(going to the past), the console shows that there are now 4 fragments in the list. However, the ViewPager doesn't let me swipe left-to-right to see the new first fragment.

I've done tons of searching on Google and I haven't been able to find anything useful. I'm hoping I can get some help here. I have this about 95% completed, I just can't figure this last part out. Thanks in advance.

Use FragmentStatePagerAdapter

Update your Fragment data in:

 public class YourFragment extends Fragment {
    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            //Update Here

        } else {
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM