ViewPager学习demo2

来源:互联网 发布:淘宝拍卖会官网 编辑:程序博客网 时间:2024/05/16 16:15


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;@Overrideprotected 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 {@Overridepublic Object instantiateItem(ViewGroup container, int position) {// TODO Auto-generated method stubSystem.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() {@Overridepublic void getImageContent(Bitmap bitmap) {// TODO Auto-generated method stubimageView.setImageBitmap(bitmap);}});((ViewPager) container).addView(view);return view;}@Overridepublic CharSequence getPageTitle(int position) {// TODO Auto-generated method stubreturn title.get(position);}@Overridepublic int getCount() {// TODO Auto-generated method stubreturn content.size();}@Overridepublic boolean isViewFromObject(View arg0, Object arg1) {// TODO Auto-generated method stubreturn (arg0 == arg1);}@Overridepublic void destroyItem(ViewGroup container, int position, Object object) {// TODO Auto-generated method stub// super.destroyItem(container, position, object);((ViewPager) container).removeView(content.get(position));}}@Overridepublic 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() {@Overridepublic void handleMessage(Message msg) {// TODO Auto-generated method stubsuper.handleMessage(msg);Bitmap bitmap = (Bitmap) msg.obj;callBack.getImageContent(bitmap);}};new Thread(new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubHttpClient 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 exceptione.printStackTrace();} finally {httpClient.getConnectionManager().shutdown();}}}).start();}public interface ImageCallBack {public void getImageContent(Bitmap bitmap);}}


0 0
原创粉丝点击