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

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

问题描述

在 Android 中使用 Viewpager,是否可以以编程方式将任何页面移动到末尾?例如,我有 5 页,想将第 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?

谢谢

推荐答案

是的.我将尝试通过展示我是如何做到的来回答您的案例,并在我的代码示例下方展示您案例的示例.

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) 中删除一个项目并在您的 MyFragmentPagerAdapter 实例上调用 notifyDataSetChanged(),它将从 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 的末尾.为此,您必须将 item#2 定位到数据源的末尾(通过 Collection.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(..)它的页面.该方法调用的结果决定了页面(本例中为 Fragment)是否以及在何处显示.

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天全站免登陆