Android:处理ListView回收

发布于 2024-10-06 18:22:29 字数 1086 浏览 4 评论 0原文

我正在开发一个音板应用程序,在其中使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦中楼上月下 2024-10-13 18:22:29

简短的回答:让列表的适配器处理数据更新以正确呈现更改。也就是说,如果需要了解如何处理数据更改,请重写/编辑 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文