setCurrentItem在ViewPager没有在正确的位置立即滚动 [英] setCurrentItem in ViewPager not scroll immediately in the correct position

查看:1643
本文介绍了setCurrentItem在ViewPager没有在正确的位置立即滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用的PagerAdapter。我有很多的元素,所以我不加载在同一时间的所有元素。我重装适配器,我换了新的元素的旧元素。例如,我有4个元素,当我在最后一个元素到达时,我重装元素和我打电话setCurentItem(1)能够展现viewpager在一个位置。我已经做了很多的测试。我把:

I use a PagerAdapter. I have a lot of elements so I don't load all elements in the same time. I reload the adapter, I change the old elements by the new elements. For example I have 4 elements, when I arrive at the last element, I reload the elements and I call setCurentItem(1) to be able to show the viewpager in position one. I have done a lot of tests. I put:

viewPager.setCurrentItem(1,false); //But not scroll, put drastically the new page.
viewPager.setCurrentItem(1);

我试图修改onPageScrollStateChanged(INT为arg0),我只有3次,并始终ubicate在中间。

I have tried to modify onPageScrollStateChanged(int arg0), I only have 3 views and always ubicate in the middle.

if (arg0 == ViewPager.SCROLL_STATE_DRAGGING) {
    if(focusedPage==2) {
        //Modify adapter
        viewPager.setCurrentItem(1,false);   

    }else if(focusedPage==0){
        //Modify adapter
        viewPager.setCurrentItem(1,false);
    } 

和我已经tryed修改使用onPageSelected(INT为arg0)

And I have tryed to modify onPageSelected(int arg0)

focusedPage=arg0;

使用这个最后的例子我马上滚动的看法,但它始终位于最后一个页面上。
任何人都知道我怎么可以滑动。

Using this last example I have scroll the views immediately but always it located on the last page. Anybody know how I can slide.

感谢您

推荐答案

我可以更多地了解你所要完成什么背景?

Can I know more about the background of what you are trying to accomplish?

总之,从我能理解你正在尝试做一个无限 ViewPager 从最后一个到第一个,反之亦然打算(请纠正我,如果我我错了)。你希望你的 ViewPager 进入到第一页,但滚动 - 不只是从无处出现

Anyway, from what I can understand you are trying to make an "infinite" ViewPager going from the last one to the first and vice versa (please correct me if I am wrong). And you want your ViewPager to go to the first page but scrolling - not just appearing from nowhere.

好吧,如果你禁用平滑滚动(第二个参数),那么它会直接进入你想,没有滚动动作页面(而不会变的道路上其他网页),如果你启用平滑滚动,然后它会经过各方面的意见,所以你会看到它是怎么不能下单,但第一个。

Well, if you disable the smooth scroll (the second parameter), then it will go directly to the page you want with no scroll motion (and will not "change" to the other pages on the way), and if you enable the smooth scroll then it will go through all the views, so you will see how it was not the next one but the first one.

    /*No swipe animation, and no onPageChanged for the page in the middle*/
    mPager.setCurrentItem(1, false); 

    /*The same as if you do not add the second parameter*/
    mPager.setCurrentItem(1, true);

    /*Your ViewPager scrolls through all the needed views until it arrives at that view.*/
    mPager.setCurrentItem(1);

我可以推荐给你考虑到这一点的是,你可以尝试添加在位置0哑视图和最后,并在位置0的观点是相同的位置,最后 - 1和最后的看法是一样的位置1。所以,你会做的OnPageChangeListener一切正常,只是你会检查当前页面的位置0或最后,如果是,你会调用setCurrentItem(1,false),还是setCurrentItem(最后 - 这取决于你现在所在的哪个位置1,假)。通过这种方式,你将保持页面变的效果,同时实现你想要的东西。

What I can recommend to you with this in mind is that you can try to add "dummy" view at positions 0 and "last" and the view in position 0 would be the same as position "last - 1" and the "last" view would be the same as position "1". So you will do everything normal, EXCEPT that on the OnPageChangeListener you will check if the current page is the position "0" or "last" and if it is, you will call setCurrentItem("1", false) or setCurrentItem("last - 1", false) depending on which position you are at now. In this way you will keep the "page change" effect while accomplishing what you want.

我希望这可以帮助你解决问题。

I hope this can help you with your problem.

注意:
它可能有一个laggy改变你在新的位置后抵达,因为它必须加载当前,一个和最后一个视图的视图(或者,如果你是4.0+那么它将只加载的实际位置)。

NOTE: It may have a laggy change after you arrive at the new position, because it will have to load the view for the current, next and last views (or if you are on 4.0+ then it will load only the actual position).

更新:

如果您更改列表/阵列的决定内容的内容 ViewPager ,你需要调用:

If you change the content of the List/Array that determines the content of the ViewPager, you need to call:

ViewPager mPager = new ViewPager();
mPager.getAdapter().notifyDataSetChanged();  //This notify that you need to recreate the views

/*This is if you want to change the data and then go to a specific position
If you do not do this you will have a very Buggy Behavior*/
new Handler().post(new Runnable() {
@Override
public void run() {
    mPager.setCurrentItem(2); //Where "2" is the position you want to go
    }
});

更新2:

为什么你不加你所有的元素到 ViewPager ?在 ViewPager 已经优化处理的情况下,在我来说,我有很多它的元素,它是工作就像一个魅力。

Why do you not add all your elements to the ViewPager? the ViewPager already has optimization for handling those cases, in my case I have a lot of elements in it and it is working like a charm.

另一件事,在你的code我看你换到中间页每次页面定居。但你真的从来没有表现出左,右视图,因此你为什么让他们?

Another thing, in your code I see you change to the middle page each time a page is "settling". But then you really never show the right and left view, so why do you have them?

最后一件事,当你改变你的数据,你需要调用我上面提到的notifyDataSetChanged的方法,然后调用的处理程序里面的setCurrentItem的方法。如果你不这样做,如预期您的应用程序将无法正常工作。

Last thing, when you change your dataset you need to call the "notifyDataSetChanged" method that I mentioned above, and then you call the "setCurrentItem" method inside a handler. If you don't, your app will not work as expected.

更新3:

好吧,我可以推荐的唯一的事情是有,你继续增加,你从数据库中带来的,然后只要你保持插入到列表的末尾对象是没有问题的新对象名单。如果你想在中间或再开始添加对象,你需要调用notifyDataSetChanged但这将是一种缓慢的,因为它会再次产生意见。我们这样做是这样的,它完美的作品,我们甚至从服务器上下载的信息,数据是不是本地(而实际上我们添加信息,以结束和列表的开头)。

Well, the only thing I can recommend is having a List where you keep adding the new objects that you bring from the database, and then as long as you keep inserting objects to the end of the List there is no problem. If you want to add objects in the middle or at the beginning then you need to call the notifyDataSetChanged but that is going to be kind of slow because it will generate the views again. We do it this way and it works perfectly and we even download the info from a server, the data is not local (And actually we add info to the end and beginning of the List).

如果您有关于如何实现这一点不要犹豫,问任何其他问题。

If you have any other questions about how to achieve this do not hesitate to ask.

更新4:

万一有人实际上是在寻找一个圆形 ViewPager ,我添加了一个<一个href=\"http://stackoverflow.com/questions/11622544/fix-the-animation-of-a-circular-viewpager/11691347#11691347\">proof理念code ,你可以重新使用。它的工作原理没有问题,我曾与复杂视图中使用它,仍然没有任何滞后

Just in case anyone is actually looking for a circular ViewPager, I added a proof of concept code that you can re-use. It works without problem and I have used it with complex views and there is still no lag whatsoever.

这篇关于setCurrentItem在ViewPager没有在正确的位置立即滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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