更新viewpager与片段新秩序 [英] updating viewpager with fragments in a new order

查看:124
本文介绍了更新viewpager与片段新秩序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ViewPager设置所绘制的数据从服务器传下来的数据它的页面(视图)。有时,服务器将发下来,这将重新排序的新数据视图(有时加新的观点),我需要让我的Andr​​oid设备上无缝地发生在用户正在观看一个特定的片段。

我有它设置为来电notifyDataSetChanged()当新的数据被从服务器拉下来,但似乎保持幻灯片的缓存版本到当前观看幻灯片的左边和右边这潜在地将与改变重新排序。我已经看过这个话题在这里:<一href=\"http://stackoverflow.com/questions/7263291/viewpager-pageradapter-not-updating-the-view/\">ViewPager PagerAdapter更新不及时的视图和实施工作正常,除了它重新加载正在查看这不会为我工作的目的当前幻灯片的首选解决方案。我参加了一个浅潜水考虑实施第二个答案提出同一页上的setTag()方法,将工作更新幻灯片上的信息,但它不会帮助我重新排序它们。

是有一些方法可以让我重新安排所有幕后的幻灯片W / O在当前查看的幻灯片?

造成任何打嗝

TIA

编辑:加code和要求一些进一步的澄清

这是我的适配器类。

 私有类ScreenSlidePagerAdapter扩展FragmentPagerAdapter {
    ContestEntriesModel条目;    公共ScreenSlidePagerAdapter(FragmentManager FM,ContestEntriesModel项){
        超(FM);
        this.entries =条目;
    }    @覆盖
    众长getItemId(INT位置){
        返回entries.entries [位置] .ContestEntryId;    }    @覆盖
    公众诠释getItemPosition(Object对象){
        android.support.v4.app.Fragment F =(android.support.v4.app.Fragment)对象;
        的for(int i = 0; I&LT; getCount将();我++){            android.support.v4.app.Fragment片段=的getItem(ⅰ);
            如果(f.equals(片段)){
                返回我;
            }
        }
        返回POSITION_NONE;
    }
    @覆盖
    公共android.support.v4.app.Fragment的getItem(INT位置){        长ENTRYID = getItemId(位置);
        //Log.d(\"EntryIds\",Integer.toString(entryId));
        如果(mItems.get(ENTRYID)!= NULL){
            返回mItems.get(ENTRYID);
        }
        片f = ScreenSlidePageFragment.create(位置);
        mItems.put(ENTRYID,F);
        返回F;
    }    @覆盖
    公众诠释的getCount(){
        返回NUM_PAGES;
    }
}

这是我使用的是什么,当一个新的有效载荷来自服务器下来:

  @覆盖
    保护无效onPostExecute(ContestEntriesModel项){
        片段currentFragment = ContestEntries.mItems.get(ContestEntries.CurrentEntryId);
        Log.d(fromUpdate,Long.toString(ContestEntries.CurrentEntryId));
        ContestEntries.Entries =条目;
        ContestEntries.NUM_PAGES = ContestEntries.Entries.entries.length;
        ContestEntries.mPagerAdapter.notifyDataSetChanged();
        ContestEntries.mPager.setCurrentItem(ContestEntries.mPagerAdapter.getItemPosition(currentFragment));
    }


解决方案

检查<一href=\"http://stackoverflow.com/questions/12321617/android-viewpager-move-any-page-to-end-programatically/14822077#14822077\">my回答这里。关键是要同时覆盖 getItemId(INT) getItemPosition(对象)在您的适配器。

getItemId(INT)用于您的数据集内唯一识别您的网页。 getItemPosition(对象)用来获取数据集中为这个独特的页面(更新)位置。

如果你想保持焦点在同一页上之前和更新(添加/删除页面),你应该叫后 viewpager.setCurrentItem(INT)新位置你的页面调用后 .notifyDataSetChanged()您的适配器。

I have a ViewPager set up that is drawing the data for its pages (views) from data passed down from a server. On occasion, the server will send down new data that will re-order the views (and sometimes add new views) and I need to make that happen seamlessly on my android device while the user is currently viewing a particular fragment.

I have it set to call notifyDataSetChanged() when the new data is pulled down from the server, but that seems to keep a cached version of the slides to the left and right of the currently viewed slide which potentially will change with the reorder. I have looked at this topic here: ViewPager PagerAdapter not updating the View and implemented the first solution which works fine, other than it reloads the current slide that is being viewed which won't work for my purposes. I took a shallow-dive look into implementing the setTag() method proposed on that same page in the second answer and that would work for updating information on the slides, but it won't help me for reordering them.

is there some way I can reorder all of the slides behind the scenes w/o causing any burps in the currently viewed slide?

TIA

EDIT: adding code and asking for some further clarification

This is my Adapter class.

    private class ScreenSlidePagerAdapter extends FragmentPagerAdapter {
    ContestEntriesModel entries;

    public ScreenSlidePagerAdapter(FragmentManager fm, ContestEntriesModel Entries) {
        super(fm);
        this.entries = Entries;
    }

    @Override
    public long getItemId(int position) {
        return entries.entries[position].ContestEntryId;

    }

    @Override
    public int getItemPosition(Object object) {
        android.support.v4.app.Fragment f = (android.support.v4.app.Fragment)object;
        for(int i = 0; i < getCount(); i++){

            android.support.v4.app.Fragment fragment = getItem(i);
            if(f.equals(fragment)){
                return i;
            }
        }
        return POSITION_NONE;
    }


    @Override
    public android.support.v4.app.Fragment getItem(int position) {

        long entryId = getItemId(position);
        //Log.d("EntryIds",Integer.toString(entryId));
        if(mItems.get(entryId) != null) {
            return mItems.get(entryId);
        }
        Fragment f = ScreenSlidePageFragment.create(position);
        mItems.put(entryId, f);
        return f;
    }

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

And here is what I'm using when a new payload comes down from the server:

    @Override
    protected void onPostExecute(ContestEntriesModel entries) {
        Fragment currentFragment = ContestEntries.mItems.get(ContestEntries.CurrentEntryId);
        Log.d("fromUpdate",Long.toString(ContestEntries.CurrentEntryId));
        ContestEntries.Entries = entries;
        ContestEntries.NUM_PAGES = ContestEntries.Entries.entries.length;
        ContestEntries.mPagerAdapter.notifyDataSetChanged();
        ContestEntries.mPager.setCurrentItem(ContestEntries.mPagerAdapter.getItemPosition(currentFragment));
    }

解决方案

Check my answer here. The key is to override both getItemId(int) and getItemPosition(Object) in your adapter.

getItemId(int) is used to identify your pages uniquely within your dataset. getItemPosition(Object) is used to fetch the (updated) position for this unique page in your dataset.

If you want to keep focus on the same page before and after updating (adding/removing pages), you should call viewpager.setCurrentItem(int) for the new position of your page after calling .notifyDataSetChanged() on your adapter.

这篇关于更新viewpager与片段新秩序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆