3

I have a FragmentPagerAdapter which creates it's child pages (which are ListFragments) as follows:

@Override
public Fragment getItem(int position) {
    return FragmentChildPage.newInstance();
}

I give the ViewPager an OffscreenPageLimit of 1. The child ListFragments have a custom ScrollListener.

The issue I have is the custom scroll listener only works on child ListFragments that are created when the ViewPager is swiped past its OffscreenPageLimit, i.e. child pages > 2.

More confusing is if I scroll several pages and then return to the first page the scroll listener now works.

What is going on here? It seems the ViewPager interferes with some of the child pages OnScrollListener.

Thanks

Milo
  • 1,017
  • 3
  • 16
  • 22

1 Answers1

5

Try this out. Works for me!

public class ActivityTest extends SherlockFragmentActivity {

    private ViewPager mViewPager;

    private TestAdapter mAdapter;

    @Override
    protected void onCreate(Bundle arg0) {
        super.onCreate(arg0);

        mViewPager = new ViewPager(this);
        mViewPager.setId(android.R.id.button1);
        setContentView(mViewPager);

        mAdapter = new TestAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(mAdapter);
    }

    private class TestAdapter extends FragmentPagerAdapter {

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

        @Override
        public Fragment getItem(int position) {
            return TestListFragment.newInstance(position);
        }

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

    }

    private static class TestListFragment extends SherlockListFragment {

        private static final String TAG = TestListFragment.class.getSimpleName();

        private String[] mItems = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9","0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };

        private int mPos;

        @SuppressLint("ValidFragment")
        public static TestListFragment newInstance(int pos) {
            TestListFragment frag = new TestListFragment();
            Bundle b = new Bundle();
            b.putInt("pos", pos);
            frag.setArguments(b);
            return frag;
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            mPos = getArguments().getInt("pos");
        }

        @Override
        public void onViewCreated(View view, Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);

            setListAdapter(new ArrayAdapter<String>(getSherlockActivity(), android.R.layout.simple_list_item_1, mItems));

            getListView().setOnScrollListener(new OnScrollListener() {              
                @Override
                public void onScrollStateChanged(AbsListView view, int scrollState) {

                }

                @Override
                public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                    Log.d(TAG, "onScroll() - Fragment: " + mPos);
                }
            });
        }
    }

}
Mimminito
  • 2,803
  • 3
  • 21
  • 27
  • This example works fine for me and doesn't look to dissimilar to my implementation. I'll therefore have to go through and remove parts of code until I find out what's breaking it. Thanks. – Milo Mar 07 '13 at 12:06
  • @Milo Did you ever find what exactly the problem was? – brk3 May 12 '13 at 18:00