简体   繁体   English

Android我如何有一个ViewPager,其中页数由ASyncTask确定

[英]Android How can I have a ViewPager where the number of pages is determined by ASyncTask

I have an activity which has a ViewPager on it. 我有一个活动,上面有一个ViewPager。 I would like to have one page in the pager for every item returned from a server call using an AsyncTask. 我想在寻呼机中为使用AsyncTask从服务器调用返回的每个项目提供一页。

My server call is a paginated call, so the first time i call the server i ask for items 1 through 10. I then want my ViewPager to have a page for each of these 10 items and i can swipe left and right through the pages. 我的服务器呼叫是分页呼叫,因此,第一次呼叫服务器时,我会询问项目1到10。然后,我希望ViewPager拥有这10个项目中的每一个的页面,并且我可以在页面中左右滑动。 When i swipe to the 10th page, i want to call the server to get the next 10 pages (ie pages 11 through 20) so that on the 10th page i can swipe to get to page 11. 当我滑动到第10页时,我想调用服务器以获取下一个10页(即第11到20页),以便在第10页上可以滑动到第11页。

I see a lot of examples online for how i can use an AsyncTask to get the contents of a single " ViewPager page's fragment ", but this is not what i want, I want the AsyncTask to determine the " number of pages in the pager " (by returning a list of data objects containing information to display on each page). 我在网上看到很多示例,这些示例说明了如何使用AsyncTask来获取单个“ ViewPager页面的片段 ”的内容,但这不是我想要的,我希望AsyncTask确定“ pager中的页面数 ” (通过返回包含要在每个页面上显示的信息的数据对象的列表)。

This is simple with lists and ListAdapters, but i can not work out how to do this with ViewPagers and PagerAdapters. 这对于列表和ListAdapters很简单,但是我不知道如何使用ViewPagers和PagerAdapters做到这一点。

Any help would be greatly appreciated. 任何帮助将不胜感激。

I appear to have got this working in a very basic state. 我似乎已经在一个非常基本的状态下进行了这项工作。

DataManager I have a DataManager which is responsible for getting the items from the server using a ASyncTask DataManager我有一个DataManager,它负责使用ASyncTask从服务器获取项目

  • it contains a list of the items returned from the server 它包含从服务器返回的项目的列表
  • it contains the ASyncTask that goes to the server to get the items, when the task completes the list of items is updated 它包含ASyncTask,当任务完成时,该ASyncTask会发送到服务器以获取项目
  • it contains a "getSize()" method to return the size of the list of items 它包含一个“ getSize()”方法以返回项目列表的大小
  • it has a "getItem(int position)" method which returns the item at the given position 它有一个“ getItem(int position)”方法,该方法返回给定位置的项目
  • it has an "isItemAvailble(int position)" method, if the item at the given index is not in the list of items, the task is called again to get the next batch of items 它具有“ isItemAvailble(int position)”方法,如果给定索引处的项目不在项目列表中,则再次调用该任务以获取下一批项目
  • it has a DataSetObservable so that the manager can inform any observers when its data changes (ie when a task has completed and its items updated) 它具有一个DataSetObservable,以便管理器可以在数据更改时(例如,任务完成并且其项目更新时)通知任何观察者。

PagerAdapter 页面适配器

The PagerAdapter is as follows PagerAdapter如下

class MyPageAdapter extends FragmentPagerAdapter {

    // Members
    ....
    private DataManager dataManager;

    // create an observer to listen for changes in the data manager
    private final DataSetObserver observer = new DataSetObserver() {
        @Override
        public void onChanged() {
            Toast.makeText(appCtx(), "MyPagerAdapter observer's onChanged called", Toast.LENGTH_LONG).show();
            // Notify observers on this adapter that item list has changed.
            notifyDataSetChanged();
        }

    };

    // constructor
    public MyPageAdapter(DataManager dataManager, .....) {
        super(fm);
        this.dataManager = dataManager;
        dataManager.registerDataSetObserver(observer);
    }

    /**
     * Get the number of items from the manager
     */
    private int getSize() {
        return dataManager.size();
    }

    // get the item for the given position
    @Override
    public Fragment getItem(int position) {
        // if the next item is not available the managers task will get the next batch
        dataManager.ensureItemAvailable(position+1);

        // get this position's item from the data manager and create a fragment using that item
        Object obj = dataManager.getItem();
        return MyPagerFragment.newInstance(obj);
    }
....
....
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Android的水平ViewPager中添加和删除多个页面? - How can i add and remove multiple pages in horizontal viewpager for android? Android ViewPager、Fragments 和 AsyncTask。 如何在方向更改时保留 AsyncTask 的状态? - Android ViewPager, Fragments and AsyncTask. How do i preserve the AsyncTask's state on orientation change? 如何删除Android ViewPager中的页面? - How to remove pages in Android ViewPager? Android ViewPager,Fragment和AsyncTask - Android ViewPager, Fragment, and AsyncTask 如何使用AsyncTask,我必须在哪里放置代码行? - How to use AsyncTask, where do I have to put the code lines? 如何在 Android 中将 Swingworker 更改为 AsyncTask? - How can I change Swingworker to AsyncTask in Android? 如何摇动ViewPager以向用户显示还有更多页面可用? - How can I shake ViewPager to show the user that there are more pages available? 如何使用 RecyclerView 打开 ViewPager 中的特定页面? - How can I use the RecyclerView to open specific pages in ViewPager? Android,我可以将AsyncTask放在一个单独的类中并进行回调吗? - Android, can I put AsyncTask in a separate class and have a callback? 如何在Android中使用动画调整ViewPager的大小 - How can I resize ViewPager with animation in Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM