自动滚动广告使用
添加依赖:
compile('cn.trinea.android.view.autoscrollviewpager:android-auto-scroll-view-pager:1.1.2') {
exclude module: 'support-v4'
}
布局使用:
<cn.trinea.android.view.autoscrollviewpager.AutoScrollViewPager
android:id="@+id/ad_vp"
android:layout_width="match_parent"
android:layout_height="match_parent" />
代码使用:
//广告栏
mAdVp = (AutoScrollViewPager) view.findViewById(R.id.ad_vp);
mHomePagerAdapter = new HomePagerAdapter(getActivity());
mAdVp.setAdapter(mHomePagerAdapter);
mHomePagerAdapter.notifyDataSetChanged();
//设置一个监听器
mAdVp.addOnPageChangeListener(new JDPagerChangeListener(){
@Override
public void onPageSelected(int position) {
//动态改变指示器显示
changeAdIndicator(position);
}
});
//广告指示器
mAdIndicator = (LinearLayout) view.findViewById(R.id.ad_indicator);
//开始滚动
mAdVp.startAutoScroll();
//添加一个指示器
initAdBannerIndicator(data.size());
//广告栏指示器
private void initAdBannerIndicator(int size) {
for (int i = 0; i<size; i++){
ImageView view = new ImageView(getActivity());
//设置宽高
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(15,15);
//设置距离
params.setMargins(20,0,0,0);
view.setLayoutParams(params);
//设置背景
view.setBackgroundResource(R.drawable.ad_indicator_bg);
//添加到布局中
mAdIndicator.addView(view);
}
//默认选中第一个
changeAdIndicator(0);
}
//修改指示器显示
private void changeAdIndicator(int postion) {
//获取有多少个子空间
int childCount = mAdIndicator.getChildCount();
for (int i = 0; i< childCount; i++){
//获取到里面控件判断是否与传入的值一样
mAdIndicator.getChildAt(i).setSelected( i == postion);
}
}
选择器的使用;
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true">
<shape android:shape="oval" android:useLevel="false">
<solid android:color="@android:color/white" />
<stroke android:width="1dp" android:color="@android:color/white" />
</shape>
</item>
<item>
<shape android:shape="oval" android:useLevel="false">
<solid android:color="#3FFF" />
<stroke android:width="2dp" android:color="@android:color/white" />
</shape>
</item>
</selector>