在手机客户端上,广告的循环播放很是常见,在网上也看过1些Demo,好多写的不是太完全,人为不干预的情况下确切是依照顺序轮播,但是人为去滑动后,接下来的自动轮播不是按滑动后的currentposition播放的,而是依照手动滑动前position的位置跳跃播放的。闲暇之余,把这个问题简单的解决了1下,为后来者提供1些思路。
=======================================================================================================
版权所有,如需转载请标明出处: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⑷⑵0
*/
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);
}
}
===========================================================================================
===========================================================================================