Viewpager Android中 [英] Viewpager in Android
问题描述
我怎么能创建 ViewPager
在两个方向
。可能吗 ?如果有可能我怎么能实现它。
How could i create ViewPager
in both direction
. Is it possible ? If it possible how could i implement it.
我已经使用下面的code创建 viewpager
单方向:
I have created viewpager
in single direction using the below code:
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);
}
任何人可以帮助我out..thanks!
Could anybody help me out..thanks!!
推荐答案
从我个人理解好了,你有片段
要在其中用户启动列表在页
/ 片段
的中间,我已经打了,并测试了以下code,它是正常工作对我来说。
Okay from what I understand, you have a list of Fragments
in which you want the user to start in the middle of the Pages
/Fragments
, I have played with and tested the following code and it is working properly for me.
如果你知道片段
的数量和不动态改变片段
那么这可以处理多少更简单地说,但我将关闭在OP提供的code。试试看:
If you know the amount of Fragments
and aren't dynamically changing the Fragments
then this can be handled much more simply, but I am going off your code provided in the OP. Try it out:
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()
。
I have added a couple methods to this class, the init()
method, setCount()
, and 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;
}
}
的请注意:的你没有提供的您已经尝试了你,我已经写了code什么的例子。计算器是不是这个地方,将来当事情不工作给我们带来什么你已经尝试过,我们会尽力帮助确定出了什么问题。
Please Note: You did not provide examples of what you have tried so I have written code for you. StackOverflow isn't a place for this, in the future when something doesn't work give us what you have tried and we will try to help determine what went wrong.
这篇关于Viewpager Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!