Android 使用开源库加载网络图片,使用开源库加载图片。单击listview弹出popupwindow弹出框详情查看:
Android 单击listview弹出popupwindow弹出框
,里面还附带listview右滑删除按钮详情查看: Android 使用NineOldAndroids实现仿微信listview左滑出现删除itembutton。项目就是这样慢慢变得复杂,是往里面逐渐加功能,效果,代码。本文项目源码下载地址:点击
一、看实现的效果
二、项目结构图
三、部分源码
1、MyApplication继承了Application
package com.main.listviewsideslip;
import java.io.File;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.cache.disc.naming.HashCodeFileNameGenerator;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.cache.memory.impl.UsingFreqLimitedMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.decode.BaseImageDecoder;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import com.nostra13.universalimageloader.utils.StorageUtils;
import android.app.Application;
import android.content.Context;
public class MyApplication extends Application{
public void onCreate() {
super.onCreate();
initImageLoader(getApplicationContext());
}
public static void initImageLoader(Context context) {
//缓存文件的目录
File cacheDir = StorageUtils.getOwnCacheDirectory(context, "imageloader/Cache");
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
.memoryCacheExtraOptions(480, 800) // max width, max height,即保存的每个缓存文件的最大长宽
.threadPoolSize(3) //线程池内加载的数量
.threadPriority(Thread.NORM_PRIORITY - 2)
.denyCacheImageMultipleSizesInMemory()
.diskCacheFileNameGenerator(new Md5FileNameGenerator()) //将保存的时候的URI名称用MD5 加密
.memoryCache(new UsingFreqLimitedMemoryCache(2 * 1024 * 1024)) // You can pass your own memory cache implementation/你可以通过自己的内存缓存实现
.memoryCacheSize(2 * 1024 * 1024) // 内存缓存的最大值
.diskCacheSize(50 * 1024 * 1024) // 50 Mb sd卡(本地)缓存的最大值
.tasksProcessingOrder(QueueProcessingType.LIFO)
// 由原先的discCache -> diskCache
.diskCache(new UnlimitedDiskCache(cacheDir))//自定义缓存路径
.imageDownloader(new BaseImageDownloader(context, 5 * 1000, 30 * 1000)) // connectTimeout (5 s), readTimeout (30 s)超时时间
.writeDebugLogs() // Remove for release app
.build();
//全局初始化此配置
ImageLoader.getInstance().init(config);
}
}
2、适配器RecentAdapter里面同样设置着网络加载配置。
package com.listview.adapter;
import java.util.LinkedList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.main.entity.RecentItem;
import com.main.listviewsideslip.Constants;
import com.main.listviewsideslip.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.view.listview.SwipeListView;
public class RecentAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private LinkedList<RecentItem> mData;
private SwipeListView mListView;
private Context mContext;
private DisplayImageOptions mOptions; // 设置图片显示相关参数
public static String[] mImageUrls; // 图片路径
private ImageLoader mImageLoader; //网络图片加载类
public RecentAdapter(Context context, LinkedList<RecentItem> data,
SwipeListView listview) {
mContext = context;
this.mInflater = LayoutInflater.from(mContext);
mData = data;
this.mListView = listview;
mImageLoader = ImageLoader.getInstance();
initImageView();
}
public void remove(int position) {
if (position < mData.size()) {
mData.remove(position);
notifyDataSetChanged();
}
}
public void remove(RecentItem item) {
if (mData.contains(item)) {
mData.remove(item);
notifyDataSetChanged();
}
}
public void addFirst(RecentItem item) {
if (mData.contains(item)) {
mData.remove(item);
}
mData.addFirst(item);
notifyDataSetChanged();
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Object getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final RecentItem item = mData.get(position);
ViewHolder vHolder = null;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.recent_listview_item, null);
vHolder = new ViewHolder();
vHolder.nameTV = (TextView) convertView.findViewById(R.id.recent_list_item_name);
vHolder.numTV = (TextView) convertView.findViewById(R.id.unreadmsg);
vHolder.headIV = (ImageView) convertView.findViewById(R.id.icon);
vHolder.deleteBtn = (Button) convertView.findViewById(R.id.recent_del_btn);
convertView.setTag(vHolder);
}else {
vHolder = (ViewHolder) convertView.getTag();
}
vHolder.nameTV.setText(item.getName());
/**
* imageUrl 图片的Url地址 imageView 承载图片的ImageView控件 options
* DisplayImageOptions配置文件
*/
mImageLoader.displayImage(mImageUrls[position],
vHolder.headIV, mOptions);
int num = item.getNewNum();
if (num > 0) {
vHolder.numTV.setVisibility(View.VISIBLE);
vHolder.numTV.setText(num + "");
} else {
vHolder.numTV.setVisibility(View.GONE);
}
vHolder.deleteBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mData.remove(position);
notifyDataSetChanged();
if (mListView != null)
mListView.closeOpenedItems();
}
});
return convertView;
}
class ViewHolder{
public TextView nameTV;
public TextView numTV;
public ImageView headIV;
public Button deleteBtn;
}
package com.main.listviewsideslip;
import java.util.Collections;
import java.util.LinkedList;
import com.listview.adapter.RecentAdapter;
import com.main.entity.RecentItem;
import com.view.listview.BaseSwipeListViewListener;
import com.view.listview.SwipeListView;
import com.view.pop.ActionItem;
import com.view.pop.QuickAction;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.app.Activity;
import android.content.Intent;
public class MainActivity extends Activity{
//listview的适配器
private RecentAdapter mAdapter;
//listview数据源
public LinkedList<RecentItem> mRecentDatas;
//自定义的listview
private SwipeListView mRecentListView;
private TextView mEmpty;
private int mClickItem = -1;
// 点击item弹出
protected QuickAction mItemClickQuickAction = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView(savedInstanceState);
initPop();
}
//单击listview某一项时,可以弹出pop。QuickAction实现类是继承PopupWindows
private void initPop() {
// 设置Action
mItemClickQuickAction = new QuickAction(this, QuickAction.VERTICAL);
ActionItem pointItem = new ActionItem(1, "详细信息", null);
ActionItem areaItem = new ActionItem(2, "信息2", null);
pointItem.setIcon(null);
areaItem.setIcon(null);
mItemClickQuickAction.addActionItem(pointItem);
mItemClickQuickAction.addActionItem(areaItem);
mItemClickQuickAction.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() {
@Override
public void onItemClick(QuickAction source, int pos, int actionId) {
if (pos == 0) {
Intent intent = new Intent(source.GetActionItemsGroup().getContext(),ListViewItemShowActivity.class);
RecentItem item = new RecentItem();
item = mRecentDatas.get(mClickItem);
Bundle bundle = new Bundle();
// bundle.putString("HeadImg", mImageUrls[mClickItem]);
bundle.putInt("NewNum", item.getNewNum());
bundle.putString("Name", item.getName());
intent.putExtras(bundle);
startActivity(intent);
}
mItemClickQuickAction.dismiss();
}
});
}
private void initView(Bundle savedInstanceState) {
mRecentListView = (SwipeListView) findViewById(R.id.recent_listview);
mRecentDatas = getRecentList();
mAdapter = new RecentAdapter(this, mRecentDatas, mRecentListView);
mRecentListView.setAdapter(mAdapter);
mEmpty = (TextView) findViewById(R.id.empty);
mRecentListView.setEmptyView(mEmpty);
mRecentListView
.setSwipeListViewListener(new BaseSwipeListViewListener() {
@Override
public void onClickFrontView(View view, int position) {
mClickItem = position;
mItemClickQuickAction.show(view);
}
@Override
public void onClickBackView(int position) {
mRecentListView.closeOpenedItems();// 关闭打开的项
}
@Override
public void onDismiss(int[] reverseSortedPositions) {
for (int position : reverseSortedPositions) {
mAdapter.remove(position);
}
}
});
}
public LinkedList<RecentItem> getRecentList() {
LinkedList<RecentItem> list = new LinkedList<RecentItem>();
nameArray = getResources().getStringArray(R.array.jazzy_effects_ch);
for (int i = 0; i < 20; i++) {
// int icon = heads[i];
String name = nameArray[i];
int num = numArray[i];
RecentItem item = new RecentItem(name, 0, num);
list.add(item);
}
Collections.sort(list);// 降序
return list;
}
public static int[] numArray = { 21,67,65,8,54,67,23,78,98,54,67,23,67,423,23,67,90,12,34,56,978};
public static String[] nameArray = new String[21];
}
四、最后别忘了AndroidManifest.xml配置网络权限,以及application
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:name="com.main.listviewsideslip.MyApplication"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
本文相关文章:
1、Android 使用NineOldAndroids实现仿微信listview左滑出现删除itembutton
2、Android 单击listview弹出popupwindow弹出框
本文项目源码下载地址:点击