package com.example.ex_hh;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity {
private String [] img_uri ={“http://7xku6s.com1.z0.glb.clouddn.com/qq0.png”,”http://7xku6s.com1.z0.glb.clouddn.com/qq1.png”,
“http://7xku6s.com1.z0.glb.clouddn.com/qq2.png”,”http://7xku6s.com1.z0.glb.clouddn.com/qq3.png”,
“http://7xku6s.com1.z0.glb.clouddn.com/qq4.png”,”http://7xku6s.com1.z0.glb.clouddn.com/qq5.png”,
“http://7xku6s.com1.z0.glb.clouddn.com/aa.jpg”,”http://7xku6s.com1.z0.glb.clouddn.com/bb.jpg”,
“http://7xku6s.com1.z0.glb.clouddn.com/cc.jpg”,”http://7xku6s.com1.z0.glb.clouddn.com/dd.jpg”};
private String [] str_arr = {“aaa”,”bbb”,”ccc”,”ddd”,”eee”,”fff”,”iii”,”jjj”,”kkk”,”lll”};
private ArrayList<UserEnity> list = new ArrayList<UserEnity>();
public ImageLoader imageLoader = ImageLoader.getInstance();
public String LAG = “MainActivity”;
private DisplayImageOptions options;
private ListView listview;
private MyAdapter myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
options = new DisplayImageOptions.Builder()
.showImageOnLoading(R.drawable.ic_launcher)
.showImageForEmptyUri(R.drawable.ic_launcher)
.showImageOnFail(R.drawable.ic_launcher)
.cacheInMemory(true)
.cacheOnDisk(true)
.considerExifParams(true)
.displayer(new RoundedBitmapDisplayer(20))
.build();
for (int i = 0; i < img_uri.length; i++) {
UserEnity userEnity = new UserEnity();
userEnity.setImguri(img_uri[i]);
userEnity.setName(str_arr[i]);
list.add(userEnity);
}
imageLoader.init(ImageLoaderConfiguration.createDefault(MainActivity.this));
myAdapter = new MyAdapter(getApplicationContext(), list);
listview = (ListView) findViewById(R.id.listView1);
listview.setAdapter(myAdapter);
listview.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
switch (scrollState){
case AbsListView.OnScrollListener.SCROLL_STATE_IDLE://停止滚动
{
Log.e(LAG, “停止滚动”);
//设置为停止滚动
myAdapter.setScrollState(false);
//当前屏幕中listview的子项的个数
int count = view.getChildCount();
Log.e(LAG,count+””);
for (int i = 0; i < count; i++) {
//获取到item的name
TextView tv_name = (TextView) view.getChildAt(i).findViewById(R.id.tv_title);
//获取到item的头像
ImageView iv_show= (ImageView) view.getChildAt(i).findViewById(R.id.imageView1);
if (tv_name.getTag() != null) { //非null说明需要加载数据
tv_name.setText(tv_name.getTag().toString());//直接从Tag中取出我们存储的数据name并且赋值
tv_name.setTag(null);//设置为已加载过数据
}
if (!iv_show.getTag().equals(“1”)){//!=”1″说明需要加载数据
String image_url=iv_show.getTag().toString();//直接从Tag中取出我们存储的数据image——url
ImageLoader.getInstance().displayImage(image_url, iv_show);//显示图片
iv_show.setTag(“1”);//设置为已加载过数据
}
}
break;
}
case AbsListView.OnScrollListener.SCROLL_STATE_FLING://滚动做出了抛的动作
{
Log.e(LAG, “快速滚动”);
//设置为正在滚动
myAdapter.setScrollState(true);
break;
}
case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL://正在滚动
{
Log.e(“LAG”, “触摸滚动”);
//设置为正在滚动
myAdapter.setScrollState(true);
break;
}
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
}
});
}
ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();
private static class AnimateFirstDisplayListener extends SimpleImageLoadingListener {
static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
if (loadedImage != null) {
ImageView imageView = (ImageView) view;
boolean firstDisplay = !displayedImages.contains(imageUri);
if (firstDisplay) {
FadeInBitmapDisplayer.animate(imageView, 500);
displayedImages.add(imageUri);
}
}
}
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
imageLoader.clearMemoryCache();
imageLoader.clearDiskCache();
}
public class MyAdapter extends BaseAdapter
// implements OnScrollListener
{
Context context;
LayoutInflater inflater;
List<UserEnity> lists;
private boolean scrollState = false;
public void setScrollState(boolean scrollState) {
this.scrollState = scrollState;
}
public MyAdapter(Context context, List<UserEnity> lists) {
this.context = context;
this.inflater = LayoutInflater.from(context);
this.lists = lists;
}
@Override
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public Object getItem(int position) {
return lists.get(position%10);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.item, null);
viewHolder = new ViewHolder();
viewHolder.iv_icon = (ImageView) convertView.findViewById(R.id.imageView1);
viewHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_title);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
UserEnity userEnity = lists.get(position%10);
String name = userEnity.getName();
String img_url = userEnity.getImguri();
if (!scrollState) {
viewHolder.tv_name.setText(name);
// imageLoader.displayImage(img_url, viewHolder.iv_icon);
imageLoader.displayImage(img_url, viewHolder.iv_icon, options, animateFirstListener);
viewHolder.iv_icon.setTag(“1”);
Bitmap loadImageSync = imageLoader.loadImageSync(img_url);
Log.e(“cx”, “====”+loadImageSync);
} else {
viewHolder.tv_name.setText(“加载中”);
viewHolder.tv_name.setTag(name);
viewHolder.iv_icon.setTag(img_url);
viewHolder.iv_icon.setImageResource(R.drawable.ic_launcher);
}
return convertView;
}
class ViewHolder {
TextView tv_name;
ImageView iv_icon;
}
}
@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 boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
30
viewHolder.iv_icon.setTag(“1”);
Bitmap loadImageSync = imageLoader.loadImageSync(img_url);
Log.e(“cx”, “====”+loadImageSync);
这里是在getView 调用的,也就是在主线程调用的。主线程不能直接访问网络的,改为异步或开线程去处理
10