<--- 主Activity类
package ht.testxyapp.com.testapp.activity; import android.os.SystemClock; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import java.util.ArrayList; import java.util.List; import ht.testxyapp.com.testapp.R; import ht.testxyapp.com.testapp.adapter.BannerAdapter; public class BannerTestActivity extends AppCompatActivity { private ViewPager viewPager; private LinearLayout dots; private List<ImageView> imageViews; private int[] images = {R.drawable.yu_01,R.drawable.yu_02,R.drawable.yu_03,R.drawable.yu_04,R.drawable.yu_05,R.drawable.yu_06,}; private BannerAdapter adapter; private int dotIndex = 0; //线程标识 private boolean isStop = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_banner_test); viewPager = (ViewPager) findViewById(R.id.viewPager); dots = (LinearLayout) findViewById(R.id.dots); imageViews = new ArrayList<>(); View view; LinearLayout.LayoutParams params; for (int i = 0; i < images.length; i++){ // 设置广告图 ImageView imageView = new ImageView(BannerTestActivity.this); imageView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)); imageView.setBackgroundResource(images[i]); imageViews.add(imageView); //设置圆点 view = new View(BannerTestActivity.this); params = new LinearLayout.LayoutParams(20,20); params.leftMargin = 10; view.setBackgroundResource(R.drawable.pots_drawable); view.setLayoutParams(params); view.setEnabled(false); dots.addView(view); } adapter = new BannerAdapter(imageViews); viewPager.setAdapter(adapter); viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { int newPosition = position % images.length; //texts.setText(txts[newPosition]); dots.getChildAt(newPosition).setEnabled(true); dots.getChildAt(dotIndex).setEnabled(false); // 更新标志位 dotIndex = newPosition; } @Override public void onPageScrollStateChanged(int state) { } }); int index = (Integer.MAX_VALUE / 2) - (Integer.MAX_VALUE / 2 % imageViews.size()); viewPager.setCurrentItem(index); dots.getChildAt(dotIndex).setEnabled(true); //开启新线程 2秒更新一次 new Thread(new Runnable() { @Override public void run() { while(!isStop){ SystemClock.sleep(2000); runOnUiThread(new Runnable() { @Override public void run() { viewPager.setCurrentItem(viewPager.getCurrentItem() + 1); } }); } } }).start(); } }
< ---- 主布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_banner_test" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="ht.testxyapp.com.testapp.activity.BannerTestActivity"> <android.support.v4.view.ViewPager android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="200dp"></android.support.v4.view.ViewPager> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignBottom="@+id/viewPager" android:gravity="center" android:orientation="vertical"> <LinearLayout android:id="@+id/dots" android:layout_gravity="center" android:gravity="center" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"></LinearLayout> </LinearLayout> </RelativeLayout>
<-- Adapter
package ht.testxyapp.com.testapp.adapter; import android.support.v4.view.PagerAdapter; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import java.util.ArrayList; import java.util.List; /** * Created by Administrator on 2016/11/4/004. */ public class BannerAdapter extends PagerAdapter{ private List<ImageView> imageViews = new ArrayList<>(); public BannerAdapter (List<ImageView> images){ this.imageViews = images; } @Override public int getCount() { // 去超大的数 实现无限循环 return Integer.MAX_VALUE; } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } @Override public Object instantiateItem(ViewGroup container, int position) { container.addView(imageViews.get(position % imageViews.size())); return imageViews.get(position % imageViews.size()); } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView(imageViews.get(position % imageViews.size())); } }