本文接着实现轮播图广告的监听滚动
本文地址:http://www.cnblogs.com/wuyudong/p/5920757.html,转载请注明源地址。
首先添加布局文件,实现小白点
shape_bg_point_enable.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" > <corners android:radius="5dp" /> <solid android:color="@android:color/white" /> </shape>
shape_bg_point_disable.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" > <corners android:radius="5dp" /> <solid android:color="@android:color/darker_gray" /> </shape>
再添加一个selector
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_enabled="true" android:drawable="@drawable/shape_bg_point_enable"></item> <item android:state_enabled="false" android:drawable="@drawable/shape_bg_point_disable"></item> </selector>
将selector添加到视图:
for (int i = 0; i < imageResIds.length; i++) { imageView = new ImageView(this); imageView.setBackgroundResource(imageResIds[i]); imageViewList.add(imageView); // 加小白点,指示器 pointView = new View(this); pointView.setBackgroundResource(R.drawable.selector_bg_point); layoutParams = new LayoutParams(7, 7); if (i != 0) { layoutParams.leftMargin = 10; } pointView.setEnabled(false); ll_point_container.addView(pointView, layoutParams); }
为ViewPager添加滑动监听
public class MainActivity extends Activity implements OnPageChangeListener ...... @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { // 滚动时调用 } @Override public void onPageSelected(int position) { // 新条目被选中时调用 position = position % 5; tv_desc.setText(contentDesc[position]); // 把之前的禁用,把最新的启用 ll_point_container.getChildAt(lastEnablePoint).setEnabled(false); ll_point_container.getChildAt(position).setEnabled(true); lastEnablePoint = position; } @Override public void onPageScrollStateChanged(int state) { // 滚动状态变化时调用 }
开启页面自动轮询:
//开启轮询 new Thread() { public void run() { while(isRunning) { try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } //往下跳一位 runOnUiThread(new Runnable() { @Override public void run() { viewPager.setCurrentItem(viewPager.getCurrentItem() + 1); } }); } }; }.start();
完整代码如下:
package com.wuyudong.viewpager; import java.util.ArrayList; import android.R.layout; import android.os.Bundle; import android.app.Activity; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.view.Menu; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.LinearLayout.LayoutParams; import android.widget.TextView; public class MainActivity extends Activity implements OnPageChangeListener { private ViewPager viewPager; private int[] imageResIds; private ArrayList<ImageView> imageViewList; private ArrayList<View> pointViews; private LinearLayout ll_point_container; private String[] contentDesc; private TextView tv_desc; private int lastEnablePoint = 0; private boolean isRunning = true; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 初始化布局 View 视图 initViews(); // Model 数据 initData(); // Controller 控制器 initAdapter(); //开启轮询 new Thread() { public void run() { while(isRunning) { try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } //往下跳一位 runOnUiThread(new Runnable() { @Override public void run() { viewPager.setCurrentItem(viewPager.getCurrentItem() + 1); } }); } }; }.start(); } @Override protected void onDestroy() { super.onDestroy(); isRunning = false; } private void initViews() { viewPager = (ViewPager) findViewById(R.id.viewpager); viewPager.setOnPageChangeListener(this);// 设置滚动更新 tv_desc = (TextView) findViewById(R.id.tv_desc); ll_point_container = (LinearLayout) findViewById(R.id.ll_point_container); } private void initAdapter() { ll_point_container.getChildAt(0).setEnabled(true); tv_desc.setText(contentDesc[0]); // 设置适配器 viewPager.setAdapter(new MyAdapter()); //默认设置到中间的某个位置 int pos = Integer.MAX_VALUE / 2; //viewPager.setCurrentItem(pos - 3); viewPager.setCurrentItem(5000000); } private void initData() { // 初始化要显示的数据 // 图片资源id数组 imageResIds = new int[] { R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e }; // 文本描述 contentDesc = new String[] { "巩俐不低俗,我就不能低俗", "朴树又回来啦!再唱经典老歌引万人大合唱", "揭秘北京电影如何升级", "乐视网TV版大派送", "热血屌丝的反杀" }; imageViewList = new ArrayList<ImageView>(); pointViews = new ArrayList<View>(); ImageView imageView; View pointView; LayoutParams layoutParams; for (int i = 0; i < imageResIds.length; i++) { imageView = new ImageView(this); imageView.setBackgroundResource(imageResIds[i]); imageViewList.add(imageView); // 加小白点,指示器 pointView = new View(this); pointView.setBackgroundResource(R.drawable.selector_bg_point); layoutParams = new LayoutParams(7, 7); if (i != 0) { layoutParams.leftMargin = 10; } pointView.setEnabled(false); ll_point_container.addView(pointView, layoutParams); } } class MyAdapter extends PagerAdapter { @Override public int getCount() { return Integer.MAX_VALUE; } // 3、指定复用的判断逻辑 @Override public boolean isViewFromObject(View view, Object object) { // 当滑到新的条目,又返回来,view是否可以被复用 return view == object; } // 1、返回要显示的条目内容,创建条目 @Override public Object instantiateItem(ViewGroup container, int position) { // container:容器:ViewPager // position:当前要显示的条目的位置 position = position % 5; ImageView imageView = imageViewList.get(position); System.out.println("instantItem创建:" + position); // a、把view对象添加到container中 container.addView(imageView); // b、把view对象返回给框架,适配器 return imageView; // 必须要重写,否则抛异常 } // 2、销毁条目 @Override public void destroyItem(ViewGroup container, int position, Object object) { // object 要销毁的对象 System.out.println("destroyItem销毁:" + position); container.removeView((View) object); } } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { // 滚动时调用 } @Override public void onPageSelected(int position) { // 新条目被选中时调用 position = position % 5; tv_desc.setText(contentDesc[position]); // 把之前的禁用,把最新的启用 ll_point_container.getChildAt(lastEnablePoint).setEnabled(false); ll_point_container.getChildAt(position).setEnabled(true); lastEnablePoint = position; } @Override public void onPageScrollStateChanged(int state) { // 滚动状态变化时调用 } }