android viewpager实现无限循环自动滚动

         实现原理很简单粗暴,把设配器的总数设置到最大。在两个广告的时候会出现滑动到边缘空白情况,这里可以换种思路,判断在两个广告的时候,当成四个处理,其实就是添加了两个相同的变成四个,暂时解决了这个问题。

 效果图:

                    


    

  入口文件:           

 
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private CycleSlipViewPager cycleSlipViewPager;
    private List<String> imageList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        cycleSlipViewPager= (CycleSlipViewPager) findViewById(R.id.cs_vp);
        imageList=new ArrayList<>();
        for(int i=0;i<6;i++){
            imageList.add("");
        }
        cycleSlipViewPager.initViewpager(MainActivity.this,imageList);
    }
}

   

程序主文件:       

public class CycleSlipViewPager extends LinearLayout {
    private ViewPager viewPager;
    private AdAdapter adAdapter;
    private LinearLayout parentView;
    private ImageView[] imageViews;
    private ImageView[] indicatorImageViews;
    private LinearLayout ll_indicator;
    private int currentPosition;
    private boolean isImageTwo;
    public CycleSlipViewPager(Context context) {
        super(context);
    }

    public CycleSlipViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    private void initView(Context context) {
        parentView = (LinearLayout) LayoutInflater.from(context).inflate(R.layout.viewpager_cycle_slip, this);
    }

    private void initIndicator(Context context) {
        ll_indicator = (LinearLayout) findViewById(R.id.ll_indicator);

        if (imageViews.length != 1) {
            if(!isImageTwo) {
                indicatorImageViews = new ImageView[imageViews.length];
                for (int i = 0; i < indicatorImageViews.length; i++) {
                    ImageView imageView = new ImageView(context);
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(10, 10);
                    layoutParams.leftMargin = 8;
                    layoutParams.bottomMargin = 8;
                    imageView.setBackgroundResource(R.drawable.round_white_bg);
                    indicatorImageViews[i] = imageView;
                    ll_indicator.addView(imageView, layoutParams);
                }
            }else{
                indicatorImageViews = new ImageView[2];
                for (int i = 0; i < 2; i++) {
                    ImageView imageView = new ImageView(context);
                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(10, 10);
                    layoutParams.leftMargin = 8;
                    layoutParams.bottomMargin = 8;
                    imageView.setBackgroundResource(R.drawable.round_white_bg);
                    indicatorImageViews[i] = imageView;
                    ll_indicator.addView(imageView, layoutParams);
                }
            }
        }
    }

    private void initViewPager() {
        viewPager = (ViewPager) parentView.findViewById(R.id.vp_ad);
        adAdapter = new AdAdapter();
        viewPager.setAdapter(adAdapter);
        if (imageViews.length != 1) {
            setIndicatorSelect(0);
            currentPosition = (imageViews.length) * 2;
            viewPager.setCurrentItem(currentPosition);
        }
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                //初始化的时候会调用一次的。
                if (imageViews.length != 1) {
                    handler.removeMessages(1);
                    handler.sendEmptyMessageDelayed(1, 3000);
                } else {
                    viewPager.setCurrentItem(0);
                    viewPager.scrollTo(0, 0);
                }
            }

            @Override
            public void onPageSelected(int position) {
                setIndicatorSelect(position);
                currentPosition = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    private void setIndicatorSelect(int position) {
        if (imageViews.length != 1) {
            for (int i = 0; i < indicatorImageViews.length; i++) {
                indicatorImageViews[i].setBackgroundResource(0);
                indicatorImageViews[i].setBackgroundResource(R.drawable.round_white_bg);
            }
            indicatorImageViews[position % indicatorImageViews.length].setBackgroundResource(R.drawable.round_red_bg);
        }
    }

    /**
     *
     * @param context
     * @param listArray 实体数据
     */
    public void initViewpager(Context context,List<?> listArray) {

        if (listArray != null && listArray.size() > 0) {
            if(listArray.size()==2) {
                ImageView[] imageViews=new ImageView[4];
                for (int i = 0; i < imageViews.length; i++) {
                    ImageView imageView = new ImageView(context);
                    loanImage(imageView);//image view的初始化
                    imageViews[i] = imageView;
                }
                isImageTwo=true;
            }else {
                ImageView[] imageViews=new ImageView[listArray.size()];
                for (int i = 0; i < imageViews.length; i++) {
                    ImageView imageView = new ImageView(context);
                    loanImage(imageView);//image view的初始化
                    imageViews[i] = imageView;
                }
                isImageTwo=false;
            }
            initView(context);
            initIndicator(context);
            initViewPager();
        }
    }
    /**异步加载图片*/
    private void loanImage(ImageView imageView){

    }
    public void removeHandleMessage() {
        handler.removeMessages(1);
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            currentPosition++;
            viewPager.setCurrentItem(currentPosition, true);
            sendEmptyMessageDelayed(1, 3000);
        }
    };

    class AdAdapter extends PagerAdapter {

        public AdAdapter() {

        }

        @Override
        public int getCount() {
            return Integer.MAX_VALUE;
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            // container.removeView(imageViews[position % imageViews.length]);
        }

        @Override
        public Object instantiateItem(View container, int position) {
            try {
                imageViews[position % imageViews.length].setOnClickListener(new AdvertisementOnClickListener(position));
                ((ViewGroup) container).addView(imageViews[position % imageViews.length]);
            } catch (Exception e) {
                // TODO: handle exception
            }
            return imageViews[position % imageViews.length];
        }

        private class AdvertisementOnClickListener implements OnClickListener {
            private int position;

            public AdvertisementOnClickListener(int position) {
                this.position = position;
            }

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(getContext(), "" + position % imageViews.length, Toast.LENGTH_SHORT).show();
            }
        }
    }

}


 
xml文件: 

<?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="wrap_content">
        <android.support.v4.view.ViewPager
            android:id="@+id/vp_ad"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        </android.support.v4.view.ViewPager>

        <LinearLayout
            android:id="@+id/ll_indicator"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:orientation="horizontal">
        </LinearLayout>
</RelativeLayout>


drawable:指示器文件

红色圆背景:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval"
    android:useLevel="false">
    <solid android:color="#d63508"/>
</shape>


白色圆背景:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval"
    android:useLevel="false">
    <solid android:color="#ffffff"/>
</shape>

                       


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值