android 图片轮播 图片切换向右滑动出现白色页面
来源:互联网 发布:幻想乡网络手游 编辑:程序博客网 时间:2024/05/05 10:13
?
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());
}
0 0
- android 图片轮播 图片切换向右滑动出现白色页面
- android 图片轮播 图片切换向右滑动出现白色页面
- android图片向右滑动
- Flexslider图片轮播、文字图片相结合滑动切换效果
- Flexslider图片轮播、文字图片相结合滑动切换效果
- 图片滑动轮播
- 轮播图片切换
- 手机webApp触摸左右滑动切换轮播图片广告
- Android实现图片轮播切换
- 轮播插件--Flexslider图片轮播、文字图片相结合滑动切换效果
- Android 向右滑动关闭页面
- Android使用ViewPager实现图片轮播和手势滑动
- Android图片轮播
- Android 图片轮播
- android图片轮播
- Android图片轮播
- android 图片轮播
- android 图片轮播
- ios两张图片合成后有明显的背景不一致,区别很大,如何减少区别
- android的适配问题?在线等...
- Leetcode 39. Combination Sum
- GridView自适应高度后,第一个Item点击无效
- 学ios需要具备什么专业基础
- android 图片轮播 图片切换向右滑动出现白色页面
- jpush如何做到指定用户的推送?
- 求大神解救!!!一个不懂的异常,该怎么解决?
- 关于listview嵌套gridview等复杂布局的问题
- viewpager和fragment实现的tab页面如何实现点击跳转
- Objective-C编程之道iOS设计模式子类化单例疑惑
- android-gif-drawable在android studio上怎么配置
- uiwebview打开网页崩溃。求大神。
- 关于第三方facebook app Invalid key hash 问题