国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > php开源 > 综合技术 > 广告自动轮播(完美解决人为滑动后按顺序自动轮播)

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

来源:程序员人生   发布时间:2015-05-26 08:30:04 阅读次数:3325次

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


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


<?xml version="1.0" encoding="utf⑻"?>
<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>



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

生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生