码迷,mamicode.com
首页 > 其他好文 > 详细

ViewPager少于三张图片时,滑动Bug

时间:2015-02-06 13:17:53      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:

用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;
}


}


}

ViewPager少于三张图片时,滑动Bug

标签:

原文地址:http://blog.csdn.net/beefandpotatoes/article/details/43562633

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!