Android:处理ListView回收
我正在开发一个音板应用程序,在其中使用 Listview Activity。但由于 Android 的 Listview 具有回收其列表视图的属性,我对所选文本视图所做的更改会在滚动列表视图时反映在所有页面中。我不希望发生这种情况。那么我该如何正确处理呢。如果有人可以帮助我解决代码片段,这将非常有帮助,我感谢您的努力。谢谢你!
编辑:希望这能更好地解释
我的类扩展了 ListActivity ,列表视图如下所示
[Image] [Text]
[Image] [Text]
[Image] [Text]
现在,当用户单击任何文本视图时,我需要更改该文本视图的图像。我通过以下代码实现它。
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
TextView tv = (TextView) view;
//Here I change the image with tv as reference
final Resources res = getBaseContext().getResources();
final Drawable myImage = res.getDrawable(R.drawable.pause);
// myImage.
final Drawable myImage1 = res.getDrawable(R.drawable.play);
tv.setCompoundDrawablesWithIntrinsicBounds(myImage, null, null, null);
MediaPlayer.OnCompletionListener listener = new MediaPlayer.OnCompletionListener(){
public void onCompletion(MediaPlayer mp) {
tv.setCompoundDrawablesWithIntrinsicBounds(myImage1, null, null, null);
}
};
}
I am developing a soundboard application in which I use Listview Activity. But since Listview of Android has the property of recycling its listviews, the changes I make to the selected textview gets reflected in all the pages while scrolling the listview. I don't want this to happen. So how do I handle it properly. If somebody can help me out with code snippet, it will be really helpful and I appreciate your effort. Thank you!
Edit: Hope this will explain better
My class extends ListActivity and the listview is as follows
[Image] [Text]
[Image] [Text]
[Image] [Text]
Now when the user clicks on any textview, I need to change the image of that textview. I implement that by the following code.
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
TextView tv = (TextView) view;
//Here I change the image with tv as reference
final Resources res = getBaseContext().getResources();
final Drawable myImage = res.getDrawable(R.drawable.pause);
// myImage.
final Drawable myImage1 = res.getDrawable(R.drawable.play);
tv.setCompoundDrawablesWithIntrinsicBounds(myImage, null, null, null);
MediaPlayer.OnCompletionListener listener = new MediaPlayer.OnCompletionListener(){
public void onCompletion(MediaPlayer mp) {
tv.setCompoundDrawablesWithIntrinsicBounds(myImage1, null, null, null);
}
};
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简短的回答:让列表的适配器处理数据更新以正确呈现更改。也就是说,如果需要了解如何处理数据更改,请重写/编辑 getView() 方法,编辑适配器底层的数据,调用 notificationDataSetChanged(),并在调用 getView 时让这些数据更改向下传播到视图。现在,您可能正在手动修改视图而不更改底层数据,这违反了 MVC 模式,无论视图回收位如何。
不过,考虑到您提出这个问题的普遍性,为您提供适合您的特定情况的代码示例将相当困难。
Short answer: Have your list's adapter handle updates to the data to render the changes correctly. That is, override/edit the getView() method if necessary to know how to handle the data change, edit the data underlying your Adapter, call notifyDataSetChanged(), and let those data changes propagate down to views when getView gets called. Right now, you're probably hand-modifying the views without changing the underlying data, which is a violation of MVC patterns regardless of the view-recycling bits.
Given how generically you asked the question, though, it'd be rather difficult to give you a code sample that fits your particular case.