Android PagerAdapter 起始位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11879315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:03:59  来源:igfitidea点击:

PagerAdapter start position

androidandroid-viewpagerswipe

提问by Kirill Kulakov

I'm using the following example to impliment my viewPager: http://code.google.com/p/viewpagerexample/issues/list

我正在使用以下示例来实现我的 viewPager:http: //code.google.com/p/viewpagerexample/issues/list

The problem with this example is that I can't figure out how to set my starting position, the default starting position is 0. Basically I wan't to be able to control if there is an available view on its left or the right.

这个例子的问题是我不知道如何设置我的起始位置,默认的起始位置是 0。基本上我无法控制它的左侧或右侧是否有可用的视图。

Is there any way to control center's View current position? is there a better way to do it? is it possible to make it circular?

有没有办法控制中心的查看当前位置?有没有更好的方法来做到这一点?有没有可能让它变成圆形?

回答by Kirill Kulakov

I've found a way to set it's position, which is done outside of the class:

我找到了一种方法来设置它的位置,这是在课外完成的:

 awesomePager = (ViewPager) findViewById(R.id.awesomepager);
 awesomePager.setAdapter(awesomeAdapter);
 awesomePager.setCurrentItem(CurrentPosition);

and it can be limited by calculating the amount of items I want to fit in to it

并且可以通过计算我想要放入的项目数量来限制它

回答by Malachiasz

I have noticed that if you recreate Activity (orientation change) with ViewPager having FragmentStatePagerAdapter, then the Adapter will reuse it's Fragments. The way to stop it is:

我注意到,如果您使用具有 FragmentStatePagerAdapter 的 ViewPager 重新创建 Activity(方向更改),则适配器将重用它的 Fragments。阻止它的方法是:

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    if (viewPager != null) {
        // before screen rotation it's better to detach pagerAdapter from the ViewPager, so
        // pagerAdapter can remove all old fragments, so they're not reused after rotation.
        viewPager.setAdapter(null);
    }
    super.onSaveInstanceState(savedInstanceState);
}

but then after Activity recreation ViewPager alwayes opens page 0 first and setCurrentItem(CurrentPosition); doesn't work. Fix for that is changing page after delay:

但是在 Activity 娱乐 ViewPager 之后总是先打开第 0 页并 setCurrentItem(CurrentPosition); 不起作用。修复延迟后更改页面的问题:

new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        viewPager.setCurrentItem(newPosition);
    }
}, 100);

回答by Helali

To start with the last fragment I did this:

从最后一个片段开始,我是这样做的:

PagerAdapter pagerAdapter = new PagerAdapter();
viewPager.setAdapter(pagerAdapter);
viewPager.setCurrentItem(pagerAdapter.getCount() - 1);

回答by DaveB

I came across a problem whereby if I set the current item before I set the adapter, the first item I get back will always be the one at position 0.

我遇到了一个问题,如果我在设置适配器之前设置当前项目,我返回的第一个项目将始终是位置 0 的项目。

Make sure you do:

确保你这样做:

awesomePager.setAdapter(awesomeAdapter);
awesomePager.setCurrentItem(CurrentPosition);

and not:

并不是:

awesomePager.setCurrentItem(CurrentPosition);
awesomePager.setAdapter(awesomeAdapter);

回答by Suhail k khan

I have an array of size more than 1000 and in dymanic viewpager I was facing leftswipe stuck on firstload. The below code solved this and resulted in smooth scroll:

我有一个大小超过 1000 的数组,在 dymanic viewpager 中,我面临着左滑动卡在第一次加载时的问题。下面的代码解决了这个问题并导致平滑滚动:

@Override
onResume(){
   super.onResume();
   viewPager.setCurrentItem(newPosition);
}

回答by Leebeedev

I'm using 3 fragments and on starting my app, the second (middle) fragment will be shown by default. Just I'm using the onResume function and all works great.

我正在使用 3 个片段,在启动我的应用程序时,默认情况下将显示第二个(中间)片段。只是我正在使用 onResume 函数,一切都很好。

@Override
protected void onResume() {
    super.onResume();
    m_viewPager.setCurrentItem(1);
}

回答by chanzmao

I encountered same problem. When I initialize ViewPager, the indicator position was 0. This may depends on amount of calculating for Pager contents. I use ViewTreeObserver like below.

我遇到了同样的问题。当我初始化 ViewPager 时,指标位置为 0。这可能取决于 Pager 内容的计算量。我使用如下所示的 ViewTreeObserver。

mGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        mViewPager.setCurrentItem(newPosition);
        removeOnGlobalLayoutListener(mSlidingTabLayout.getViewTreeObserver(), mGlobalLayoutListener);
    }
};
mSlidingLayout.getViewTreeObserver().addOnGlobalLayoutListener(mGlobalLayoutListener);

and,

和,

private void removeOnGlobalLayoutListener(ViewTreeObserver observer, ViewTreeObserver.OnGlobalLayoutListener listener) {
       if (observer == null) return;
       if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
               observer.removeGlobalOnLayoutListener(listener);
       } else {
               observer.removeOnGlobalLayoutListener(listener);
       }
} 

In this way, also never to bother with time setting of delay.

这样,也不必费心延迟时间设置。

回答by Yousef Gamal

Using viewPager.setCurrentItem(newPosition);shows to the user the transition from the starting page to the newPosition, to prevent that from happening and show the newPositiondirectly as if it was the starting point, I added falseto the second parameter something like this:

使用viewPager.setCurrentItem(newPosition);向用户显示从起始页到 的转换newPosition,为了防止这种情况发生并将newPosition直接显示为起点,我false在第二个参数中添加了如下内容:

int newPosition = pages.size()-1; // Get last page position

viewPager.setCurrentItem(newPosition, false); // 2nd parameter (false) stands for "smoothScroll"

回答by Kvant

@Override
public Object instantiateItem(ViewGroup container, int position) {
    View currentPage = null;
    switch(position){
        case 0:
            currentPage = LayoutInflater.from(context).inflate(R.layout.page0, null)    
            break;
        case 1:
            currentPage = LayoutInflater.from(context).inflate(R.layout.page1, null)    
            ///////////// This page will be default ////////////////////
            ((ViewPager)container).setCurrentItem(position);
            ////////////////////////////////////////////////////////////
            break;
        case 2:
            currentPage = LayoutInflater.from(context).inflate(R.layout.page2, null)    
            break;
    return currentPage;
}