主代码:
public class Showactivity extends Activity implements OnPageChangeListener { private List<Jsonimage> listimage; private ViewPager vpager; private LinearLayout llayout; private int count = 0; private BitmapUtils bitmapUtils; private List<ImageView> imagelist; private int lastindex = 0; @SuppressLint("HandlerLeak") Handler h = new Handler() { @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub vpager.setCurrentItem(count); } }; @SuppressWarnings("unchecked") @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.showactivity); vpager = (ViewPager) findViewById(R.id.viewpager); llayout = (LinearLayout) findViewById(R.id.ll_layout); bitmapUtils = new BitmapUtils(getApplicationContext()); imagelist = new ArrayList<ImageView>(); Intent intent = getIntent(); listimage = (List<Jsonimage>) intent.getSerializableExtra("ls"); // Log.i("TAG", listimage.toString()); for (int i = 0; i < listimage.size(); i++) { ImageView imageView = new ImageView(getApplicationContext()); bitmapUtils.display(imageView, listimage.get(i).getHead_img()); imagelist.add(imageView); ImageView imageView2 = new ImageView(getApplicationContext()); imageView2.setBackgroundResource(R.drawable.shape); LayoutParams layoutParams = new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); layoutParams.leftMargin = 15; imageView2.setLayoutParams(layoutParams); llayout.addView(imageView2); if (i == 0) { imageView2.setEnabled(true); } else { imageView2.setEnabled(false); } } vpager.setAdapter(new Mypageadapter(getApplicationContext(), imagelist)); Timer t = new Timer(); t.schedule(new TimerTask() { @Override public void run() { // TODO Auto-generated method stub count++; if (count == listimage.size()) { count = 0; } h.sendEmptyMessage(0); } }, 1000, 1000); vpager.setOnPageChangeListener(this); } @Override public void onPageScrollStateChanged(int arg0) { // TODO Auto-generated method stub } @Override public void onPageScrolled(int arg0, float arg1, int arg2) { // TODO Auto-generated method stub } @Override public void onPageSelected(int arg0) { // TODO Auto-generated method stub int index = arg0 % imagelist.size(); // 将当前指示灯改为true llayout.getChildAt(index).setEnabled(true); llayout.getChildAt(lastindex).setEnabled(false); lastindex = index; } }
自定义PagerAdapter:
public class Mypageadapter extends PagerAdapter { Context context; List<ImageView> list; public Mypageadapter(Context context, List<ImageView> list) { super(); this.context = context; this.list = list; } // 得到图片的数量 @Override public int getCount() { // TODO Auto-generated method stub return list.size(); } @Override public boolean isViewFromObject(View arg0, Object arg1) { // TODO Auto-generated method stub return (arg0 == arg1); } // 将图片添加到View @Override public Object instantiateItem(ViewGroup container, int position) { container.addView(list.get(position)); return list.get(position); } // 销毁 @Override public void destroyItem(ViewGroup container, int position, Object object) { // 销毁对应位置上的Object // super.destroyItem(container, position, object); container.removeView(list.get(position)); } }
在res中创建drawble文件
dot_focus.xml <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval"> <size android:width="10dp" android:height="10dp"/> <solid android:color="#FF0000"/> </shape> dot_nomal.xml <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" > <size android:height="10dp" android:width="10dp" /> <solid android:color="#808080" /> </shape> shape.xml <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/dot_focus" android:state_enabled="true"></item> <item android:drawable="@drawable/dot_nomal" android:state_enabled="false"></item> </selector>
XML布局:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:id="@+id/ll_layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_margin="5dp" > </LinearLayout> <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@id/ll_layout" > </android.support.v4.view.ViewPager> </RelativeLayout>