ViewPager设置广告位,无限滚动
2016-12-05 16:21
253 查看
import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.LinearLayout; import java.util.ArrayList; import java.util.List; public class MainActivity extends Activity { private ViewPager viewpager_1; private LinearLayout dian_ll; private List<ImageView> dianList =new ArrayList<ImageView>(); private int currentPosition = 0;//当前的位置 private final static int AD_START_PLAY=1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); viewpager_1= (ViewPager) findViewById(R.id.viewpager_1); dian_ll= (LinearLayout) findViewById(R.id.dian_ll); viewpager_1.setAdapter(new adapter()); handler.removeMessages(AD_START_PLAY);//清空 handler.sendEmptyMessageDelayed(AD_START_PLAY, 3000);//执行3秒滚动 viewpager_1.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {//手动滑动广告 @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { if (currentPosition !=position){ dianList.get(position).setImageDrawable(getResources().getDrawable(R.mipmap.page_indicator)); dianList.get(currentPosition).setImageDrawable(getResources().getDrawable(R.mipmap.page_indicator_focused)); currentPosition =position; } } @Override public void onPageScrollStateChanged(int state) { } }); setDian(dian_ll,4); } H 4000 andler handler=new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what){ case AD_START_PLAY: int currentItem = viewpager_1.getCurrentItem(); //获取当前页 int totalCount = viewpager_1.getAdapter().getCount();//全部的页数 if (currentItem + 1 >= totalCount) { viewpager_1.setCurrentItem(0, true);//设置当前页 } else { viewpager_1.setCurrentItem(currentItem + 1, true); } handler.sendEmptyMessageDelayed(AD_START_PLAY, 3000); break; } } }; private void setDian(LinearLayout fragment_course_adDots,int num){ fragment_course_adDots.removeAllViews(); for (int i=0;i<num;i++){ ImageView imageView=new ImageView(MainActivity.this); imageView.setLayoutParams(new ViewGroup.LayoutParams( ViewPager.LayoutParams.WRAP_CONTENT,ViewPager.LayoutParams.WRAP_CONTENT)); dianList.add(imageView); if (i==0){ dianList.get(i).setImageDrawable(getResources().getDrawable(R.mipmap.page_indicator)); }else { dianList.get(i).setImageDrawable(getResources().getDrawable(R.mipmap.page_indicator_focused)); } fragment_course_adDots.addView(dianList.get(i)); } } private class adapter extends PagerAdapter { @Override public int getCount() { return 4; } @Override public boolean isViewFromObject(View view, Object object) { return view==object;//要赋值否则图片无法显示 } //赋值图片 @Override public Object instantiateItem(ViewGroup container, int position) { ImageView imageView=new ImageView(getBaseContext()); imageView.setImageResource(R.mipmap.ic_launcher); container.addView(imageView); return imageView; } //要销毁否则报错(Required method destroyItem was not overridden) @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } } }
下载地址:
http://download.csdn.net/detail/u011531708/9702208
相关文章推荐
- 【Android】ViewPager实现无限循环滚动
- android自定义控件-AutoScrollViewpager(无限滚动轮播控件)
- android:ViewPager的无限多页数的设置
- Android中ViewPager支持一屏多个View、切换动画以及无限滚动
- ViewPager的使用和无限滚动的实现
- 458_无限滚动的ViewPager
- ViewPager 的adapter 设置notifyDataSetChanged(),后 ViewPager.setCurrentItem()无效。(页面不滚动)
- ViewPager实现无限循环的广告位
- viewPager滚动速度的设置
- ViewPager:真正实现图片的无限轮播、自定义指示点和条目点击事件(支持长按暂停滚动)
- viewpager无限循环滚动遇到的坑
- Android ViewPager实现无限循环轮播广告位Banner效果
- viewpager实现无限循环滚动幻灯片
- autoscrollviewpager + indicator 无限滚动
- android简单实现无限滚动,自动滚动的ViewPager
- Android简单实现无限滚动自动滚动的ViewPager
- Android ViewPager的无限循环与自动滚动实现
- Android 一个无限循环滚动的卡片式ViewPager
- 与ViewPager合用的MyTitleLayout,可以设置滚动指示器
- ViewPager 无限滚动广告栏