Android的ViewPager:移动任何页面以编程方式结束? [英] Android ViewPager: Move any page to end programatically?

查看:263
本文介绍了Android的ViewPager:移动任何页面以编程方式结束?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Viewpager Android中,是否有可能以编程方式移动任何网页到最后?例如,我有五页,并希望第2移动到最后的位置(第5位),所以第3页变为2,4变为3,5变为4?

Using the Viewpager in Android, is it possible to programmatically move any page to the end? For example, I have five pages and want to move page #2 to the last position (5th position) so page 3 becomes 2, 4 becomes 3, and 5 becomes 4?

感谢

推荐答案

是的。我会试着通过展示我是如何做到的回答你的情况,我会告诉你的情况我下面的code样品的例子。

Yes. I'll try and answer your case by showing how I did it, and I'll show an example for your case below my code sample.

基本上,要做到这一点,你必须跟踪哪个位置,你的片段具有。下面是我用于执行我的 FragmentPagerAdapter ,它使用一个ArrayList作为数据源。

Basically, to accomplish this you have to keep track which position your Fragments have. Below is the implementation I used for my FragmentPagerAdapter, which uses an ArrayList as its data source.

public class MyFragmentPagerAdapter extends FragmentPagerAdapter {

    private HashMap<Long, Fragment> mItems
                    = new HashMap<Long, Fragment>();

    private ArrayList<MyObject> dataset;

    public MyFragmentPagerAdapter(ArrayList<MyObject> objects) {
        this.dataset = objects;
    }

    @Override
    public int getCount() {
        return dataset.size();
    }

    @Override
    public Fragment getItem(int position) {
        long id = getItemId(position);

        if(mItems.get(id) != null) {
            // caching to prevent multiple instances of the same fragment
            // for the same position/id
            return mItems.get(id);
        }

        Fragment f = Fragment.newInstance();

        mItems.put(id, f);

        return f;
    }

    @Override
    public long getItemId(int position) {
        // return a unique id
        return dataset.get(position).getUniqueId();
    }

    @Override
    public int getItemPosition(Object object) {
        /*
         * Purpose of this method is to check whether an item in the adapter
         * still exists in the dataset and where it should show.
         * For each entry in dataset, request its Fragment.
         * 
         * If the Fragment is found, return its (new) position. There's
         * no need to return POSITION_UNCHANGED; ViewPager handles it.
         * 
         * If the Fragment passed to this method is not found, remove all
         * references and let the ViewPager remove it from display by
         * by returning POSITION_NONE;
         */
        Fragment f = (Fragment) object;

        for(int i = 0; i < getCount(); i++) {

            Fragment item = (Fragment) getItem(i);
            if(item.equals(f)) {
                // item still exists in dataset; return position
                return i;
            }
        }

        // if we arrive here, the data-item for which the Fragment was created
        // does not exist anymore.

        // Also, cleanup: remove reference to Fragment from mItems
        for(Map.Entry<Long, MainListFragment> entry : mItems.entrySet()) {
            if(entry.getValue().equals(f)) {
                mItems.remove(entry.getKey());
                break;
            }
        }

        // Let ViewPager remove the Fragment by returning POSITION_NONE.
        return POSITION_NONE;
    }
}

现在如果从数据集删除项目( this.dataset ),并调用 notifyDataSetChanged() MyFragmentPagerAdapter 的情况下,它将从ViewPager(即使是当前正在查看的话)删除该项目。

Now if you remove an item from the dataset (this.dataset) and call notifyDataSetChanged() on your instance of MyFragmentPagerAdapter, it will remove the item from the ViewPager (even if it's currently being viewed).

比方说, this.dataset 包含5个项目,要移动#2到ViewPager的结束。要做到这一点,你就会有第2项定位到您的数据源(通过 Col​​lection.Sort 或其他方式)的结束。我就告诉你最简单的方式。

Let's say this.dataset contains 5 items, and you want to move #2 to the end of the ViewPager. To accomplish this, you'll have to position item#2 to the end of your datasource (either via Collection.Sort or some other way). I'll just show you the easy way.

ArrayList<MyObject> list = new ArrayList<>();
MyFragmentPagerAdapter adapter = new MyFragmentPagerAdapter(list);
viewpager.setAdapter(adapter);

...

MyObject item = list.get(2);
list.remove(item);
list.add(item); // now it's positioned at the end of the list
adapter.notifyDataSetChanged(); // voilá, that should do the trick!

adapter.notifyDataSetChanged()最终调用 viewpager.dataSetChanged(),这反过来又调用 adapter.getItemPosition(..)它的每个页面。该方法调用的结果决定是否以及所在的页面(片段在这种情况下)会显示出来。

adapter.notifyDataSetChanged() eventually calls viewpager.dataSetChanged(), which in turn calls adapter.getItemPosition(..) on each of its pages. The result of that method call determines if and where the page (Fragment in this case) will show up.

这篇关于Android的ViewPager:移动任何页面以编程方式结束?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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