安卓广告栏的自动轮播加指示器的使用

自动滚动广告使用

添加依赖:

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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值