安卓:处理回收的ListView [英] Android: Handle ListView Recycle

查看:302
本文介绍了安卓:处理回收的ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发中,我使用列表视图活动的音板的应用程序。但由于Android的列表视图都有其回收列表视图,财产的我做出选择的TextView的变化而得到滚动列表视图反映在所有的页面。我不希望这种情况发生。那么,如何妥善处理。如果有人能帮助我走出code段,这将是非常有益的,我AP preciate你的努力。谢谢!

编辑:希望这将更好地解释

我的类扩展ListActivity和列表视图如下:

  [图片] [文字]
[图片] [文字]
[图片] [文字]

现在,当用户点击任何TextView的,我需要改变的TextView的形象。我实现了由以下code。

 公共无效onItemClick(适配器视图<>母公司,观景,
           INT位置,长的id){TextView的电视=(TextView的)视图。
//这里我改变形象与电视作为参考
最终资源解析度= getBaseContext()getResources()。
    最终绘制对象MYIMAGE = res.getDrawable(R.drawable.pause);
  // MYIMAGE。
    最终绘制对象myImage1 = res.getDrawable(R.drawable.play);    tv.setCompoundDrawablesWithIntrinsicBounds(MYIMAGE,NULL,NULL,NULL);    MediaPlayer.OnCompletionListener监听器=新MediaPlayer.OnCompletionListener(){
      公共无效onCompletion(MediaPlayer的MP){         tv.setCompoundDrawablesWithIntrinsicBounds(myImage1,NULL,NULL,NULL);
      }
    };
}


解决方案

简短的回答:有你的列表的适配器处理更新的数据,以正确地呈现变化。也就是说,覆盖/编辑getView()方法,如果有必要知道如何处理数据的变化,编辑适配器背后的数据,调用notifyDataSetChanged(),让那些在getView被调用更改传播到视图中的数据。现在,你可能通过手工修改的意见,在不改变基础数据,这是违反MVC模式而不考虑视图回收位。

由于你一般怎么问这个问题,但是,它会是相当困难的,给你适合你的特殊情况下,code样本。

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);


      }
    };
}

解决方案

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.

这篇关于安卓:处理回收的ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆