前言:昨晚上师傅让我写了发给一个人的,因为我很闲,于是就写了,网上我看到好多例子,涉及到好多类,看的头疼还不如自己写,一个类搞定,如下:(PS:防止以后忘记)xml里面就一个ViewPager控件,没啦,哈哈^_^.
package com.yalantis.contextmenu.sample;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.yalantis.contextmenu.R;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity2Activity extends ActionBarActivity {
ViewPager viewPager;
ViewPagerAdapter adapter;
List<Map<String, Object>> data;
protected ImageLoader imageLoader = ImageLoader.getInstance();
DisplayImageOptions options;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity2);
init();
}
public void init() {
viewPager = (ViewPager) findViewById(R.id.mypager);
data = getData();
imageLoader=ImageLoader.getInstance();
imageLoader.init(ImageLoaderConfiguration.createDefault(this));
options = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.ic_launcher) // 在ImageView加载过程中显示图片
.showImageForEmptyUri(R.drawable.ic_launcher) // image连接地址为空时
.showImageOnFail(R.drawable.ic_launcher) // image加载失败
.cacheInMemory(true) // 加载图片时会在内存中加载缓存
.cacheOnDisc(true) // 加载图片时会在磁盘中加载
.build();
adapter = new ViewPagerAdapter(data);
viewPager.setAdapter(adapter);
}
public List<Map<String, Object>> getData() {
List<Map<String, Object>> mdata = new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<String, Object>();
map.put("url", "http://img2.duitang.com/uploads/item/201207/19/20120719132725_UkzCN.jpeg");
map.put("view", new ImageView(this));
mdata.add(map);
Map<String, Object> map1 = new HashMap<String, Object>();
map1.put("url", "http://img4.duitang.com/uploads/item/201404/24/20140424195028_vtvZu.jpeg");
map1.put("view", new ImageView(this));
mdata.add(map1);
Map<String, Object> map2 = new HashMap<String, Object>();
map2.put("url", "http://download.pchome.net/wallpaper/pic-5041-8-240x320.jpg");
map2.put("view", new ImageView(this));
mdata.add(map2);
Map<String, Object> map3 = new HashMap<String, Object>();
map3.put("url", "http://www.mangowed.com/uploads/allimg/130425/572-130425105311304.jpg");
map3.put("view", new ImageView(this));
mdata.add(map3);
return mdata;
}
public class ViewPagerAdapter extends PagerAdapter {
List<Map<String,Object>> viewLists;
public ViewPagerAdapter(List<Map<String,Object>> lists)
{
viewLists = lists;
}
@Override
public int getCount() { //获得size
// TODO Auto-generated method stub
return viewLists.size();
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
// TODO Auto-generated method stub
return arg0 == arg1;
}
@Override
public void destroyItem(View view, int position, Object object) //销毁Item
{
ImageView x=(ImageView)viewLists.get(position).get("view");
x.setScaleType(ImageView.ScaleType.FIT_CENTER);
((ViewPager) view).removeView(x);
}
@Override
public Object instantiateItem(View view, int position) //实例化Item
{
ImageView x=(ImageView)viewLists.get(position).get("view");
x.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageLoader.displayImage(viewLists.get(position).get("url").toString(), x,options);
((ViewPager) view).addView(x, 0);
return viewLists.get(position).get("view");
}
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-11-11 17:12:46