在手机客户端上,广告的循环播放很是常见,在网上也看过一些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>
===========================================================================================