我如何在ViewPager
中创建both direction
。可能吗 ?如果可能的话,我怎么实现呢。
我使用以下代码在单个方向上创建了viewpager
:
private void initialisePaging() {
List<Fragment> fragments = new Vector<Fragment>();
fragments.add(Fragment.instantiate(this, Fragment0.class.getName()));
fragments.add(Fragment.instantiate(this, Fragment1.class.getName()));
fragments.add(Fragment.instantiate(this, Fragment2.class.getName()));
this.mPagerAdapter = new MyPagerAdapter(super.getSupportFragmentManager(), fragments);
ViewPager pager = (ViewPager) super.findViewById(R.id.viewpager);
pager.setAdapter(this.mPagerAdapter);
}
有人可以帮我吗..谢谢!
最佳答案
根据我的理解,您有一个Fragments
列表,您希望用户在Pages
/ Fragments
的中间开始,我已经使用并测试了以下代码,并且对于我。
如果您知道Fragments
的数量并且没有动态更改Fragments
,则可以更简单地进行处理,但是我不使用OP中提供的代码。试试看:
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// This is going to initialize our Fragment list
mSectionsPagerAdapter.init();
// Now we are going to determine the starting position
int startPos = mSectionsPagerAdapter.getStartPosition();
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
// AFTER we set the adapter we will define our start position
mViewPager.setCurrentItem(startPos);
}
}
现在让我们来看看我们的
FragmentPagerAdapter
我向此类添加了两个方法,分别是
init()
方法,setCount()
和getStartPosition()
。public class SectionsPagerAdapter extends FragmentPagerAdapter {
private int _count = 2;
private int _start = 0;
List<Fragment> fragments;
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
public void init() {
fragments = new ArrayList<Fragment>();
fragments.add(new MyFragmentOne());
fragments.add(new MyFragmentTwo());
fragments.add(new MyFragmentThree());
fragments.add(new MyFragmentFour());
fragments.add(new MyFragmentFive());
setCount();
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
return super.instantiateItem(container, position);
}
@Override
public Fragment getItem(int position) {
return (fragments.size() >= position) ? fragments.get(position) : null;
}
// We have created this method to get the starting position
// You might want to play around with this to make sure the returned
// position fits what you want for odd numbered Fragment Pages
public int getStartPosition() {
_start = fragments.size() / 2;
return Math.round(_start);
}
public void setCount() { this._count = fragments.size(); }
@Override
public int getCount() { return this._count; }
// You will need to change this to get the appropriate title per page
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return getString(R.string.company_select).toUpperCase(Locale.ENGLISH);
case 1:
return getString(R.string.companies_pending).toUpperCase(Locale.ENGLISH);
}
return null;
}
}
请注意:您没有提供尝试的示例,所以我为您编写了代码。 StackOverflow并不是解决这个问题的地方,将来在某些情况下无法正常工作时,请向我们提供您所尝试的内容,我们将尽力帮助确定问题出在哪里。