实现原理很简单粗暴,把设配器的总数设置到最大。在两个广告的时候会出现滑动到边缘空白情况,这里可以换种思路,判断在两个广告的时候,当成四个处理,其实就是添加了两个相同的变成四个,暂时解决了这个问题。
效果图:
入口文件:
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 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();
}
}
}
}
drawable:指示器文件
红色圆背景:
白色圆背景: