Android listview局部刷新和模拟应用下载(zhu)
在android开发中,listview是比较常用的一个组件,在listview的数据需要更新的时候,一般会用notifyDataSetChanged()这个函数,但是它会更新listview中所有可视范围内的item,这样对性能肯定会有影响。比较常见的情景是android应用商店中的下载列表,当我们下载一款游戏的时候,只需要更新这款游戏对应的进度就可以了。本文就来模拟android应用商店的游戏下载,实现对listview的局部刷新,只实现一个简单的demo,不去真的下载文件。
1. 首先来创建代表应用商店中的app文件的类:AppFile.java,包含了一些基本的属性,源码:
package com.alexzhou.downloadfile; /**
* author:alexzhou
* email :zhoujiangbohai@163.com
* date :2013-1-27
*
* 游戏列表中的app文件
**/ public class AppFile { public int id;
public String name;
// app的大小
public int size;
// 已下载大小
public int downloadSize;
// 下载状态:正常,正在下载,暂停,等待,已下载
public int downloadState;
}
2. 由于实际开发时,AppFile的属性比较多,这里创建一个辅助类:DownloadFile.java,代表下载中的文件,源码:
package com.alexzhou.downloadfile; /**
* author:alexzhou
* email :zhoujiangbohai@163.com
* date :2013-1-27
*
* 下载的文件
**/ public class DownloadFile { public int downloadID;
public int downloadSize;
public int totalSize;
public int downloadState;
}
3. 接下来需要一个下载管理类:DownloadManager.java,它管理所有下载任务。当同时下载很多任务的时候,界面会卡,所以指定只能同时下载3个任务,每个任务会启动一个线程,这里使用了ExecutorService线程池。当提交了超过三个下载任务时,只执行前3个任务,第四个任务会等到前面有一个下载完成后再下载,以此类推。这里还用到了android提供的一个工具类SparseArray,它是用来替代HashMap的,性能比HashMap要好。下面看源码:
import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.util.SparseArray; /**
author:alexzhou
email :zhoujiangbohai@163.com
date :2013-1-27 下载管理
**/ 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();
} 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() != )
{
DownloadTask task = taskList.remove();
// 可以在这里做其他的处理
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();
} catch (InterruptedException e) {
e.printStackTrace();
downloadFile.downloadState = DOWNLOAD_STATE_PAUSE;
this.update(downloadFile);
downloadFiles.remove(downloadId);
isWorking = false;
break;
} ++ downloadFile.downloadSize;
}
} }
} }
4. 接下来就需要实现listview的adapter了,这里比较重要的一个函数是updateView,这是实现listview局部刷新的关键,通过索引index得到listview中对应位置的子view,然后再更新该view的数据。源码:
package com.alexzhou.downloadfile; import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.util.SparseArray;
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.LinearLayout;
import android.widget.ListView;
import android.widget.TextView; /**
author:alexzhou
email :zhoujiangbohai@163.com
date :2013-1-27 app列表的数据适配器
**/ 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;
} 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 < ) 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;
} }
3. 接下来需要一个下载管理类:DownloadManager.java,它管理所有下载任务。当同时下载很多任务的时候,界面会卡,所以指定只能同时下载3个任务,每个任务会启动一个线程,这里使用了ExecutorService线程池。当提交了超过三个下载任务时,只执行前3个任务,第四个任务会等到前面有一个下载完成后再下载,以此类推。这里还用到了android提供的一个工具类SparseArray,它是用来替代HashMap的,性能比HashMap要好。下面看源码:
package com.alexzhou.downloadfile; import java.util.ArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.util.SparseArray; /**
author:alexzhou
email :zhoujiangbohai@163.com
date :2013-1-27 下载管理
**/ 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();
} 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() != )
{
DownloadTask task = taskList.remove();
// 可以在这里做其他的处理
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();
} catch (InterruptedException e) {
e.printStackTrace();
downloadFile.downloadState = DOWNLOAD_STATE_PAUSE;
this.update(downloadFile);
downloadFiles.remove(downloadId);
isWorking = false;
break;
} ++ downloadFile.downloadSize;
}
} }
} }
4. 接下来就需要实现listview的adapter了,这里比较重要的一个函数是updateView,这是实现listview局部刷新的关键,通过索引index得到listview中对应位置的子view,然后再更新该view的数据。源码:
package com.alexzhou.downloadfile; import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.util.SparseArray;
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.LinearLayout;
import android.widget.ListView;
import android.widget.TextView; /**
author:alexzhou
email :zhoujiangbohai@163.com
date :2013-1-27 app列表的数据适配器
**/ 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;
} 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 < ) 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;
} }
}
布局文件listitem_app.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gamelist_item_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:background="@drawable/style_listitem_background"
android:paddingBottom="5dp"
android:paddingTop="5dp" > <ImageView
android:id="@+id/app_icon"
android:layout_width="53dip"
android:layout_height="53dip"
android:layout_marginLeft="5dip"
android:adjustViewBounds="false"
android:padding="5dp" /> <LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginLeft="5dp"
android:layout_weight=""
android:gravity="center_vertical"
android:orientation="vertical" > <TextView
android:id="@+id/app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:text=""
android:textColor="#000000"
android:textSize="13sp" /> <TextView
android:id="@+id/app_size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="10sp" /> </LinearLayout> <Button
android:id="@+id/download_btn"
android:layout_width="55dip"
android:layout_height="30dip"
android:layout_marginRight="10dip"
android:background="@drawable/style_btn_download"
android:focusable="false"
android:text="@string/download"
android:textColor="#ffffffff"
android:textSize="12sp" /> </LinearLayout>
listview中item样式文件style_listitem_background.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 没有焦点时的背景颜色 -->
<item android:state_window_focused="false" >
<shape>
<gradient
android:startColor="#ffffff"
android:endColor="#E3E3E3"
android:angle="-90" />
</shape>
</item> <!-- 非触摸模式下获得焦点并单击时的背景颜色 -->
<item android:state_focused="true" android:state_pressed="true"
android:drawable="@drawable/bg_listview_item_selected" />
<!--触摸模式下单击时的背景颜色 -->
<item android:state_focused="false" android:state_pressed="true"
android:drawable="@drawable/bg_listview_item_selected" />
<!--选中时的背景颜色 -->
<item android:state_selected="true" android:drawable="@drawable/bg_listview_item_selected" />
<!--获得焦点时的背景 颜色-->
<item android:state_focused="true" android:drawable="@drawable/bg_listview_item_selected" />
</selector>
item中的button样式文件style_btn_download.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/btn_download_pressed" />
<item android:drawable="@drawable/btn_download_norm" />
</selector>
字符文件strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">AndroidDownloadFile</string>
<string name="download">下载</string>
</resources>
5. 最后创建MainActivity.java,源码:
package com.alexzhou.downloadfile; import android.app.Activity;
import android.os.Bundle;
import android.util.SparseArray;
import android.widget.ListView; public class MainActivity extends Activity
{
private SparseArray<AppFile> appList = new SparseArray<AppFile>(); private ListView listView; @Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initData();
initUI();
} private void initData()
{
for(int i =; i<; i++)
{
AppFile app = new AppFile();
app.name = "快玩游戏--" + (i+);
app.size = ;
app.id = i;
app.downloadState = DownloadManager.DOWNLOAD_STATE_NORMAL;
app.downloadSize = ;
appList.put(app.id, app);
}
} private void initUI()
{
listView = (ListView)this.findViewById(R.id.listview);
AppListAdapter adapter = new AppListAdapter(this, appList);
adapter.setListView(listView);
listView.setAdapter(adapter);
} @Override
protected void onDestroy() {
super.onDestroy();
DownloadManager.getInstance().stopAllDownloadTask();
} }
布局文件activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fastScrollEnabled="true"
/>
</LinearLayout>
到此为止,代码部分已经全部完成了,下面来看看最终效果图:
这里对比一下分别使用updateView和notifyDataSetChanged时,有什么不一样,看看打印日志:
(1)使用notifyDataSetChanged时,listview可视范围内的所有子项都更新了。
(2)使用updateView时,只更新了指定的子项。
实例源码地址:http://pan.baidu.com/share/link?shareid=229182&uk=167811495,
Android listview局部刷新和模拟应用下载(zhu)的更多相关文章
- 转:android listview局部刷新和模拟应用下载
在 android开发中,listview是比较常用的一个组件,在listview的数据需要更新的时候,一般会用 notifyDataSetChanged()这个函数,但是它会更新listview中所 ...
- Android 结合实际项目学会ListView局部刷新和相关知识《一》
转载本专栏博客,请注明出处:道龙的博客 最近在公司参与的项目中有一个界面需要做局部UI更新处理,把其化烦为简为Demoi形式写在这里.我们还是运行该Demo,知道ListView局部刷新的使用场景:( ...
- 再说Android RecyclerView局部刷新那个坑
RecyclerView局部刷新大家都遇到过,有时候还说会遇见图片闪烁的问题. 优化之前的效果: 优化之后的效果: 如果想单独更新一个item,我们通常会这样做,代码如下: mLRecyclerV ...
- Android RecyclerView局部刷新那个坑
关键:public final void notifyItemChanged(int position, Object payload) RecyclerView局部刷新大家都遇到过,有时候还说会遇见 ...
- Android ListView 单条刷新方法实践及原理解析
对于使用listView配合adapter进行刷新的方法大家都不陌生,先刷新adapter里的数据,然后调用notifydatasetchange通知listView刷新界面. 方法虽然简单,但这里面 ...
- jquerymobile listview 局部刷新
function onSuccess(data, status) { data = $.trim(data); // alert(data); // return; if (data) { $('#l ...
- android:ListView的局部刷新
1.简介 对于android中的ListView刷新机制,大多数的程序员都是很熟悉的,修改或者添加adapter中的数据源之后,然后调用notifyDataSetChanged()刷新ListView ...
- [置顶]
android ListView包含Checkbox滑动时状态改变
题外话: 在xamarin android的开发中基本上所有人都会遇到这个小小的坎,的确有点麻烦,当时我也折腾了好一半天,如果你能看到这篇博客,说明你和我当初也是一样的焦灼,如果你想解决掉这个小小的坎 ...
- ListView实现Item局部刷新
对于ListView数据的刷新大家都知道,改变Adapter的数据源,然后调用Adapter的notifyDateSetChanged()方法即可. 但是博主在做公司项目的时候,有个下载模块,因为可 ...
随机推荐
- nc命令
监视指定网络接口的数据包 nc -l -u 1001 像指定端口使用指定协议发送数据包 nc -l -u 192 168.10.11 1001
- python_Opencv_使用Matplotlib模块
使用Matplotlib模块 Matplotib 是python 的一个绘图库,里头有各种各样的绘图方法. 之后会陆续了解.先学习怎样用Matplotib 显示图像. 可以放大图像,保存图像. 安装M ...
- android style 退出动画 解决退出动画无效问题
在AndroidMenifest.xml文件里面的Activity声明中,增加自己的Theme声明,如下: <activity android:name=".MyOrderListSe ...
- java相关的路径获取 (转载 http://tomfish88.iteye.com/blog/971255)
在jsp和class文件中调用的相对路径不同.在jsp里,根目录是WebRoot 在class文件中,根目录是WebRoot/WEB-INF/classes 当然你也可以用System.getProp ...
- Android中完全退出当前应用系统
一.将统一管理Activity的类ActivityManager复制到工程里面. package com.jsmtr.www.Helper; import java.util.LinkedList; ...
- 【转】大素数判断和素因子分解【miller-rabin和Pollard_rho算法】
集训队有人提到这个算法,就学习一下,如果用到可以直接贴模板,例题:POJ 1811 转自:http://www.cnblogs.com/kuangbin/archive/2012/08/19/2646 ...
- java语言基础特性
使用Java5中的类型安全枚举和注解(以及元注解)编程 http://www.importnew.com/11053.html
- android 广播分类
安卓广播分为两类:1.普通广播, broadcast,广播发出之后所有满足条件的应用都能获取到广播里面的数据,缺点是应用获取广播中的数据修改之后不能传递给其它接收广播的应用:2.有序广播,orderb ...
- iOS 网络与多线程--5.异步Post方式的网络请求(非阻塞)
通过Post请求方式,异步获取网络数据,异步请求不会阻塞主线程,而会建立一个新的线程来操作. 代码如下 ViewController.h文件 #import <UIKit/UIKit.h> ...
- NetBeans使用技巧记录
1.窗体字体大小设置: 在etc文件夹下的netbeans.conf中添加,12表示字体大小,12正合适. netbeans_default_options="--fontsize 12 - ...