我正在开发循环视图页面指示器(循环)。基于stackoverflow解决方案,我实现了Circularviewpager类,它工作正常。我在这里遇到的问题是我有四个图像阵列即A,B,C,D。如果我从D向左滑动它会移动到A图像没有任何动画或平滑滚动,如从A到B图像滑动图像。我试过可能的方法来实现这一点没有任何问题,我在下面添加了我的代码建议我一些解决方案来实现这个目标。
public class HomeScreen extends FragmentActivity {
private ViewPager mPager;
private CirclePageIndicator mIndicator;
private PromotionalAdapter mAdapter;
private static int count = 0;
private static Context ctx;
protected int orientation;
protected int width = 0;
protected int height = 0;
RelativeLayout relativeLayout;
int currentPosition = 0;
int backPosition = 0;
//
private int mCurrentPos;
private int mScrollState;
LinearLayout itemContainer;
List<ImageView> itemImage;
TextView currentText;
private int previousState, currentState;
int currentPage;
public static final void createPromotionalPager(HomeScreen home,
TypedArray drawables) {
home.mAdapter = new PromotionalAdapter(
home.getSupportFragmentManager(), drawables);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ctx = this;
requestWindowFeature(Window.FEATURE_NO_TITLE);
initialUISetup();
}
private void screenDisplay() {
TypedArray drawables = getResources().obtainTypedArray(
R.array.launchscreen_images);
createPromotionalPager(this, drawables);
mPager = (ViewPager) findViewById(R.id.pager);
mIndicator = (CirclePageIndicator) findViewById(R.id.indicator);
mPager.addOnPageChangeListener(new CircularViewPageHandler(mPager));
mPager.setAdapter(mAdapter);
int leftRightSpace = (int) (width * 0.0153);
int topBottomSpace = (int) (height * 0.0089);
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
leftRightSpace = (int) (height * 0.0153);
topBottomSpace = (int) (width * 0.0089);
}
mIndicator.setSnap(true);
mIndicator.setViewPager(mPager);
}
public static class PromotionalFragment extends Fragment {
private Drawable mDrawable;
public static PromotionalFragment newInstance(Drawable drawable) {
PromotionalFragment fragment = new PromotionalFragment();
fragment.setDrawable(drawable);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.promotional_fragment, container,
false);
ImageView iv = (ImageView) v.findViewById(R.id.promotional_picture);
iv.setScaleType(ScaleType.CENTER_CROP);
iv.setImageDrawable(mDrawable);
return v;
}
public void setDrawable(Drawable drawable) {
mDrawable = drawable;
}
}
public static class PromotionalAdapter extends FragmentStatePagerAdapter {
private final TypedArray mPictures;
public static int LOOPS_COUNT = 2;
public PromotionalAdapter(FragmentManager fm, TypedArray drawables) {
super(fm);
mPictures = drawables;
}
@Override
public Fragment getItem(int arg0) {
try {
return PromotionalFragment.newInstance(mPictures
.getDrawable(arg0));
} catch (Resources.NotFoundException e) {
return null;
}
}
@Override
public int getCount() {
count = mPictures.length();
return mPictures.length();
}
}
public void initialUISetup() {
setContentView(R.layout.launchscreen_layout);
screenDisplay();
}
}
}
// CircularViewPageHandler.java
public class CircularViewPageHandler implements ViewPager.OnPageChangeListener {
private ViewPager mViewPager;
private int mCurrentPosition;
private int mScrollState;
int mInitialPage=0;
public CircularViewPageHandler(final ViewPager viewPager) {
mViewPager = viewPager;
}
@Override
public void onPageSelected(final int position) {
mCurrentPosition = position;
}
@Override
public void onPageScrollStateChanged(final int state) {
handleScrollState(state);
mScrollState = state;
}
private void handleScrollState(final int state) {
if (state == ViewPager.SCROLL_STATE_IDLE) {
setNextItemIfNeeded();
}
}
private void setNextItemIfNeeded() {
if (!isScrollStateSettling()) {
handleSetNextItem();
}
}
private boolean isScrollStateSettling() {
return mScrollState == ViewPager.SCROLL_STATE_SETTLING;
}
private void handleSetNextItem() {
final int lastPosition = mViewPager.getAdapter().getCount() - 1;
if(mCurrentPosition == 0) {
mViewPager.setCurrentItem(lastPosition, false);
} else if(mCurrentPosition == lastPosition) {
mViewPager.setCurrentItem(0, false);
}
}
@Override
public void onPageScrolled(final int position, final float positionOffset, final int positionOffsetPixels) {
Log.e("position",""+position);
}
}