如何不调用notifyDataSetChanged()时更新ListView的某一项数据

本文介绍了一种更新Android ListView中单个条目的方法,避免了整个列表的刷新,提高了UI响应速度。这种方法利用了getChildAt和getFirstVisiblePosition等API来直接定位并修改可见区域内的条目。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

通过结合使用View#getChildAt(int index)和ListView#getFirstVisiblePosition(). 
示例如下:

public boolean updateListView(int position, int newProgress) {
int first = mListView.getFirstVisiblePosition();
int last = mListView.getLastVisiblePosition();
if(position < first || position > last) {
    //just update your DataSet
    //the next time getView is called
    //the ui is updated automatically
    return false;
}
else {
    View convertView = mListView.getChildAt(position - first);
    //this is the convertView that you previously returned in getView
    //just fix it (for example:)
    ProgressBar bar = (ProgressBar) convertView.findViewById(R.id.progress);
    bar.setProgress(newProgress);
    return true;
}
}

方法取自StackOverFlow:
[1] http://stackoverflow.com/questions/3724874/how-can-i-update-a-single-row-in-a-listview
[2]http://stackoverflow.com/questions/19025841/how-to-update-some-data-in-a-listview-without-using-notifydatasetchanged

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值