Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does PullToRefresh work with FragmentPagerAdapter?

Libraries:

  • https://github.com/chrisbanes/Android-PullToRefresh
  • https://github.com/JakeWharton/Android-ViewPagerIndicator

In the Sample PullToRefreshListInViewPagerActivity only works with PageAdapter.

I couldn't find a way to put it to work with FragmentPageAdapter.

Sample:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ptr_list_in_vp);

        mViewPager = (ViewPager) findViewById(R.id.vp_list);
        mViewPager.setAdapter(new ListViewPagerAdapter());
    }

    private class ListViewPagerAdapter extends PagerAdapter {

        @Override
        public View instantiateItem(ViewGroup container, int position) {
            Context context = container.getContext();

            PullToRefreshListView plv = (PullToRefreshListView) LayoutInflater.from(context).inflate(
                    R.layout.layout_listview_in_viewpager, container, false);

            ListAdapter adapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1,
                    Arrays.asList(STRINGS));
            plv.setAdapter(adapter);

            plv.setOnRefreshListener(PullToRefreshListInViewPagerActivity.this);

            // Now just add ListView to ViewPager and return it
            container.addView(plv, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

            return plv;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

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

    }

    @Override
    public void onRefresh(PullToRefreshBase<ListView> refreshView) {
        new GetDataTask(refreshView).execute();
    }

    private static class GetDataTask extends AsyncTask<Void, Void, Void> {

        PullToRefreshBase<?> mRefreshedView;

        public GetDataTask(PullToRefreshBase<?> refreshedView) {
            mRefreshedView = refreshedView;
        }

        @Override
        protected Void doInBackground(Void... params) {
            // Simulates a background job.
            try {
                Thread.sleep(4000);
            } catch (InterruptedException e) {
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            mRefreshedView.onRefreshComplete();
            super.onPostExecute(result);
        }
    }

}
like image 548
Marckaraujo Avatar asked Oct 21 '22 19:10

Marckaraujo


1 Answers

You can add a ListFragment to the FragmentPageAdapter (or FragmentStatePageAdapter) and add the Pull-To-Refresh feature on it.

The Android-PullToRefresh repository has an example about ListFragment.

The Android Developers website has a sample code on how to make FragmentPagerAdapter or FragmentStatePagerAdapter.

You can also take a look at my test project: https://github.com/caiguanhao/FSPAtest

like image 171
caiguanhao Avatar answered Oct 24 '22 10:10

caiguanhao