Refresh Fragment in ViewPager From getItemPosition

I have a screen that has a new TabLayout from the design support library. I also have a ViewPager that is used to display a fragment. This snippet displays different view lists depending on the selected tab.

The app receives a network call response, my activity updates the FragmentPagerAdapter owned by the ViewPager with the data returned by the response, in my case it's 3 lists.

In my ViewPager, I override onPageChangeListener()

to detect when the page was selected and then I call my FragmentPagerAdapter with a new tab. I also call notifyDataSet()

to redraw the view.

I am overriding the method getItemPosition()

to determine which list should be passed to the fragment. However, I don't think this is the correct way to do it, since a lot of their interception comes from getItemPosition

. The method actually updates each of the snippets with the same list of results. When scrolling to the next tab, the current list is displayed in the next tab until the scroll is released and then the slice is redrawn with the correct list.

My question is, am I properly approaching displaying lists in my ViewPager fragments? Or am I seriously overdoing it? I don't have much experience with ViewPagers or FragmentPagerAdapters, so any input would be appreciated.

My code looks like this.

Adding a snippet (called from onResume()

)

private void addFragment() {

    mViewPager = (ViewPager) findViewById(R.id.viewpager);

    mMyPagerAdapter = new MyPagerAdapter(getSupportFragmentManager(),
            SampleActivity.this);

    mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(final int i, final float v, final int i2) {
        }
        @Override
        public void onPageSelected(final int i) {
            mMyPagerAdapter.updateTabPosition(i);
        }
        @Override
        public void onPageScrollStateChanged(final int i) {
        }
    });

    mViewPager.setAdapter(mMyPagerAdapter);
    mViewPager.setOffscreenPageLimit(1);

    mTabLayout = (TabLayout) findViewById(R.id.my_tab_layout);
    mTabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
    mTabLayout.setTabMode(TabLayout.MODE_FIXED);
    mTabLayout.setupWithViewPager(mViewPager);

    if (shouldMakeNetworkCall()) {
        getLists();
    }
}

      

ViewPager FragmentPagerAdapter Class

public class MyFragment extends FragmentPagerAdapter {

private Context mContext;
final int PAGE_COUNT = 3;
private String tabTitles[] = new String[] { "One", "Two", "Three" };
private List<MyListType> mListOne;
private List<MyListType> mListTwo;
private List<MyListType> mListThree;
private int mTabPosition;

public MyPagerAdapter(FragmentManager fm, Context context) {
    super(fm);
    mContext = context;
}

@Override
public Fragment getItem(int position) {
    switch (position) {
        case 0:
            return MyFragment.newInstance(mListOne);
        case 1:
            return MyFragment.newInstance(mListTwo);
        case 2:
            return MyFragment.newInstance(mListThree);
        default:
            return null;
    }
}

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

@Override
public CharSequence getPageTitle(int position) {
    // Generate title based on item position
    return tabTitles[position];
}

@Override
public int getItemPosition(Object object) {
    if (object instanceof MyFragment) {

        switch (mTabPosition) {
            case 0:
                ((MyFragment) object).setupUI(mListOne);
                break;
            case 1:
                ((MyFragment) object).setupUI(mListTwo);
                break;
            case 2:
                ((MyFragment) object).setupUI(mListThree);
                break;
            default:
                break;
        }
    }
    return super.getItemPosition(object);
}


public void updateTabPosition(int tabPosition) {
    mTabPosition = tabPosition;
    notifyDataSetChanged();
}
}

      

+3


source to share


1 answer


In fact, I think you are complicating it.

For me, the best solution is to have 3 network calls, each for a chunk in ViewPager

. Let the fragments manage their calls on their own, as you would do on a single fragment Activity

. This way, you can even show and hide progress on each chunk if you want to use the appropriate UX. I recommend using Switcher for this



The question is, can you make individual calls? Bacause if you can't, it will make the same big call 3 times, which is not the smartest idea. In this second case, you can start each snippet with a progress scrolling and let the activity make the call. For me, the preferred solution for communicating with fragments ViewPager

would be using events . You just make a call in an activity and after loading it you split the data accordingly and send 3 events with the data inside it and let them react to the fragment. Just register with EventBus inside onAttach

and unregister in onDetach

methods

+3


source







All Articles