广告自动轮播(完美解决人为滑动后按顺序自动轮播)

在手机客户端上,广告的循环播放很是常见,在网上也看过一些Demo,好多写的不是太完整,人为不干预的情况下确实是按照顺序轮播,但是人为去滑动后,接下来的自动轮播不是按滑动后的currentposition播放的,而是按照手动滑动前position的位置跳跃播放的。闲暇之余,把这个问题简单的解决了一下,为后来者提供一些思路。

=======================================================================================================

版权所有,如需转载请标明出处:http://blog.csdn.net/you4580

=======================================================================================================

public class MainActivity extends Activity implements OnPageChangeListener{

private ViewPager mViewPager;

private FoucsAdapter mFoucsAdapter;

private ArrayList<View> mFocusViewList;

private int mCurrentItem;

private boolean isRun;

private boolean nowAction;

//轮播定时器

private ScheduledExecutorService mScheduledExecutorService;

private int[] image=new int[]{R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher};

private Handler handler = new Handler() {

public void handleMessage(android.os.Message msg) {

mViewPager.setCurrentItem(mCurrentItem);

};

};

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mViewPager=(ViewPager) findViewById(R.id.electricity_viewPager);

mViewPager.setOnPageChangeListener(this);

mFoucsAdapter = new FoucsAdapter(getApplicationContext());

mViewPager.setAdapter(mFoucsAdapter);

mFocusViewList = new ArrayList<View>();

for (int i = 0; i < 5 ; i++) {

View view = this.getLayoutInflater().inflate(R.layout.electricity_focus_item,null);

TextView focusIndex = (TextView)view.findViewById(R.id.electricity_focusIndex);

ImageView focusImage = (ImageView)view.findViewById(R.id.electricity_focusImage);

focusIndex.setText(i+1+"/"+5);

focusImage.setBackgroundResource(image[i]);

mFocusViewList.add(view);

}

mFoucsAdapter.setList(mFocusViewList);

handler.postDelayed(new Runnable() {

@Override

public void run() {

//mScrollView.scrollTo(0,0);

if(!isRun){

if(mScheduledExecutorService != null){

mScheduledExecutorService.scheduleAtFixedRate(new ScrollTask(), 0, 3, TimeUnit.SECONDS);

}

}

}

},500);

}

@Override

protected void onStart() {

super.onStart();

mScheduledExecutorService = Executors.newSingleThreadScheduledExecutor();

}

@Override

protected void onStop() {

super.onStop();

if(mScheduledExecutorService!=null){

mScheduledExecutorService.shutdown();

}

}

private class ScrollTask implements Runnable {

public void run() {

synchronized (mViewPager) {

if (!nowAction) {

mCurrentItem = mCurrentItem+1;

handler.obtainMessage().sendToTarget();

}

}

}

}

@Override

public void onPageScrolled(int position, float positionOffset,

int positionOffsetPixels) {

}

@Override

public void onPageSelected(int position) {

mCurrentItem = position;

}

@Override

public void onPageScrollStateChanged(int state) {

if (state == 0) {

nowAction = false;

}

if (state == 1) {

nowAction = true;

}

}

}

===========================================================================================

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context="com.example.adscirledemo.MainActivity" >

<android.support.v4.view.ViewPager

android:id="@+id/electricity_viewPager"

android:layout_width="match_parent"

android:layout_height="150dp"

android:background="#ffffff" />

</RelativeLayout>

============================================================================================

/**

*

* @DESCRIPTION:

* @AUTHOR:you4580

* @VERSION:v1.0

* @DATE:2015-4-20

*/

public class FoucsAdapter extends PagerAdapter{

private Context mContext;

private List<View> mList;

private MyItemClickLitener myItemClickLitener;

public FoucsAdapter(Context context){

this.mContext = context;

}

public void setList(List<View> list){

this.mList = list;

this.notifyDataSetChanged();

}

@Override

public int getCount() {

if(mList == null)return 0;

return Integer.MAX_VALUE;

}

@Override

public Object instantiateItem(ViewGroup container, final int position) {

View view = null;

if (position % mList.size() < 0) {

view = mList.get(mList.size() + position);

} else {

view = mList.get(position % mList.size());

}

ViewParent vp = view.getParent();

if (vp != null) {

ViewGroup parent = (ViewGroup) vp;

parent.removeView(view);

}

((ViewPager) container).addView(view);

view.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

myItemClickLitener.OnItemClick(position);

}

});

return view;

}

@Override

public void destroyItem(ViewGroup container, int position, Object object) {

}

@Override

public boolean isViewFromObject(View view, Object object) {

return view == object;

}

public void setMyItemClickLitener(MyItemClickLitener myItemClickLitener){

this.myItemClickLitener = myItemClickLitener;

}

public interface MyItemClickLitener{

public void OnItemClick(int position);

}

}

===========================================================================================

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent" >

<RelativeLayout

android:layout_width="match_parent"

android:layout_height="match_parent" >

<ImageView

android:id="@+id/electricity_focusImage"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="@drawable/bplaceholder_head_news"

android:flipInterval="30"

android:persistentDrawingCache="animation"

android:scaleType="centerCrop" />

</RelativeLayout>

<TextView

android:layout_width="fill_parent"

android:layout_height="20dip"

android:layout_alignParentBottom="true"

android:alpha="0.5"

android:background="@drawable/ele_focus_bottom" />

<TextView

android:id="@+id/electricity_focusIndex"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentBottom="true"

android:layout_alignParentRight="true"

android:layout_marginRight="3dp"

android:gravity="center_vertical|right"

android:paddingRight="5dp"

android:textColor="#ffffff"

android:textSize="16sp" />

</RelativeLayout>

===========================================================================================

时间: 2024-11-05 21:39:22

广告自动轮播(完美解决人为滑动后按顺序自动轮播)的相关文章

教你用两招就完美解决 Android 滑动冲突!

冲突情况 在 Android 开发中,滑动冲突总是我们一个无法避免的话题.而对于解决方案却是众说纷纭.比如 RecyclerView 嵌套 RecyclerView,直接通过相关方法禁掉内部 RecyclerView 的滑动:ScrollView 嵌套 RecyclerView 直接把 ScrollView 替换为 NestedScrollView 等等. 但我们今天要说的是在自定义 View 中遇到滑动冲突时,我们又应该如何处理呢?当然,今天的话题需要 View 的事件分发机制做理论前提. 1

Win7+Linux双系统,完美解决删除Linux后出现的任何问题!

首先,进入到Win7,安装MiniTool Partition Wizard Home Edition删除掉Linux分区, 点Yes 然后选中 这里选中这个,重写MBR,开机神马grub神马问题都没有了,点完应用,自己启动试试就知道了!完毕! 

解决41版后chrome菜单标题不支持mactype的方法

chrome升级后,采用directwrite,导致mactype无法对chrome进行字体渲染,根据前人的成果,解决方法总结如下: 1.chrome://flags/#disable-direct-write 启用 此项可以解决网页字体渲染问题 2.chrome快捷方式启动参数添加 --disable-directwrite-for-ui 此项可以解决菜单标题问题,但是仅此快捷方式有效 3.注册表修改添加--disable-directwrite-for-ui 可以完美解决chrome启动后的

完美解决ScrollView嵌套ViewPager滑动失效和无法正常滑动冲突问题

/******************************************************************************* * Copyright 2011, 2012 Chris Banes. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the Li

Android:完美解决listview的setonitemclicklistener 和 setontouchlistener冲突问题

@Override public boolean onTouch(View v, MotionEvent event) { createVelocityTracker(event); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: xDown = event.getRawX(); break; case MotionEvent.ACTION_MOVE: xMove = event.getRawX(); //活动的距离 int

【Android开发】完美解决Android完全退出程序

背景:假说有两个Activity, Activity1和Activity2, 1跳转到2,如果要在2退出程序,一般网上比较常见的说法是用 System.exit(0) 或是 android.os.Process.killProcess(android.os.Process.myPid()) 但实际应用中,并不是能够真正退出,问题出在?1跳转到2时,如果Activity1你finish掉了,两么是可以退出程序的,但有时1跳转到2时,我们不能将Activity1 finish掉,那么在Activit

完美解决微信下拉上拉

转http://note.youdao.com/share/?id=f13df2c846cac5d4f0e24a715d8372a6&type=note#/ 完美解决微信下拉上拉回弹 1.使用jroll.js完美解决,下载jroll.js. 2.DOM结构为: class container 为滑动页面 3.添加css #wrapper { position: absolute; top: 44px; bottom: 0; width: 100%; } 4.编写js <script src=

web移动端fixed布局和input等表单的爱恨情仇 - 终极BUG,完美解决

[问题]移动端开发,ios下当fixed属性和输入框input(这里不限于input,只要可以调用移动端输入法的都包括,如:textarea.HTML5中contenteditable等),同时存在的时候:两位大侠瞬间发生剧烈的化学反应,出现各种奇葩问题,见下图: [结论]输入框position属性值不是fixed,而变成了absolute [出现情况]当我们唤起键盘的时候,输入框位置不再页面最下面,或者说页面当时还可以继续往下滚动,再或者页面没有滚动到最下边,这个时候就会出现上面的问题 [学习

完美解决Android完全退出程序(转)

背景:假说有两个Activity, Activity1和Activity2, 1跳转到2,如果要在2退出程序,一般网上比较常见的说法是用 System.exit(0) 或是 android.os.Process.killProcess(android.os.Process.myPid()) 但实际应用中,并不是能够真正退出,问题出在?1跳转到2时,如果Activity1你finish掉了,两么是可以退出程序的,但有时1跳转到2时,我们不能将Activity1 finish掉,那么在Activit