您的位置:首页 > 其它

listview的局部刷新

2016-01-18 14:57 183 查看
1、效果:



2、实现:使用同等级的方法修改每项的ui

public class TestAdapter extends BaseAdapter {

private SparseArray<AppFile> dataList = null;
private LayoutInflater inflater = null;
private Context mContext;

public TestAdapter(Context context, SparseArray<AppFile> dataList) {
this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.dataList = dataList;
this.mContext = context;
}

@Override
public int getCount() {
return dataList.size();
}

@Override
public Object getItem(int position) {
return dataList.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

// 改变下载按钮的样式
private void changeBtnStyle(Button btn, boolean enable) {
if (enable) {
btn.setBackgroundResource(R.drawable.btn_download_norm);
} else {
btn.setBackgroundResource(R.drawable.btn_download_disable);
}
btn.setEnabled(enable);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

final ViewHolder holder;
if (null == convertView) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.listitem_app, null);
holder.layout = (LinearLayout) convertView.findViewById(R.id.gamelist_item_layout);
holder.icon = (ImageView) convertView.findViewById(R.id.app_icon);
holder.name = (TextView) convertView.findViewById(R.id.app_name);
holder.size = (TextView) convertView.findViewById(R.id.app_size);
holder.btn = (Button) convertView.findViewById(R.id.download_btn);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}

// 这里position和app.id的值是相等的
final AppFile app = dataList.get(position);
// Log.e("", "id="+app.id+", name="+app.name);

holder.name.setText(app.name);
holder.size.setText((app.downloadSize * 100.0f / app.size) + "%");

Drawable drawable = mContext.getResources().getDrawable(R.drawable.app_icon);
holder.icon.setImageDrawable(drawable);

switch (app.downloadState) {
case DownloadManager.DOWNLOAD_STATE_NORMAL:
holder.btn.setText("下载");
this.changeBtnStyle(holder.btn, true);
break;
case DownloadManager.DOWNLOAD_STATE_DOWNLOADING:
holder.btn.setText("下载中");
this.changeBtnStyle(holder.btn, false);
break;
case DownloadManager.DOWNLOAD_STATE_FINISH:
holder.btn.setText("已下载");
this.changeBtnStyle(holder.btn, false);
break;
case DownloadManager.DOWNLOAD_STATE_WAITING:
holder.btn.setText("排队中");
this.changeBtnStyle(holder.btn, false);
break;
}
holder.btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
DownloadFile downloadFile = new DownloadFile();
downloadFile.downloadID = app.id;
downloadFile.downloadState = DownloadManager.DOWNLOAD_STATE_WAITING;
app.downloadState = DownloadManager.DOWNLOAD_STATE_WAITING;
downloadFile.downloadSize = app.downloadSize;
downloadFile.totalSize = app.size;
holder.btn.setText("排队中");
changeBtnStyle(holder.btn, false);
}
});
return convertView;
}

static class ViewHolder {
LinearLayout layout;
ImageView icon;
TextView name;
TextView size;
Button btn;
}


3、启动多个下载线程:

////activity

listView = (ListView)this.findViewById(R.id.listview);
AppListAdapter adapter = new AppListAdapter(this, appList);
adapter.setListView(listView);
listView.setAdapter(adapter);


//adapter

public class AppListAdapter extends BaseAdapter {

private SparseArray<AppFile> dataList = null;
private LayoutInflater inflater = null;
private Context mContext;
private DownloadManager downloadManager;
private ListView listView;

public AppListAdapter(Context context, SparseArray<AppFile> dataList) {
this.inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.dataList = dataList;
this.mContext = context;
this.downloadManager = DownloadManager.getInstance();
this.downloadManager.setHandler(mHandler);
}

public void setListView(ListView view)
{
this.listView = view;
}

@Override
public int getCount() {
return dataList.size();
}

@Override
public Object getItem(int position) {
return dataList.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

// 改变下载按钮的样式
private void changeBtnStyle(Button btn, boolean enable)
{
if(enable)
{
btn.setBackgroundResource(R.drawable.btn_download_norm);
}
else
{
btn.setBackgroundResource(R.drawable.btn_download_disable);
}
btn.setEnabled(enable);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

final ViewHolder holder;
if (null == convertView) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.listitem_app, null);
holder.layout = (LinearLayout) convertView
.findViewById(R.id.gamelist_item_layout);
holder.icon = (ImageView) convertView
.findViewById(R.id.app_icon);
holder.name = (TextView) convertView
.findViewById(R.id.app_name);
holder.size = (TextView) convertView
.findViewById(R.id.app_size);
holder.btn = (Button) convertView
.findViewById(R.id.download_btn);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}

// 这里position和app.id的值是相等的
final AppFile app = dataList.get(position);
//Log.e("", "id="+app.id+", name="+app.name);

holder.name.setText(app.name);
holder.size.setText((app.downloadSize * 100.0f / app.size) + "%");

Drawable drawable = mContext.getResources().getDrawable(R.drawable.app_icon);
holder.icon.setImageDrawable(drawable);

switch(app.downloadState)
{
case DownloadManager.DOWNLOAD_STATE_NORMAL:
holder.btn.setText("下载");
this.changeBtnStyle(holder.btn, true);
break;
case DownloadManager.DOWNLOAD_STATE_DOWNLOADING:
holder.btn.setText("下载中");
this.changeBtnStyle(holder.btn, false);
break;
case DownloadManager.DOWNLOAD_STATE_FINISH:
holder.btn.setText("已下载");
this.changeBtnStyle(holder.btn, false);
break;
case DownloadManager.DOWNLOAD_STATE_WAITING:
holder.btn.setText("排队中");
this.changeBtnStyle(holder.btn, false);
break;
}
holder.btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
DownloadFile downloadFile = new DownloadFile();
downloadFile.downloadID = app.id;
downloadFile.downloadState = DownloadManager.DOWNLOAD_STATE_WAITING;
app.downloadState = DownloadManager.DOWNLOAD_STATE_WAITING;
downloadFile.downloadSize = app.downloadSize;
downloadFile.totalSize = app.size;
holder.btn.setText("排队中");
changeBtnStyle(holder.btn, false);
downloadManager.startDownload(downloadFile);
}
});
return convertView;
}

static class ViewHolder {
LinearLayout layout;
ImageView icon;
TextView name;
TextView size;
Button btn;
}

/**
* 使用外部类---来管理handler的异步操作
*/
private Handler mHandler = new Handler() {

public void handleMessage(Message msg)
{
DownloadFile downloadFile = (DownloadFile)msg.obj;
AppFile appFile = dataList.get(downloadFile.downloadID);
appFile.downloadSize = downloadFile.downloadSize;
appFile.downloadState = downloadFile.downloadState;

// notifyDataSetChanged会执行getView函数,更新所有可视item的数据
//notifyDataSetChanged();
// 只更新指定item的数据,提高了性能
updateView(appFile.id);
}
};

// 更新指定item的数据
private void updateView(int index)
{
int visiblePos = listView.getFirstVisiblePosition();
int offset = index - visiblePos;
//Log.e("", "index="+index+"visiblePos="+visiblePos+"offset="+offset);
// 只有在可见区域才更新
if(offset < 0) return;

View view = listView.getChildAt(offset);
final AppFile app = dataList.get(index);
ViewHolder holder = (ViewHolder)view.getTag();
//Log.e("", "id="+app.id+", name="+app.name);

holder.name.setText(app.name);
holder.size.setText((app.downloadSize * 100.0f / app.size) + "%");
Drawable drawable = mContext.getResources().getDrawable(R.drawable.app_icon);
holder.icon.setImageDrawable(drawable);

switch(app.downloadState)
{
case DownloadManager.DOWNLOAD_STATE_DOWNLOADING:
holder.btn.setText("下载中");
this.changeBtnStyle(holder.btn, false);
break;
case DownloadManager.DOWNLOAD_STATE_FINISH:
holder.btn.setText("已下载");
this.changeBtnStyle(holder.btn, false);
break;
}

}
}


///下载线程类:

public class DownloadManager {

// 下载状态:正常,暂停,下载中,已下载,排队中
public static final int DOWNLOAD_STATE_NORMAL = 0x00;
public static final int DOWNLOAD_STATE_PAUSE = 0x01;
public static final int DOWNLOAD_STATE_DOWNLOADING = 0x02;
public static final int DOWNLOAD_STATE_FINISH = 0x03;
public static final int DOWNLOAD_STATE_WAITING = 0x04;

// SparseArray是android中替代Hashmap的类,可以提高效率
private SparseArray<DownloadFile> downloadFiles = new SparseArray<DownloadFile>();
// 用来管理所有下载任务
private ArrayList<DownloadTask> taskList = new ArrayList<DownloadTask>();
private Handler mHandler;
private final static Object syncObj = new Object();
private static DownloadManager instance;
private ExecutorService executorService;

private DownloadManager() {
// 最多只能同时下载3个任务,其余的任务排队等待
executorService = Executors.newFixedThreadPool(3);
}

public static DownloadManager getInstance() {
if (null == instance) {
synchronized (syncObj) {
instance = new DownloadManager();
}
return instance;
}
return instance;
}

public void setHandler(Handler handler) {
this.mHandler = handler;
}

// 开始下载,创建一个下载线程
public void startDownload(DownloadFile file) {
downloadFiles.put(file.downloadID, file);
DownloadTask task = new DownloadTask(file.downloadID);
taskList.add(task);
executorService.submit(task);
}

public void stopAllDownloadTask() {
while (taskList.size() != 0) {
DownloadTask task = taskList.remove(0);
// 可以在这里做其他的处理
task.stopTask();
}
// 会停止正在进行的任务和拒绝接受新的任务
executorService.shutdownNow();

}

// 下载任务
class DownloadTask implements Runnable {

private boolean isWorking = false;
private int downloadId;

public DownloadTask(int id) {
this.isWorking = true;
this.downloadId = id;
}

public void stopTask() {
this.isWorking = false;
}

// 更新listview中对应的item
public void update(DownloadFile downloadFile) {
Message msg = mHandler.obtainMessage();
if (downloadFile.totalSize == downloadFile.downloadSize)
downloadFile.downloadState = DOWNLOAD_STATE_FINISH;
msg.obj = downloadFile;
msg.sendToTarget();

}

public void run() {
// 更新下载文件的状态
DownloadFile downloadFile = downloadFiles.get(downloadId);
downloadFile.downloadState = DOWNLOAD_STATE_DOWNLOADING;
while (isWorking) {
// 检测是否下载完成
if (downloadFile.downloadState != DOWNLOAD_STATE_DOWNLOADING) {
downloadFiles.remove(downloadFile.downloadID);
taskList.remove(this);
isWorking = false;
break;
}
// Log.e("", "downloadSize="+downloadFile.downloadSize+"; size="+downloadFile.totalSize);
// 这里只是模拟了下载,每一秒更新一次item的下载状态
if (downloadFile.downloadSize <= downloadFile.totalSize) {
this.update(downloadFile);
}

if (downloadFile.downloadSize < downloadFile.totalSize) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
downloadFile.downloadState = DOWNLOAD_STATE_PAUSE;
this.update(downloadFile);
downloadFiles.remove(downloadId);
isWorking = false;
break;
}

++downloadFile.downloadSize;
}
}

}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: