- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章Android开发之ListView实现Item局部刷新由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
对于android中的ListView刷新机制,大多数的程序员都是很熟悉的,修改或者添加adapter中的数据源之后,然后调用notifyDataSetChanged()刷新ListView。在这种模式下,我们会在getView中,根据不同的数据源,让控件显示不同的内容。这种模式是最常见的刷新模式,当我们来回滑动ListView的时候,调用adapter的getView方法,然后listview对adapter返回的View进行绘制。这种模式下,View的显示内容或状态都记录在adapter里面的数据源中,listview的更新频率不频繁,它随着数据源的变化而更新.
但是我小编在做公司项目的时候,有个下载模块,因为可能同时下载好几个数据,所以用的listview展示所有正在下载的内容。因为下载进度要实时更新,所以要不停的调用notifyDateSetChanged刷新数据。这样会不停的重新绘制整个listview的界面,性能开销非常大。而且如果每个item有图片的话,每个item的图片都需要重新加载,就算图片做了内存缓存,刷新一下图片也会闪一下,不停的刷新就会导致各个item的图片不停的闪,体验一点都不好.
那么对于上面问题,有没有解决办法呢?当然是有的。我们可以针对某一个item进行局部更新,而不影响其它没有修改的item。那么具体如何实现的呢?我们看下面的代码.
1
2
3
4
5
6
7
8
9
10
11
|
private
void
updateView(
int
itemIndex) {
//得到第一个可显示控件的位置,
int
visiblePosition = mListView.getFirstVisiblePosition();
//只有当要更新的view在可见的位置时才更新,不可见时,跳过不更新
if
(itemIndex - visiblePosition >= ) {
//得到要更新的item的view
View view = mListView.getChildAt(itemIndex - visiblePosition);
//调用adapter更新界面
mAdapter.updateView(view, itemIndex);
}
}
|
这个函数主要是根据传入的itemIndex来获取第itemIndex的数据所显示的view。itemIndex就是要修改的数据再List集合中的位置,比如我这里下载进度有更新,发了一个广播这里接收到了,需要修改该下载内容的进度条,广播接收器可以这么写:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
@Override
public
void
onReceive(Context context, Intent intent) {
AppContent appContent = intent.getParcelableExtra(
"appContent"
);
if
(appContent ==
null
)
return
;
int
itemIndex = ;
for
(AppContent appContent : mList) {
if
(appContent.getUrl().equals(appContent.getUrl())) {
itemIndex = mList.indexOf(appContent);
appContent.setDownloadPercent(appContent.getDownloadPercent());
break
;
}
}
updateView(itemIndex);
}
|
下面看Adapter的具体代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
public
class
AppContentAdapter
extends
BaseAdapter{
private
List<AppContent> mDates =
null
;
private
Context mContext;
public
AppContentAdapter(Context context) {
this
.mContext = context;
}
@Override
public
int
getCount() {
return
mDates.size();
}
@Override
public
Object getItem(
int
position) {
return
mDates.get(position);
}
@Override
public
long
getItemId(
int
position) {
return
position;
}
public
void
setDates(List<AppContent> mDates) {
this
.mDates = mDates;
}
@Override
public
View getView(
int
position, View convertView, ViewGroup parent) {
ViewHolder holder =
null
;
if
(convertView ==
null
) {
holder =
new
ViewHolder();
convertView = LayoutInflater.from(mContext).inflate(
R.layout.listitem_download,
null
);
holder.statusIcon = (DownloadPercentView) convertView.findViewById(R.id.status_icon);
holder.name = (TextView) convertView.findViewById(R.id.name);
holder.downloadPercent = (TextView) convertView.findViewById(R.id.download_percent);
holder.progressBar = (ProgressBar) convertView.findViewById(R.id.progressbar);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
}
setData(holder, position);
return
convertView;
}
/**
* 设置viewHolder的数据
* @param holder
* @param itemIndex
*/
private
void
setData(ViewHolder holder,
int
itemIndex) {
AppContent appContent = mDates.get(itemIndex);
holder.name.setText(appContent.getName());
holder.progressBar.setProgress(appContent.getDownloadPercent());
setIconByStatus(holder.statusIcon, appContent.getStatus());
if
(appContent.getStatus() == AppContent.Status.PENDING) {
holder.downloadPercent.setVisibility(View.INVISIBLE);
}
else
{
holder.downloadPercent.setVisibility(View.VISIBLE);
holder.statusIcon.setProgress(appContent.getDownloadPercent());
holder.downloadPercent.setText(
"下载进度:"
+ appContent.getDownloadPercent() +
"%"
);
}
}
/**
* 局部刷新
* @param view
* @param itemIndex
*/
public
void
updateView(View view,
int
itemIndex) {
if
(view ==
null
) {
return
;
}
//从view中取得holder
ViewHolder holder = (ViewHolder) view.getTag();
holder.statusIcon = (DownloadPercentView) view.findViewById(R.id.status_icon);
holder.name = (TextView) view.findViewById(R.id.name);
holder.downloadPercent = (TextView) view.findViewById(R.id.download_percent);
holder.progressBar = (ProgressBar) view.findViewById(R.id.progressbar);
setData(holder, itemIndex);
}
/**
* 根据状态设置图标
* @param downloadPercentView
* @param status
*/
private
void
setIconByStatus(DownloadPercentView downloadPercentView, AppContent.Status status) {
downloadPercentView.setVisibility(View.VISIBLE);
if
(status == AppContent.Status.PENDING) {
downloadPercentView.setStatus(DownloadPercentView.STATUS_PEDDING);
}
if
(status == AppContent.Status.DOWNLOADING) {
downloadPercentView.setStatus(DownloadPercentView.STATUS_DOWNLOADING);
}
if
(status == AppContent.Status.WAITING) {
downloadPercentView.setStatus(DownloadPercentView.STATUS_WAITING);
}
if
(status == AppContent.Status.PAUSED) {
downloadPercentView.setStatus(DownloadPercentView.STATUS_PAUSED);
}
if
(status == AppContent.Status.FINISHED) {
downloadPercentView.setStatus(DownloadPercentView.STATUS_FINISHED);
}
}
private
class
ViewHolder {
private
DownloadPercentView statusIcon;
private
TextView name;
private
TextView downloadPercent;
private
ProgressBar progressBar;
}
}
|
其实这些代码就是我上篇博文《AsyncTask实现多任务多线程下载》的例子中的,如果需要可以去下载.
以上内容是关于Android开发之ListView实现Item局部刷新的全部内容,希望对大家有用,更多有关listview局部刷新问题,请登录我官网查询,谢谢! 。
最后此篇关于Android开发之ListView实现Item局部刷新的文章就讲到这里了,如果你想了解更多关于Android开发之ListView实现Item局部刷新的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
如果我们定义一个像这样的函数 (defun foo(x) (setf x somevalue)) x 定义为局部变量还是全局变量?使用 setf/q 将值设置为全局值。如果它是全局的,谁能告诉我如
仍在学习 MVC3、EF。现在我正在连接到 MySql,但我相信这无关紧要。为简单起见,我决定为我的测试应用程序使用一个数据库,并且我包含了一个类别来区分数据。例如,我有一个新闻、事件、信息和页面类别
假设我定义了以下代码: int *func() { int *p=(int *)malloc(sizeof(int)); // memory is allocated from heap
我正在构建一个小型 PHP MVC,但我在一小部分编码方面碰壁了。我想我需要“局部 View ”,但我也许可以用现有代码实现一些东西。 目前我的 Controller 是最简单的形式: 实例化一个对象
假设我定义了以下代码: int *func() { int *p=(int *)malloc(sizeof(int)); // memory is allocated from heap
我有以下代码(用 Python 2.X 编写): def banana(x): def apple(stuff): x /= 10 return stuff -
我正在尝试重用一些代码,部分 View 似乎是使用 MVC 时执行此操作的最佳方式。 我创建了一个继承自 IEnumerable 的局部 View (见下文)。 @model IEnumerable
局部 const 变量将存储在哪里?我已经验证过,函数中使用 const 变量的每个位置都会被其值替换(如立即值寻址模式)。但如果指针被分配给它,那么它就会存储在堆栈中。在这里我不明白处理器如何知道其
我想将局部变量用作全局变量,有人告诉我这样做的方法是在函数外部创建变量,如下所示: var foo = null; function bar() {
我正在处理一个很长的 Angular 表格。我想知道我是否可以将它分成许多不同的 View 并在主视图中引用它们中的每一个。 First Section
我有一个导航栏,它是一个局部 View ,我需要在设计页面上呈现它,以便用户编辑他们的个人资料。事实上,我只有一个页面,但是添加执行帐户维护的路径搞乱了我的导航栏加载,因为实例变量不存在。无论如何,我
我没有用到全局变量,也从未明确定义过全局变量,但我的代码中似乎有一个。你能帮我把它做成本地的吗? def algo(X): # randomized algorithm while len(X
假设我有这个(当前无返回)函数: def codepoint_convert(text, offset): codepoint = text[offset] if codepoint
我在我的项目中同时使用了局部 View 和布局概念,但我无法区分。但我的感觉是两者都在做同样的工作。任何人都可以通过示例说出有关局部 View 和布局的简要概念以及区别吗? 最佳答案 除了 Josh
使用全局变量会加快速度吗?在英特尔的体系结构软件开发人员手册(关于微处理器)中建议使用局部变量而不是全局变量。但是,请考虑以下代码: void process_tcp_packets(void) {
我有一个局部 View 使用的模型与我在其中呈现它的 View 不同。我不断收到错误消息。 The model item passed into the dictionary is of type '
我在 cshtml 页面上有一个局部 View ,如下所示:- @model MvcCommons.ViewModels.CompositeViewModel @{ ViewBag.Title = "
我在从 while 循环全局更新数组时遇到问题,如下所述。请注意,我只能使用 C 95 及之前版本的功能。任何帮助将不胜感激!满浆箱http://pastebin.com/ss6VgTCD 在我的程序
我想刷新 Json 局部 View 。我正在尝试使用这个: $('#example123').load('@Url.Action("Rejestracja", "Logowanie")'); 但不能正
我有一个 asp.net 页面,它返回我当前正在使用的选项卡中的部分 View 。我已经设置了所有 jQuery 并且可以正常工作。它工作一次并通过 ajax 返回一个局部 View .html(re
我是一名优秀的程序员,十分优秀!