15

可能重复:
ViewPager 作为循环队列/包装

我的代码中有三个片段类。FirstActivity,SecondActivity,ThirdActivity 我想刷这三个活动..这三个是从 Fragment 类扩展的

public class ViewPagerFragmentActivity extends FragmentActivity {
private PagerAdapter mPagerAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    super.setContentView(R.layout.viewpager_layout);
    initialisePaging();
    }

/**
 * Initialize the fragments to be paged
 */
List<Fragment> fragments = new Vector<Fragment>();
ViewPager pager;
private void initialisePaging() {
    fragments.add(Fragment.instantiate(this, FirstActivity.class.getName()));
    fragments.add(Fragment.instantiate(this, SecondActivity.class.getName()));
    fragments.add(Fragment.instantiate(this, ThirdActivity.class.getName()));
    this.mPagerAdapter = new MyPagerAdapter(super.getSupportFragmentManager(), fragments);
    pager = (ViewPager) super.findViewById(R.id.viewpager);
    pager.setAdapter(this.mPagerAdapter);

}

}

这是我的 FragmentPageAdapter 类

public class MyPagerAdapter extends FragmentPagerAdapter {

private final List<Fragment> fragments;


public MyPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
    super(fm);
    this.fragments = fragments;
}


@Override
public Fragment getItem(int position) {
    System.out.println("position"+position);
    return this.fragments.get(position);
}


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

}

我的代码以这种方式工作, A<->B<->c 但我的要求是像这样运行C<->A<->B<->C<-A-> ,在圆形片段中是可滑动的......

4

2 回答 2

19

圆形 ViewPager 是可能的。借助ViewPager 作为循环队列/包装此链接已编码为这样。希望它对您需要的有所帮助。

CircluarPagerAdapter 类:

>

 public class CircularPagerAdapter extends PagerAdapter{

private int[] pageIDsArray;
private int count;

public CircularPagerAdapter(final ViewPager pager, int... pageIDs) {
    super();
    int actualNoOfIDs = pageIDs.length;
    count = actualNoOfIDs + 2;
    pageIDsArray = new int[count];
    for (int i = 0; i < actualNoOfIDs; i++) {
        pageIDsArray[i + 1] = pageIDs[i];
    }
    pageIDsArray[0] = pageIDs[actualNoOfIDs - 1];
    pageIDsArray[count - 1] = pageIDs[0];

    pager.setOnPageChangeListener(new OnPageChangeListener() {

        public void onPageSelected(int position) {
            int pageCount = getCount();
            if (position == 0){
                pager.setCurrentItem(pageCount-2,false);
            } else if (position == pageCount-1){
                pager.setCurrentItem(1,false);
            }
        }

        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            // TODO Auto-generated method stub
        }

        public void onPageScrollStateChanged(int state) {
            // TODO Auto-generated method stub
        }
    });
}

public int getCount() {
    return count;
}

public Object instantiateItem(View container, int position) {
    LayoutInflater inflater = (LayoutInflater) container.getContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    int pageId = pageIDsArray[position];
    View view = inflater.inflate(pageId, null);
    ((ViewPager) container).addView(view, 0);
    return view;
}

@Override
public void destroyItem(View container, int position, Object object) {
    ((ViewPager) container).removeView((View) object);
}

@Override
public void finishUpdate(View container) {
    // TODO Auto-generated method stub
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return view == ((View) object);
}

@Override
public void restoreState(Parcelable state, ClassLoader loader) {
    // TODO Auto-generated method stub
}

@Override
public Parcelable saveState() {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void startUpdate(View container) {
    // TODO Auto-generated method stub
}
}

这是你的主要课程:

>

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ViewPager myPager = (ViewPager) findViewById(R.id.conpageslider);
    PagerAdapter adapter = new CircularPagerAdapter(myPager, new int[]{R.layout.first_activity, R.layout.second_activity, R.layout.third_activity});
    myPager.setAdapter(adapter);
    myPager.setCurrentItem(3);


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}


}
于 2012-07-13T10:03:46.363 回答
14

你可以尝试这样的事情:

1)使getCount()返回一个巨大的价值(滑动方式的数量高于您的用户可能使用的方式)

2)做getItem这样的回报。fragments.get(position % fragments.size())

3) 让您的寻呼机从定义的范围中间的某个位置开始getCount()(为了允许在开始时从 A 滑动到 C),请确保选择一个可以为您提供第一个片段 % 3 的值

我知道这个解决方案似乎有点脏,但我目前找不到更好的解决方案

于 2012-07-13T07:45:18.597 回答