android 图片轮播 图片切换向右滑动出现白色页面
2016-12-31 06:26
429 查看
?
public class MyViewPagerScroll extends ViewPager {
Activity mActivity; // 上下文
List<View> mListViews; // 图片组
int mScrollTime = 0;
Timer timer;
int oldIndex = 0;
int curIndex = 0;
boolean listis2 = false;
/*
* 每隔固定时间切换广告栏图片
*/
@SuppressLint("HandlerLeak") private final Handler viewHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
MyViewPagerScroll.this.setCurrentItem(MyViewPagerScroll.this.getCurrentItem() + 1);
super.handleMessage(msg);
}
};
public MyViewPagerScroll(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void start(Activity mainActivity, List<View> imgList, int scrollTime,
LinearLayout ovalLayout, int ovalLayoutId, int ovalLayoutItemId, int focusedId,
int normalId, boolean listis2) {
this.listis2 = listis2;
mActivity = mainActivity;
mListViews = imgList;
mScrollTime = scrollTime;
if(imgList.size()>1){
// 设置圆点
setOvalLayout(ovalLayout, ovalLayoutId, ovalLayoutItemId, focusedId, normalId);
}
this.setAdapter(new MyPagerAdapter());// 设置适配器
if (scrollTime != 0 && mListViews.size() > 1) {
// 设置滑动动画时间 ,如果用默认动画时间可不用 ,反射技术实现
// new FixedSpeedScroller(mActivity).setDuration(this, 700);
startTimer();
// 触摸时停止滚动
this.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
MyViewPagerScroll.this.setCurrentItem(MyViewPagerScroll.this.getCurrentItem());
startTimer();
} else {
stopTimer();
}
return false;
}
});
}
}
// 设置圆点
private void setOvalLayout(final LinearLayout ovalLayout, int ovalLayoutId,
final int ovalLayoutItemId, final int focusedId, final int normalId) {
if (ovalLayout != null) {
LayoutInflater inflater = LayoutInflater.from(mActivity);
} else {
for (int i = 0; i < mListViews.size(); i++) {
ovalLayout.addView(inflater.inflate(ovalLayoutId, null));
}
// 选中第一个
ovalLayout.getChildAt(0).findViewById(ovalLayoutItemId).setBackgroundResource(
focusedId);
this.setOnPageChangeListener(new OnPageChangeListener() {
public void onPageSelected(int i) {
curIndex = i % mListViews.size();
// 取消圆点选中
ovalLayout.getChildAt(oldIndex).findViewById(ovalLayoutItemId).setBackgroundResource(
normalId);
// 圆点选中
ovalLayout.getChildAt(curIndex).findViewById(ovalLayoutItemId).setBackgroundResource(
focusedId);
oldIndex = curIndex;
}
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
public void onPageScrollStateChanged(int arg0) {
}
});
}
}
}
/**
* 取得当明选中下标
*
* @return
*/
public int getCurIndex() {
return curIndex;
}
/**
* 开始滚动
*/
public void startTimer() {
if (timer == null) {
timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
mActivity.runOnUiThread(new Runnable() {
public void run() {
viewHandler.sendEmptyMessage(1);
}
});
}
}, mScrollTime, mScrollTime);
}
}
// 适配器 //循环设置
private class MyPagerAdapter extends PagerAdapter {
public void finishUpdate(View arg0) {
}
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
public int getCount() {
if (mListViews.size() == 1) {// 一张图片时不用流动
return mListViews.size();
}
return Integer.MAX_VALUE;
}
public Object instantiateItem(View v, int i) {
((ViewPager) v).removeView(mListViews.get(i % mListViews.size()));
((ViewPager) v).addView(mListViews.get(i % mListViews.size()), 0);
return mListViews.get(i % mListViews.size());
}
public class MyViewPagerScroll extends ViewPager {
Activity mActivity; // 上下文
List<View> mListViews; // 图片组
int mScrollTime = 0;
Timer timer;
int oldIndex = 0;
int curIndex = 0;
boolean listis2 = false;
/*
* 每隔固定时间切换广告栏图片
*/
@SuppressLint("HandlerLeak") private final Handler viewHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
MyViewPagerScroll.this.setCurrentItem(MyViewPagerScroll.this.getCurrentItem() + 1);
super.handleMessage(msg);
}
};
public MyViewPagerScroll(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void start(Activity mainActivity, List<View> imgList, int scrollTime,
LinearLayout ovalLayout, int ovalLayoutId, int ovalLayoutItemId, int focusedId,
int normalId, boolean listis2) {
this.listis2 = listis2;
mActivity = mainActivity;
mListViews = imgList;
mScrollTime = scrollTime;
if(imgList.size()>1){
// 设置圆点
setOvalLayout(ovalLayout, ovalLayoutId, ovalLayoutItemId, focusedId, normalId);
}
this.setAdapter(new MyPagerAdapter());// 设置适配器
if (scrollTime != 0 && mListViews.size() > 1) {
// 设置滑动动画时间 ,如果用默认动画时间可不用 ,反射技术实现
// new FixedSpeedScroller(mActivity).setDuration(this, 700);
startTimer();
// 触摸时停止滚动
this.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
MyViewPagerScroll.this.setCurrentItem(MyViewPagerScroll.this.getCurrentItem());
startTimer();
} else {
stopTimer();
}
return false;
}
});
}
}
// 设置圆点
private void setOvalLayout(final LinearLayout ovalLayout, int ovalLayoutId,
final int ovalLayoutItemId, final int focusedId, final int normalId) {
if (ovalLayout != null) {
LayoutInflater inflater = LayoutInflater.from(mActivity);
} else {
for (int i = 0; i < mListViews.size(); i++) {
ovalLayout.addView(inflater.inflate(ovalLayoutId, null));
}
// 选中第一个
ovalLayout.getChildAt(0).findViewById(ovalLayoutItemId).setBackgroundResource(
focusedId);
this.setOnPageChangeListener(new OnPageChangeListener() {
public void onPageSelected(int i) {
curIndex = i % mListViews.size();
// 取消圆点选中
ovalLayout.getChildAt(oldIndex).findViewById(ovalLayoutItemId).setBackgroundResource(
normalId);
// 圆点选中
ovalLayout.getChildAt(curIndex).findViewById(ovalLayoutItemId).setBackgroundResource(
focusedId);
oldIndex = curIndex;
}
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
public void onPageScrollStateChanged(int arg0) {
}
});
}
}
}
/**
* 取得当明选中下标
*
* @return
*/
public int getCurIndex() {
return curIndex;
}
/**
* 开始滚动
*/
public void startTimer() {
if (timer == null) {
timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
mActivity.runOnUiThread(new Runnable() {
public void run() {
viewHandler.sendEmptyMessage(1);
}
});
}
}, mScrollTime, mScrollTime);
}
}
// 适配器 //循环设置
private class MyPagerAdapter extends PagerAdapter {
public void finishUpdate(View arg0) {
}
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
public int getCount() {
if (mListViews.size() == 1) {// 一张图片时不用流动
return mListViews.size();
}
return Integer.MAX_VALUE;
}
public Object instantiateItem(View v, int i) {
((ViewPager) v).removeView(mListViews.get(i % mListViews.size()));
((ViewPager) v).addView(mListViews.get(i % mListViews.size()), 0);
return mListViews.get(i % mListViews.size());
}
相关文章推荐
- android的适配问题?在线等...
- android开发代码顺序影响程序运行的问题
- 寻找将图片一次可以做成android里面各个分辨率的工具
- 【Android】从一个程序的activity调到另一个程序的activity时出错
- Android中横竖屏切换加载不同布局导致fragment数据丢失的问题
- android DES解密返回空值
- Android手机通过Wifi与PC机之间进行通信问题 (有代码)
- android从横屏的activity退回到竖屏的activity时,程序自动退出了。
- 请问Android如何在拍摄相片时获得地理位置
- android开发中遇到一个图片轮播和listview的问题
- android 蓝牙自动配对问题
- android消息传递机制
- Android Settings类的"LPw"含义
- android播放amr格式音频文件的问题
- 请教各位Android登录空指针问题
- Android中调用listview 控件的 listview.setAdapter(adapter)程序闪退、崩溃
- android iTextAsian
- android Fragment 返回键 退出
- android问题:从list集合中循环读取图片地址,使用异步下载,利用baseAdapter怎样显示到listView中
- 关于Android SDK Manager结构问题