效果图
代码如下
ViewPageActivity
package com.yq1012.youku; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.OnPageChangeListener; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; public class ViewPageActivity extends Activity { private ViewPager viewPager; private LinearLayout pointGroup;// 点 private TextView msg;// 图片说明 private List<ImageView> imageList; private final int[] imageIds = { R.drawable.a, R.drawable.b, R.drawable.c, R.drawable.d, R.drawable.e }; private final String[] msgs = { "你猜我是男是女", "看看是啥", "粮油以及", "主管不给力", "北京了我要打酱油了了" }; protected int lastPosition = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_viewpage); viewPager = (ViewPager) findViewById(R.id.viewpager); pointGroup = (LinearLayout) findViewById(R.id.point_group); msg = (TextView) findViewById(R.id.msg); msg.setText(msgs[0]); imageList = new ArrayList<ImageView>(); for (int i = 0; i < imageIds.length; i++) { ImageView image = new ImageView(this); image.setBackgroundResource(imageIds[i]); imageList.add(image); ImageView point = new ImageView(this); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(5, 5); params.rightMargin = 20; point.setLayoutParams(params); point.setBackgroundResource(R.drawable.point_bg); if (i == 0) { point.setEnabled(true); } else { point.setEnabled(false); } pointGroup.addView(point); } viewPager.setAdapter(new MyPagerAdapter()); viewPager.setOnPageChangeListener(new OnPageChangeListener() { @Override public void onPageSelected(int position) { // 页面切换后的调用 。。 // 新的页面位置 position = position % imageList.size(); msg.setText(msgs[position]); // 把 当前点设置城 true pointGroup.getChildAt(position).setEnabled(true); pointGroup.getChildAt(lastPosition).setEnabled(false); lastPosition = position; } @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { // 页面正在滑动 } @Override public void onPageScrollStateChanged(int position) { // 页面在正在 发生变化 } }); // 自动循环 /* * 1.定时器 2. 开子线程 while true 循环 3.colckManager 4. handle 发送延时信息实现循环 */ isRunning = true; handler.sendEmptyMessageDelayed(0, 2000); } @Override protected void onDestroy() { isRunning = false; super.onDestroy(); } private boolean isRunning = false;// 判断是否自动滚动 private Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { viewPager.setCurrentItem(viewPager.getCurrentItem() + 1); if (isRunning) { handler.sendEmptyMessageDelayed(0, 2000); } }; }; private class MyPagerAdapter extends PagerAdapter { // viewpager 成员可以不同 无法 设置同一个 复用 @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); object = null; } @Override public Object instantiateItem(ViewGroup container, int position) { // 获得 响应位置的view // container view的容器 其实就是viewpager自身 // position 位置(不是当前位置) container.addView(imageList.get(position % imageList.size())); return imageList.get(position % imageList.size()); } @Override public int getCount() { // 总页面数 // return imageList.size(); return Integer.MAX_VALUE; } @Override public boolean isViewFromObject(View view, Object object) { // 和instantiateItem 方法对应 return view == object; } } }
界面
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.yq1012.youku.MainActivity$PlaceholderFragment" > <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="wrap_content" android:layout_height="200dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignBottom="@id/viewpager" android:background="#33000000" android:orientation="vertical" > <TextView android:id="@+id/msg" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="你看我是男是呢" android:textColor="@android:color/white" android:textSize="22sp" /> <LinearLayout android:id="@+id/point_group" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:orientation="horizontal" > </LinearLayout> </LinearLayout> </RelativeLayout>
注意点 。。 导 的第三方宝需要注意