1

Can any one please explain how to make endless adapter concept for view pager

I am currently using view pager to see my datas. On every 10th swipe of the view pager I need to hit the server and take dynamic response and need to update the viewpager. Obviously we need to use the endless adapter concept. But I was confused with the exact concept. Anyone please do the needful...

Thanks in advance...

Devi Indra
  • 23
  • 3

2 Answers2

0

Well, let's start from the beginning.

If you would like to have 'endless' number of pages you need to use some trick. E.g. you can't store endless number of pages in memory. Probably Android will destroy PageView everytime, when it isn't visible. To avoid destroying and recreating those views all the time you can consider recycling mechanism, which are used e.g. ListView. Here you can check and analyse idea how to implement recycling mechanism for pager adapter.

Moreover to make your UI fluid, try to make request and download new data before user gets to X0th page (10, 20, 30, 40...). You can start downloading data e.g when user is at X5th (5, 15, 25...) page. Store data from requests to model (it could be e.g. sqlite db), and user proper data based on page number.

It's just a brief of solution, but it's interesting problem to solve as well;)

Edit

I've started looking for inspiration and just found standalone view recycler implemented by Jake Wharton and called Salvage. Maybe it will be good start to create solution for your problem.

Community
  • 1
  • 1
Krzysztof Skrzynecki
  • 2,345
  • 27
  • 39
0

I’ve implemented an endless ViewPager. I think it suits you needs. The request is simulated with a time delay ​​in the AsyncTask thread.

//ViewPagerActivity

public class ViewPagerActivity extends FragmentActivity {

    private ViewPager vp_endless;

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

        vp_endless = (ViewPager) findViewById(R.id.vp_endless);
        vp_endless.setAdapter(new FragmentViewPagerAdapter(getSupportFragmentManager()));
    }

}

//FragmentViewPagerAdapter

public class FragmentViewPagerAdapter extends FragmentStatePagerAdapter {

    private List<CustomObject> _customObjects;
    private volatile boolean isRequesting;
    private static final int ITEMS_PER_REQUEST = 10;


    public FragmentViewPagerAdapter(FragmentManager fragmentManager) {
        super(fragmentManager);
        _customObjects = HandlerCustomObject.INSTANCE._customObjects;
    }

    @Override
    public Fragment getItem(int position) {
        CustomFragment fragment = new CustomFragment();
        fragment.setPositionInViewPager(position);

        if (position == _customObjects.size() && !isRequesting)
            new AsyncRequestItems().execute("www.test.com");

        return fragment;
    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

    public class AsyncRequestItems extends AsyncTask<String, Void, Void> {

        @Override
        protected Void doInBackground(String... urls) {
            isRequesting = true;

            //Fake request lag
            try {Thread.sleep(2500);}
            catch (InterruptedException e) {e.printStackTrace();}

            for (int i = 0; i < ITEMS_PER_REQUEST; i++) {
                _customObjects.add(new CustomObject());
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            isRequesting = false;
        }

    }

}

//CustomFragment

public class CustomFragment extends Fragment {

    private CustomObject _customObject;
    private TextView tv_position;
    private ProgressBar pb_loading;
    private View root;
    private int _positionInViewPager;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        root = inflater.inflate(R.layout.frament_endless_view_pager, container, false);
        pb_loading = (ProgressBar) root.findViewById(R.id.pb_loading);
        tv_position = (TextView) root.findViewById(R.id.tv_position);

        _customObject = retrieveDataSafety();
        if(_customObject != null) bindData();
        else createCountDownToListenerForUpdates();

        return root;
    }

    public void createCountDownToListenerForUpdates() {
        new CountDownTimer(10000, 250) {

            public void onTick(long millisUntilFinished) {
                _customObject = retrieveDataSafety();
                if(_customObject != null) {
                    bindData();
                    cancel();
                }
            }

            public void onFinish() {}

        }.start();
    }

    private CustomObject retrieveDataSafety() {
        List<CustomObject> customObjects = HandlerCustomObject.INSTANCE._customObjects;
        if(customObjects.size() > _positionInViewPager)
            return customObjects.get(_positionInViewPager);
        else
            return null;
    }

    private void bindData() {
        pb_loading.setVisibility(View.GONE);
        String feedback = "Position: " + _positionInViewPager;
        feedback += System.getProperty("line.separator");
        feedback += "Created At: " + _customObject._createdAt;
        tv_position.setText(feedback);
    }

    public void setPositionInViewPager(int positionAtViewPager) {
        _positionInViewPager = positionAtViewPager;
    }

}

//CustomObject

public class CustomObject {
    public String _createdAt;

    public CustomObject() {
        DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
        _createdAt = dateFormat.format(new Date());
    }
}

//HandlerCustomObject

public enum  HandlerCustomObject {
    INSTANCE;

    public List<CustomObject> _customObjects = new ArrayList<CustomObject>();

}
Víctor Albertos
  • 8,093
  • 5
  • 43
  • 71
  • Thanks for the suggestion I have worked it, the view pager working fine, but my doubt is at which point I need to hit the server again. As like EndlessAdapter, dont we have cacheInBackground and appendCachedData functions for this adapter. So that we can hit the sever for the next 10 records and update the view pager list – Devi Indra Sep 09 '14 at 07:26