activity_main
<?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:layout_width="match_parent" android:layout_height="match_parent" > <android.support.v4.view.ViewPager android:id="@+id/vp" android:layout_width="match_parent" android:layout_height="200dp"> </android.support.v4.view.ViewPager> <LinearLayout android:id="@+id/lan" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="10dp" android:gravity="center" android:layout_alignBottom="@+id/vp" android:layout_marginBottom="20dp" android:layout_centerHorizontal="true"></LinearLayout> </RelativeLayout><?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/im" android:src="@mipmap/ic_launcher" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_alignParentTop="true" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="title" android:id="@+id/title" android:layout_marginBottom="80dp" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginLeft="52dp" android:layout_marginStart="52dp" /> </RelativeLayout>package fengras.com.xiaoyuandian; import android.os.Bundle; import android.os.Handler; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.LinearLayout; import java.util.ArrayList; import java.util.List; import fengras.com.xiaoyuandian.bean.MyPageradapter; import fengras.com.xiaoyuandian.bean.Mynews; public class MainActivity extends AppCompatActivity { private ViewPager vp; private List<Mynews> list=new ArrayList<>(); private LinearLayout lean; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); vp = (ViewPager) findViewById(R.id.vp); lean = (LinearLayout) findViewById(R.id.lan); initdata(); startSwitch(); } private void initdata() { list.add(new Mynews("张三",R.mipmap.ic_launcher)); list.add(new Mynews("李四",R.mipmap.ic_launcher)); list.add(new Mynews("王五",R.mipmap.ic_launcher)); MyPageradapter mm=new MyPageradapter(list,MainActivity.this); vp.setAdapter(mm); initdatos(); vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { int count = lean.getChildCount(); for (int i = 0; i < count; i++) { View view = lean.getChildAt(i); //页面改变时选中小圆点 view.setEnabled(i == position ? false : true); } //页面改变时选中文字 // tv_info.setText(infos.get(position)); } @Override public void onPageSelected(int position) { } @Override public void onPageScrollStateChanged(int state) { } }); } private void initdatos() { // ll_point.removeAllViews(); for (int i = 0; i < list.size(); i++) { // Toast.makeText(MainActivity.this, "图片个数:"+images.size(), Toast.LENGTH_SHORT).show(); //创建小圆点控件 View view = new View(this); //设置背景选择器 view.setBackgroundResource(R.drawable.yuan); //创建小圆点大小 LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(20, 20); //创建小圆点间距 params.rightMargin = 10; //把小圆点添加容器 lean.addView(view, params); view.setTag(i); } //设置第一个为默认选择 View view = lean.getChildAt(0); view.setEnabled(false); //设置第一个文字为默认选择 //tv_info.setText(infos.get(0)); } private Handler handler = new Handler(); //开启 public void startSwitch() { handler.postDelayed(new Runnable() { @Override public void run() { int item = vp.getCurrentItem(); if (item == vp.getAdapter().getCount() - 1) { item = 0; } else { item++; } vp.setCurrentItem(item); handler.postDelayed(this, 2000); } }, 2000); } }package fengras.com.xiaoyuandian.bean; import android.content.Context; import android.support.v4.view.PagerAdapter; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import java.util.List; import fengras.com.xiaoyuandian.R; /** * Created by Administrator on 2017/3/28. */ public class MyPageradapter extends PagerAdapter { private List<Mynews> list; private Context context; public MyPageradapter(List<Mynews> list, Context context) { this.list = list; this.context = context; } @Override public int getCount() { return list.size(); } @Override public boolean isViewFromObject(View view, Object object) { return view==object; } @Override public Object instantiateItem(ViewGroup container, int position) { View view=View.inflate(context,R.layout.listview,null); ImageView imageView= (ImageView) view.findViewById(R.id.im); imageView.setImageResource(list.get(position).getImage()); TextView t1= (TextView) view.findViewById(R.id.title); t1.setText(list.get(position).getName()); container.addView(view); return view; } @Override public void destroyItem(ViewGroup container, int position, Object object) { // super.destroyItem(container, position, object); container.removeView((View) object); } }