标签:
用ViewPager实现轮滑,当图片少于三张时,滑动出现问题,修改如下,逻辑比较简单,直接代码:
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
private ViewPager mPager = null;
private int imagesId[] = new int[] { R.drawable.img01
};
public int LOOPCOUNT;
public int MIDCOUNT;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPager = (ViewPager) findViewById(R.id.pager);
LOOPCOUNT = (imagesId.length) * 100;
MIDCOUNT = LOOPCOUNT / 2;
PagerAdapter galleryAdapter = new galleryAdapter();
mPager.setAdapter(galleryAdapter);
if (imagesId.length == 3) {
mPager.setCurrentItem(MIDCOUNT);
} else {
mPager.setCurrentItem(MIDCOUNT);
}
}
private class galleryAdapter extends PagerAdapter {
@Override
public int getCount() {
// TODO Auto-generated method stub
return LOOPCOUNT;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
container.removeView((View) object);
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
int s = 0;
Toast.makeText(MainActivity.this, position + "", Toast.LENGTH_LONG)
.show();
if (position < MIDCOUNT) {
s = imagesId.length
- (Math.abs(position - MIDCOUNT) % imagesId.length);
} else {
s = position - MIDCOUNT;
}
int pos = s % imagesId.length;
ImageView view = new ImageView(getApplicationContext());
view.setImageResource(imagesId[pos]);
container.addView(view);
return view;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0 == arg1;
}
}
}
标签:
原文地址:http://blog.csdn.net/beefandpotatoes/article/details/43562633