博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ViewPager Banner(广告墙)
阅读量:5906 次
发布时间:2019-06-19

本文共 7062 字,大约阅读时间需要 23 分钟。

hot3.png

layout.xml

java.class

public class MainActivity extends Activity{    private static String TAG = "MainActivity";    /**     * Viewpager对象     */    private ViewPager mViewPager;        /**     * Viewpager的适配器     */    private ViewPagerAdapter mVpAdapter;    /**     * 存放view的列表     */    private List
mViews; /** * 底部小点图片 */ private ImageView[] dots; /** * 记录当前view的位置 */ private int currentIndex; /** * 记录当前自动滑动的状态,true就滑动,false停止滑动 */ private boolean isContinue = true; private Handler mHandler; private Timer timer; private static boolean isSleep = true; /** * 设置viewpager的初始页面 */ private static final int initPositon = 50000; /** * viewpager的当前页面 */ private static int currentPosition = initPositon; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mViews = new ArrayList
(); LinearLayout.LayoutParams mParams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT); Resources res = getResources(); for (int i = 0; i < Images.mPics.length; i++) { ImageView mImageView = new ImageView(this); mImageView.setLayoutParams(mParams); Bitmap bitmap = BitmapFactory.decodeResource(res, Images.mPics[i]); mImageView.setImageBitmap(bitmap); mViews.add(mImageView); } // Handler对象更新UI mHandler = new Handler() { public void handleMessage(Message msg) { mViewPager.setCurrentItem(currentPosition); } }; //启动线程,监控是否要自动滑动 timer = new Timer(); timer.schedule(new TimerTask() { public void run() { while (true) { if (isContinue) { /* if (isSleep) { sleep(4000); } */ currentPosition ++; mHandler.sendEmptyMessage(0); sleep(3000); /* if (!isSleep) { sleep(3000); } isSleep = false;*/ } } } }, 4000); mViewPager = (ViewPager) findViewById(R.id.viewpager); mVpAdapter = new ViewPagerAdapter(mViews); mViewPager.setAdapter(mVpAdapter); mViewPager.setCurrentItem(initPositon); mViewPager.setOnPageChangeListener(new MyPageChangeListener()); mViewPager.setOnTouchListener(new MyTouchListener()); initDots(); } /** * 底部圆点初始化 */ private void initDots() { LinearLayout mLinearLayout = (LinearLayout) findViewById(R.id.llyt_welcome_dot); dots = new ImageView[Images.mPics.length]; for (int i = 0; i < Images.mPics.length; i++) { dots[i] = (ImageView) mLinearLayout.getChildAt(i); // 设置为灰色 dots[i].setEnabled(true); // 为每一个imageview添加点击监听 // dots[i].setOnClickListener(new MyOnClickListener()); // 设置位置tag,方面取出与当前位置对应 dots[i].setTag(i); } currentIndex = 0; //设置第一个圆点高亮 dots[currentIndex].setEnabled(false); } /** * 设置底部圆点的高亮 * @param position * 将要显示的view的位置 */ private void setCurrentDot(int position) { if (position < 0 || position > Images.mPics.length - 1) { return; } dots[position].setEnabled(false); dots[currentIndex].setEnabled(true); currentIndex = position; } /* * class MyOnClickListener implements OnClickListener{ * * @Override public void onClick(View v) { int position = (Integer) * v.getTag(); mViewPager.setCurrentItem(position); } * * } */ /** * viewpager页面变化的监听器 * @author user * */ class MyPageChangeListener implements OnPageChangeListener { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { // TODO Auto-generated method stub } @Override public void onPageSelected(int position) { setCurrentDot(position % mViews.size()); currentPosition = position; } @Override public void onPageScrollStateChanged(int state) { // TODO Auto-generated method stub } } /** * 监听手势监听器 * @author user * */ class MyTouchListener implements OnTouchListener { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_MOVE: isContinue = false; break; case MotionEvent.ACTION_UP: default: isContinue = true; isSleep = true; break; } return false; } } /** * 设置线程间隔 */ private void sleep(long time){ try { Thread.sleep(time); } catch (Exception e) { e.printStackTrace(); } }}
adapter.class
public class ViewPagerAdapter extends PagerAdapter{        private static String TAG = "ViewPagerAdapter";    /**     * 界面列表     */    private List
mViews; public ViewPagerAdapter(List
mViews) { this.mViews = mViews; } /** * 获取当前页面数 */ @Override public int getCount() { // Log.v(TAG, "getCount" + mViews.size()); return Integer.MAX_VALUE; } @Override public boolean isViewFromObject(View view, Object object) { // Log.v(TAG, "isViewFromObject" + (view == object)); return view == object; } /** * 适配器给container容器添加视图 */ @Override public Object instantiateItem(ViewGroup container, int position) { Log.v(TAG, "instantiateItem" + position); position = position % mViews.size(); container.addView(mViews.get(position), 0); return mViews.get(position); } /** * 适配器移除container容器中的视图 */ @Override public void destroyItem(ViewGroup container, int position, Object object) { Log.v(TAG, "destroyItem" + position); position = position % mViews.size(); container.removeView(mViews.get(position)); } }

转载于:https://my.oschina.net/oppo4545/blog/200732

你可能感兴趣的文章
微信小程序记账应用实例课程(三)服务端实现账目CRUD
查看>>
关于二战
查看>>
区块链艺术
查看>>
powerdesigner在导出数据库报表文件时无语言可选,解决"You must choose a valid language”...
查看>>
mysql数据库备份脚本
查看>>
Flutter原理与美团的实践
查看>>
Oracle归档日志满了导致Oracle连接(ORA-00257)报错处理
查看>>
maven.bat
查看>>
Redis源码分析系列十四:processInputBuffer
查看>>
ES权威指南[官方文档学习笔记]-32 Checking whether a document XX
查看>>
DS4000系列存储配置storage manager介绍
查看>>
JPetStore Demo示例改进和讲解-轻量级J2EE技术框架应用(续)
查看>>
docker-compose使用
查看>>
apache使用.htaccess防图片盗链
查看>>
git删除中间某个commit
查看>>
gitlab的安装
查看>>
Linux下查看软、硬raid信息的方法
查看>>
getCurrentSession() 和getSession()的联系
查看>>
Android:Activity(四):Activity生命周期
查看>>
js生效后页面自动刷新的问题(js控制button,生效效果闪一下消失)解决
查看>>