public class MainActivity extends Activity {
private ViewPager viewPager;
// 加载显示内容
private List<View> content;
// 加载显示标题
private List<String> title;
private LayoutInflater inflater;
private String[] items = {
"http://lh5.ggpht.com/_mrb7w4gF8Ds/TCpetKSqM1I/AAAAAAAAD2c/Qef6Gsqf12Y/s144-c/_DSC4374%20copy.jpg",
"http://lh5.ggpht.com/_Z6tbBnE-swM/TB0CryLkiLI/AAAAAAAAVSo/n6B78hsDUz4/s144-c/_DSC3454.jpg",
"http://lh3.ggpht.com/_GEnSvSHk4iE/TDSfmyCfn0I/AAAAAAAAF8Y/cqmhEoxbwys/s144-c/_MG_3675.jpg",
"http://lh6.ggpht.com/_Nsxc889y6hY/TBp7jfx-cgI/AAAAAAAAHAg/Rr7jX44r2Gc/s144-c/IMGP9775a.jpg" };
private MyPagerAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) this.findViewById(R.id.viewpager);
inflater = LayoutInflater.from(this);
content = new ArrayList<View>();
title = new ArrayList<String>();
for (int i = 1; i <= 4; i++) {
View view = inflater.inflate(R.layout.item, null);
content.add(view);
title.add("图片集" + i);
}
adapter = new MyPagerAdapter();
viewPager.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
public class MyPagerAdapter extends PagerAdapter {
@Override
public Object instantiateItem(ViewGroup container, int position) {
// TODO Auto-generated method stub
System.out.println("--path-->>" + items[position]);
View view = content.get(position);
final ImageView imageView = (ImageView) view
.findViewById(R.id.imageView1);
// imageView.setImageBitmap(bm);
new ImageDownLoad().loadImage(items[position], new ImageCallBack() {
@Override
public void getImageContent(Bitmap bitmap) {
// TODO Auto-generated method stub
imageView.setImageBitmap(bitmap);
}
});
((ViewPager) container).addView(view);
return view;
}
@Override
public CharSequence getPageTitle(int position) {
// TODO Auto-generated method stub
return title.get(position);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return content.size();
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return (arg0 == arg1);
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// TODO Auto-generated method stub
// super.destroyItem(container, position, object);
((ViewPager) container).removeView(content.get(position));
}
}
@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;
}
}
对应布局:
<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: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=".MainActivity" >
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center" >
<android.support.v4.view.PagerTitleStrip
android:id="@+id/pagertitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top" >
</android.support.v4.view.PagerTitleStrip>
</android.support.v4.view.ViewPager>
</RelativeLayout>
item布局:
<?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" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
</LinearLayout>
下载图片工具类:
public class ImageDownLoad {
public void loadImage(final String path, final ImageCallBack callBack) {
final Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
Bitmap bitmap = (Bitmap) msg.obj;
callBack.getImageContent(bitmap);
}
};
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(path);
HttpResponse response = null;
try {
response = httpClient.execute(httpPost);
if (response.getStatusLine().getStatusCode() == 200) {
byte[] data = EntityUtils.toByteArray(response
.getEntity());
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0,
data.length);
Message message = Message.obtain();
message.obj = bitmap;
handler.sendMessage(message);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
httpClient.getConnectionManager().shutdown();
}
}
}).start();
}
public interface ImageCallBack {
public void getImageContent(Bitmap bitmap);
}
}