package com.example.qqviewpager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
public class MainActivity extends Activity implements OnClickListener {
private ImageView mImageView1, mImageView2, mImageView3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageView1 = (ImageView) this.findViewById(R.id.iv_image1);
mImageView2 = (ImageView) this.findViewById(R.id.iv_image2);
mImageView3 = (ImageView) this.findViewById(R.id.iv_image3);
mImageView1.setOnClickListener(this);
mImageView2.setOnClickListener(this);
mImageView3.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// switch (v.getId()) {
// case R.id.iv_image1:
//
// break;
// case R.id.iv_image2:
//
// break;
// case R.id.iv_image3:
//
// break;
//
// default:
// break;
// }
Intent mIntent = new Intent();
mIntent.setClass(this, ViewpagerDemo.class);
this.startActivity(mIntent);
}
}
//这个是重点
package com.example.qqviewpager;
import java.util.ArrayList;
import java.util.List;
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.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
public class ViewpagerDemo extends Activity implements OnPageChangeListener {
private ViewPager viewPager;
private List<ImageView> listviews;
private int[] pics = { R.drawable.a0, R.drawable.a1, R.drawable.a2};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewpager);
viewPager = (ViewPager) findViewById(R.id.vp_test);
listviews = new ArrayList<ImageView>();
// 添加viewpager多出的两个view
int length = pics.length + 2;
for (int i = 0; i < length; i++) {
ImageView imageView = new ImageView(ViewpagerDemo.this);
ViewGroup.LayoutParams viewPagerImageViewParams = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT);
imageView.setLayoutParams(viewPagerImageViewParams);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
listviews.add(imageView);
imageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ViewpagerDemo.this.finish();
}
});
}
viewPager.setAdapter(new ViewPagerAdapter());
viewPager.setOnPageChangeListener(ViewpagerDemo.this);
// 设置viewpager在第二个视图显示
viewPager.setCurrentItem(1);
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageSelected(int i) {
int pageIndex = i;
if (i == 0) {
// 当视图在第一个时,将页面号设置为图片的最后一张。
pageIndex = pics.length;
} else if (i == pics.length + 1) {
// 当视图在最后一个是,将页面号设置为图片的第一张。
pageIndex = 1;
}
if (i != pageIndex) {
viewPager.setCurrentItem(pageIndex, false);
return;
}
}
class ViewPagerAdapter extends PagerAdapter {
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
ImageView view = listviews.get(position);
container.removeView(view);
view.setImageBitmap(null);
}
@Override
public Object instantiateItem(ViewGroup container, int i) {
if (i == 0) {
listviews.get(i).setImageResource(pics[pics.length-1]);
} else if (i == (listviews.size() - 1)) {
listviews.get(i).setImageResource(pics[0]);
} else {
listviews.get(i).setImageResource(pics[i - 1]);
}
container.addView(listviews.get(i));
return listviews.get(i);
}
@Override
public int getCount() {
return listviews.size();
}
@Override
public boolean isViewFromObject(View view, Object o) {
return view == o;
}
}
}
//布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="测试有声资讯的viewpager" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="432dp" >
<ImageView
android:id="@+id/iv_image1"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_margin="4dip"
android:layout_weight="1.0"
android:paddingLeft="4dip"
android:scaleType="fitXY"
android:src="@drawable/a" />
<ImageView
android:id="@+id/iv_image2"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_margin="4dip"
android:layout_weight="1.0"
android:scaleType="fitXY"
android:src="@drawable/b" />
<ImageView
android:id="@+id/iv_image3"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_margin="4dip"
android:layout_weight="1.0"
android:paddingRight="4dip"
android:scaleType="fitXY"
android:src="@drawable/c" />
</LinearLayout>
</LinearLayout>
//viewpager布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/vp_test"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>