问题描述
有一个用例 ViewPager
我从来没有见过漂亮的实现。
ViewPager
或多或少的静态结构。将页面添加到右侧(附加到模型并显示它)并不困难,但是,应该有一些可以使用的解决方案来扩展PagerAdapter(或其中一些子类),以便它可以在两个方向扩展。 / p>
我可以想象这样的适配器接口
boolean isEmpty()
boolean hasNext()
boolean hasPrevious()
对象getNext()
对象getPrevious()
对象getItem(int position)
//或如果使用泛型
T getNext()
T getPrevious()
T getItem(int position)
类似于集合Iterator,但是双向。
其中索引/位置不受从0以下的限制,但可以使用整数类型的整数。
可能没有将数组的实现(这是0到无限)的基础。
我发现这个 hack:
但是正如我之前所说,我试图让它自然工作,而不是维持3,5。 ..项目和强制ViewPager根据一些扭曲的逻辑来更改当前位置
目前是否有足够的实现或有必要实现它?
我愿意奖赏奖励答案,如果这将是一个全新的实现。
这个帮助, p>
public static class MyAdapter扩展FragmentPagerAdapter {
public MyAdapter(FragmentManager fm){
super(fm);
}
@Override
public int getCount(){
return Integer.MAX_VALUE;
}
@Override
public Fragment getItem(int position){
return getFragmentBasedOnPosition(position);
}
private Fragment getFragmentBasedOnPosition(int position){
int fragmentPos = position%3; //假设你有3个片段
switch(fragmentPos){
case 1:
return Fragment1.newInstance();
case 2:
return Fragment2.newInstance();
案例3:
返回Fragment3.newInstance();
}
}
}
然后,
mPager.setCurrentItem((int)(Integer.MAX_VALUE / 2));
//假设mPager是您的ViewPager
There is one use-case of ViewPager
I've never seen pretty implemented.
ViewPager
is more or less static structure. It's not so hard to add Pages to the right side (appending to Model and displaying it), however, there should be nice-to-use solution to expand PagerAdapter (or some of it's subclasses) so it can expand in both directions.
I can imagine interface for Adapter like this
boolean isEmpty()
boolean hasNext()
boolean hasPrevious()
Object getNext()
Object getPrevious()
Object getItem(int position)
// or if using generics
T getNext()
T getPrevious()
T getItem(int position)
Similar to Collections Iterator, but both-directional.
Where index/position is not limited from below on 0, but can use whole range of Integer type.
Maybe not base the implementation on array (which is 0 to infinite).
I've found this "hack": dynamically add and remove view to viewpager
But as I stated before, I'm trying to get it working naturally, not maintaining 3,5,... items and force ViewPager to change current position based on some twisted logic
Is there currently any sufficient implementation or is it necessary to implement it?
I'm willing to bounty reward answer, if it will be a whole new implementation.
Will this help,
public static class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public Fragment getItem(int position) {
return getFragmentBasedOnPosition(position);
}
private Fragment getFragmentBasedOnPosition(int position) {
int fragmentPos = position % 3; // Assuming you have 3 fragments
switch(fragmentPos) {
case 1:
return Fragment1.newInstance();
case 2:
return Fragment2.newInstance();
case 3:
return Fragment3.newInstance();
}
}
}
and then,
mPager.setCurrentItem((int)(Integer.MAX_VALUE/2));
// assuming mPager is your ViewPager
这篇关于无限/动态ViewPager在两个方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!