一activity
package com.example.MyViewPaper;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
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.LinearLayout.LayoutParams;
import android.widget.TextView;
public class MainActivity extends Activity {
private ViewPager vp_img;
private TextView tv_des;
private LinearLayout ll_point;
private int[] imgIds;
private ArrayList<ImageView> imgList;
private ImageView imageView;
private ArrayList<View> pointList;
private String[] desStrs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initUI();
initData();
initAdapterAndListener();
}
/**
* 初始化适配器,监听器
*/
private void initAdapterAndListener() {
ll_point.getChildAt(0).setEnabled(true);
myAdapter myAdapter = new myAdapter();
vp_img.setAdapter(myAdapter);
vp_img.setCurrentItem(Integer.MAX_VALUE / 2 - Integer.MAX_VALUE / 2 % 5);
vp_img.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int oldPosition) {
int position = oldPosition % 5;
tv_des.setText(desStrs[position]);
int childCount = ll_point.getChildCount();
for (int i = 0; i < childCount; i++) {
View childAt = ll_point.getChildAt(i);
if (i != position) {
childAt.setEnabled(false);
} else {
childAt.setEnabled(true);
}
}
}
@Override
public void onPageScrolled(int position, float positionOffset,
int positionOffsetPixels) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int state) {
// TODO Auto-generated method stub
}
});
}
/**
* 初始化显示数据
*/
private void initData() {
// 图片ID数组
imgIds = new int[] { R.drawable.a, R.drawable.b, R.drawable.c,
R.drawable.d, R.drawable.e };
desStrs = new String[] { "巩俐不低俗,我就不能低俗", "扑树又回来啦!再唱经典老歌引万人大合唱",
"揭秘北京电影如何升级", "乐视网TV版大派送", "热血屌丝的反杀" };
// 存放图片控件的集合
imgList = new ArrayList<ImageView>();
View pointView = null;
for (int id : imgIds) {
// 设置图片集合
imageView = new ImageView(getApplicationContext());
imageView.setBackgroundResource(id);
imgList.add(imageView);
// 加入小白点
pointView = new View(this);
pointView.setBackgroundResource(R.drawable.selector_bg_point);
// 小白点布局
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(5,
5);
if (id != 0) {
params.leftMargin = 10;
}
pointView.setEnabled(false);
// 小白点加入布局
ll_point.addView(pointView, params);
}
}
/**
* 找到控件
*/
private void initUI() {
vp_img = (ViewPager) findViewById(R.id.vp_img);
tv_des = (TextView) findViewById(R.id.tv_des);
ll_point = (LinearLayout) findViewById(R.id.ll_point);
}
private class myAdapter extends PagerAdapter {
@Override
public int getCount() {
// TODO Auto-generated method stub
return Integer.MAX_VALUE;
}
@Override
public boolean isViewFromObject(View view, Object object) {
// TODO Auto-generated method stub
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = imgList.get(position%5);
container.addView(imageView);
return imageView;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
}
}
二布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:background="#fcc"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="200dp" >
<android.support.v4.view.ViewPager
android:id="@+id/vp_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/a" />
<View
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="#66000000" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:orientation="vertical"
android:gravity="center" >
<TextView
android:id="@+id/tv_des"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:textColor="@android:color/white"
android:text="呵呵哒先生,呵呵哒先生,呵呵哒先生,呵呵哒先生,呵呵哒先生,呵呵哒先生,呵呵哒先生,呵呵哒先生,呵呵哒先生,呵呵哒先生,呵呵哒先生,呵呵哒先生,呵呵哒先生,呵呵哒先生,呵呵哒先生,呵呵哒先生,呵呵哒先生" />
<LinearLayout
android:id="@+id/ll_point"
android:padding="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
三:图片选择器
selector_bg_point.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/shape_bg_point_enable" android:state_enabled="true"/>
<item android:drawable="@drawable/shape_bg_point_disable" android:state_enabled="false"/>
</selector>
shape_bg_point_disable.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<corners android:radius="5dp" />
<solid android:color="@android:color/darker_gray" />
</shape>
shape_bg_point_enable.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<corners android:radius="5dp" />
<solid android:color="#FFFFFF" />
</shape>