实现原理:使用ViewPager每个Pager就是一个ImageView,里面只有一张图片;
整个布局是一个相对布局,下面标题和指示点是一个线性布局;
给ViewPager设置一个页面改变的监听,当页面改变的时候让标题根据position改变标题内容;
指示点同原理;
首先是布局:
<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="230dp" tools:context="com.example.a.myviewpager.MainActivity"> <android.support.v4.view.ViewPager android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="match_parent" /> <LinearLayout android:layout_alignParentBottom="true" android:layout_width="match_parent" android:layout_height="45dp" android:background="#80000000" android:orientation="vertical" android:gravity="center"> <TextView android:gravity="center" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/textView" android:text="新浪微博:搏击爱好者" android:textColor="#ffffff" /> <LinearLayout android:gravity="center" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/ll" android:orientation="horizontal"> </LinearLayout> </LinearLayout> </RelativeLayout>
private ViewPager mViewPager; private TextView mTextView; private LinearLayout ll; //图片的id private int[] imageIds={R.mipmap.a,R.mipmap.b,R.mipmap.c,R.mipmap.d,R.mipmap.e}; // 图片对应的标题 private final String[] imageTitleTexts = { "天生一对++新浪微博:搏击爱好者Z", "扑树又回来啦++新浪微博:搏击爱好者Z", "刘德华++新浪微博:搏击爱好者Z", "源代码++新浪微博:搏击爱好者Z", "饭局++新浪微博:搏击爱好者Z" }; //是否开始自动滑动 private boolean isRunning=false; private int lastPoint=0; private List<ImageView> imageViewList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initViewData(); } private void initViewData() { mViewPager= (ViewPager) findViewById(R.id.viewPager); mTextView= (TextView) findViewById(R.id.textView); ll= (LinearLayout) findViewById(R.id.ll); mTextView.setText(imageTitleTexts[0]); imageViewList=new ArrayList<ImageView>(); for (int x=0 ; x<imageIds.length ; x++ ){ ImageView imageView = new ImageView(this); imageView.setBackgroundResource(imageIds[x]); imageViewList.add(imageView); //添加指示点 ImageView ivPoint=new ImageView(this); if(x==0){ ivPoint.setBackgroundResource(R.drawable.point_select); }else { ivPoint.setBackgroundResource(R.drawable.point_no); } //给指示点设置间距 LinearLayout.LayoutParams layoutParams =new LinearLayout.LayoutParams(-2, -2); layoutParams.leftMargin=20; ll.addView(ivPoint,layoutParams); } adapter=new MyViewPagerAdapter(); mViewPager.setAdapter(adapter); mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { /** * 当页面发生改变时调用 * @param position */ @Override public void onPageSelected(int position) { //同样道理和instantiateItem()一样 position=position % imageIds.length; mTextView.setText(imageTitleTexts[position]); //让指示点给着变化 ll.getChildAt(lastPoint).setBackgroundResource(R.drawable.point_no); ll.getChildAt(position).setBackgroundResource(R.drawable.point_select); lastPoint=position; } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageScrollStateChanged(int state) { } }); //这个必须设置,用最大数除2是得到中间数,这样无论向左还是向右都可以一直滑动; //如果设置为Integer.MAX_VALUE/2 可能打开activity轮播图不是从第一张开始;所以需要减去余数 mViewPager.setCurrentItem(Integer.MAX_VALUE/2 - Integer.MAX_VALUE % imageIds.length); isRunning=true; handler.sendEmptyMessageDelayed(99,2000); } private Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { if(isRunning==true){ mViewPager.setCurrentItem(mViewPager.getCurrentItem()+1); handler.sendEmptyMessageDelayed(99,2000); } } }; private MyViewPagerAdapter adapter; class MyViewPagerAdapter extends PagerAdapter{ @Override public int getCount() { //为了让viewpager可以一直向两边滑动,这里设置一个最大值; return Integer.MAX_VALUE; } @Override public Object instantiateItem(ViewGroup container, int position) { //当上面的return改变为最大数的时候,这里的position就是最大数了,所以必须取余数,不然就会报错 position=position % imageViewList.size(); ImageView iv=imageViewList.get(position); container.addView(iv); return iv; } @Override public boolean isViewFromObject(View view, Object object) { return view==object; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } } @Override protected void onDestroy() { isRunning=false; super.onDestroy(); }
源码下载地址:
http://download.csdn.net/detail/zheng_jiao/9510699