android 无限轮播的广告位

时间:2020-12-15 03:21:05

无限轮播广告位

没有录屏,将就将就着看,效果就是这样

android 无限轮播的广告位


主要代码KsBanner.java

/**
* 广告位
* <p>
* Created by <lzh> on 2016/12/20.
*/


public class KsBanner extends FrameLayout implements ViewPager.OnPageChangeListener {

private List<ImageView> vpLists;
private LinearLayout ll_dot_group; //用来添加小圆点
private List<String> imageDesList; // 文字描述
private List<String> imageUrlList; // 图片
private TextView tv_img_desc;
private ViewPager vp;
private Context mContext;

private boolean isSwitchPager = true; //默认切换
private int previousPosition = 0; //默认为0

private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
//更新当前viewpager的 要显示的当前条目
vp.setCurrentItem(vp.getCurrentItem() + 1);
}
};

private OnBannerItemClickListener listener;
public void setOnBannerItemClickListener(OnBannerItemClickListener listener){
this.listener = listener;
}

public KsBanner(Context context) {
super(context);
init(context, null, 0);
}

public KsBanner(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs, 0);
}

public KsBanner(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs, defStyleAttr);
}

private void init(Context context, AttributeSet attrs, int defStyleAttr) {
mContext = context;
imageDesList = new ArrayList<>();
imageUrlList = new ArrayList<>();
vpLists = new ArrayList<>();

initView();
startSlider();
}

/**
* 初始化view
*/

private void initView() {
LayoutInflater.from(mContext).inflate(R.layout.ksbanner_layout, this, true);
vp = (ViewPager) findViewById(R.id.viewPager);
ll_dot_group = (LinearLayout) findViewById(R.id.dotView);
tv_img_desc = (TextView) findViewById(R.id.bannerText);


}

// 计时更换图片
public void startSlider() {
new Thread() {
public void run() {
while (isSwitchPager) {
SystemClock.sleep(5000);
//拿着我们创建的handler 发消息
handler.sendEmptyMessage(0);
}
}
}.start();
}

/**
* 设置广告文字
* @param list
*/

public void setBannerText(List<String> list){
tv_img_desc.setVisibility(VISIBLE);
imageDesList.addAll(list);
tv_img_desc.setText(imageDesList.get(0)); // 设置广告文字
}

public void setBannerImg(List<String> imgList){
// 清除布局
if (imageUrlList.size() != 0) {
imageUrlList.clear();
vpLists.clear();
ll_dot_group.removeAllViews();
}
imageUrlList.addAll(imgList);

ImageView iv;
View dotView;

for (int i = 0; i < imageUrlList.size(); i++) {
iv = new ImageView(mContext);
iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
iv.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, CommonUtils.px2dp(220)));
Glide.with(mContext).load(imageUrlList.get(i)).into(iv);
vpLists.add(iv);
//准备小圆点的数据
dotView = new View(mContext);

//设置小圆点的宽和高
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(15, 15);
lp.setMargins(10, 0, 10, 0);
dotView.setLayoutParams(lp);

dotView.setBackgroundResource(R.drawable.selector_dot);
//设置小圆点默认状态
dotView.setEnabled(false);
//把dotview加入到线性布局中
ll_dot_group.addView(dotView);

final int finalI = i;
iv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(listener != null)
listener.ItemClickListener(finalI);
}
});
}

vp.setAdapter(new ViewpagerAdapter());

//设置当前viewpager要显示第几个条目
int item = Integer.MAX_VALUE / 2 - (Integer.MAX_VALUE / 2 % vpLists.size());
vp.setCurrentItem(item);

//把第一个小圆点设置为白色,显示第一个textview内容
ll_dot_group.getChildAt(previousPosition).setEnabled(true);
// tv_img_desc.setText(imageDesList.get(previousPosition));
//设置viewpager滑动的监听事件
vp.addOnPageChangeListener(this);


}


/**
* 定义数据适配器
*/

private class ViewpagerAdapter extends PagerAdapter {
@Override
public int getCount() {
return Integer.MAX_VALUE;
}

//是否复用当前view对象
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}

//初始化每个条目要显示的内容
@Override
public Object instantiateItem(ViewGroup container, int position) {
//拿着position位置 % 集合.size
int newposition = position % vpLists.size();
//获取到条目要显示的内容imageview
ImageView iv = vpLists.get(newposition);

if(iv.getParent() != null){
container.removeView(iv);
}

//要把 iv加入到 container 中
container.addView(iv);
return iv;
}

//销毁条目
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
//移除条目
// container.removeView((View) object);
}
}

//当新的页面被选中的时候调用
@Override
public void onPageSelected(int position) {
//拿着position位置 % 集合.size
int newposition = position % vpLists.size();
//取出postion位置的小圆点 设置为true
ll_dot_group.getChildAt(newposition).setEnabled(true);
//把一个小圆点设置为false
ll_dot_group.getChildAt(previousPosition).setEnabled(false);
tv_img_desc.setText(imageDesList.get(newposition % imageDesList.size()));
previousPosition = newposition;
}

@Override
public void onPageScrollStateChanged(int state) {
}

//当页面开始滑动
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}

/**
* 销毁自动播放子线程
*/

public void onDestroy() {
//当Activity销毁的时候 把是否切换的标记置为false
isSwitchPager = false;
}
}

下面的是布局代码

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white">


<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/c50dp"/>


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:orientation="vertical">


<TextView
android:id="@+id/bannerText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="@dimen/text_size_small"
android:visibility="gone"/>


<LinearLayout
android:id="@+id/dotView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="@dimen/c10dp"
android:gravity="center_horizontal"
android:orientation="horizontal">

</LinearLayout>
</LinearLayout>
</FrameLayout>

上面所用到的指示器圆点

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/shape_dot_enable"
android:state_enabled="true"/>

<item android:drawable="@drawable/shape_dot_normal"
android:state_enabled="false"/>

</selector>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="5dp"/>
<solid android:color="@android:color/white"/
</shape>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="5dp"/>
<solid android:color="@android:color/darker_gray"/>
</shape>

注意:instantiateItem() 进行了判断,destroyItem() 就不用再 removeView() 了,否则会出现空白页。