Search code examples
androidandroid-fragmentsandroid-viewpagerxamarin.android

How to reload fragments in ViewPager Xamarin


I've huge data at database side and I'm retrieving data using a service in batch of 50 rows and putting that data in ViewPager fragments. It's working fine, but I need to re-call service once user reached at page 50 and need to update fragments with new data. Currently, I'm calling service in override newInstance(string message) method of Android.Support.V4.App.Fragment.

Is there any way to update fragments with new data dynamically in Xamarin Anadroid?


Solution

  • To update the specific viewpager fragment. I think you do not need to recreate the activity or recreate the fragment.

    Try to use the adapter.NotifyDataSetChanged() to update the fragment.

    follow the setps to update the sepcific viewpager fragment:

    1. Create a handler

        class MyHandler : Handler
            {
                public override void HandleMessage(Message msg)
                {
                    if (msg.What == 1)
                    {
                        mainAdapter.NotifyDataSetChanged();
                    }
                }
            }
    

    2. send message when you reach the 50th fragment

        class PageChangeListen : Java.Lang.Object, IOnPageChangeListener
            {
                public void OnPageScrolled(int position, float positionOffset, int positionOffsetPixels)
                {
    
                }
    
                public void OnPageScrollStateChanged(int state)
                {
    
                }
    
                public void OnPageSelected(int position)
                {
                    if (position == 1)
                    {
                        Message msg = new Message();
                        msg.What = 1;
                        new MyHandler().SendMessage(msg);
                    }
                }
            }
    

    3. in the main thread you will received the message and call NotifyDataSetChanged

    4. add the update interface for the fragment

        public interface Updateable
        {
             void update();
        }
    
    
        public class MyFragment : Android.Support.V4.App.Fragment, Updateable
        {
    
            TextView questionBox;
            public MyFragment() { }
    
            public static MyFragment NewInstance(String question)
            {
                MyFragment fragment = new MyFragment();
                return fragment;
            }
            public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
            {
                View view = inflater.Inflate(Resource.Layout.MyFragmentLayout, container, false);
                questionBox = (TextView)view.FindViewById(Resource.Id.textView1);
                return view;
            }
    
            public void update()
            {
                questionBox.Text = "Mike Update";
            }
      }
    

    5. fragment will update the UI by call GetItemPosition.

         public override int GetItemPosition(Object @object)
                {
                    MyFragment f = (MyFragment)@object;
                    System.Console.WriteLine(f.Id + "" + f.Resources + "" + f.Tag);
                    if (f != null)
                    {
                        f.update();
                    }
                    return base.GetItemPosition(@object);
                }
    

    In my example I have update the second page with the text "Mike Update". You can change the update condition and content.

    Screen shot:

    enter image description here

    The git source code is here