列表视图回收和重复显示的图像视图、图像、列表

2023-09-04 03:06:36 作者:痞子梦

我创建了一个列表视图其中包括曲目列表以及播放和暂停image.When点击播放图像,暂停图像变得可见,点击暂停,播放的图像会变得visible.Issue是,当我点击播放,滚动下来,我看到另一个列表项显示暂停图像,当我向下滚动更多其他列表项显示暂停image.It是因为列表recylces重复images.On研究这个问题我想我需要把if-else语句中的getView的节目的方法,但我没有执行正确的code,以解决此issue..plzz帮助我的code在THT ..

I have created a listview which consists of list of tracks and play and pause image.When clicked on play image, pause image becomes visible and clicking on pause ,play image will become visible.Issue is that when i click on play and scrolls down i see another list item showing pause image and when i scroll down more another list item showing pause image.It is because list recylces an shows duplicated images.On studying the issue i think i need to place if-else statement in the getView method but i failed to implement correct code to resolve this issue..plzz help me with code in tht..

推荐答案

的名称创建在 SoundCloud 类的新布尔变量 IsPlaying模块。它创建的getter setter方法​​,就像你做了其他变量。

Create a new boolean variable in your SoundCloud class by the name isPlaying. Create getter setters for it, like you have done for other variables.

获取从 SoundCloudList 的对象是这样的:

Get the object from SoundCloudList like this:

SoundCloug soundCloud = (SoundCloud) getItem(position);

这个对象可以在任何地方使用你正在使用 soundcloudList.get(位置),这样可以很容易,因为我们没有获取对象每次。

This object can be used everywhere you are using soundcloudList.get(position), so that makes it easy to because we don't have to fetch object everytime.

然后在你的 getView 使用 IsPlaying模块来显示在每个位置如下图所示播放/暂停按钮:

Then in your getView use isPlaying to show play/pause button on every position like below:

if(soundCloud.isPlaying()){
    holder.img1.setVisibility(View.VISIBLE);
    holder.img2.setVisibility(View.GONE);
}
else{
    holder.img1.setVisibility(View.GONE);
    holder.img2.setVisibility(View.VISIBLE);
}

,然后在 onClickListeners IsPlaying模块这样的设定值:

holder.img1.setOnClickListener(new View.OnClickListener()
{
    @Override
    public void onClick(final View v) {

        soundCloud.setPlaying(true);
        try {
            notifyDataSetChanged();
            holder.img1.setVisibility(View.INVISIBLE);
            holder.img2.setVisibility(View.VISIBLE);
            mMediaPlayer = new MediaPlayer();
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mMediaPlayer.setDataSource("http://api.soundcloud.com/tracks/" + soundcloudList.get(position).getId() + "/stream?client_id=e13865f9debacb5f96375fdd96b7fa1b");
            mMediaPlayer.prepareAsync();
            mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener()
            {
                @Override
                public void onPrepared(MediaPlayer mp)
                {
                    mp.start();

                }
            });
            mMediaPlayer.setOnCompletionListener(
                    new MediaPlayer.OnCompletionListener()
                    {
                        @Override
                        public void onCompletion(MediaPlayer mp)
                        {
                            mMediaPlayer.release();
                            mMediaPlayer = null;
                            holder.img1.setVisibility(View.VISIBLE);
                        }
                    });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

});

holder.img2.setOnClickListener(new View.OnClickListener()
{
    @Override
    public void onClick(final View v)
    {
        soundCloud.setPlaying(false);
        notifyDataSetChanged();
        holder.img1.setVisibility(View.VISIBLE);
        holder.img2.setVisibility(View.INVISIBLE);
        if(mMediaPlayer!=null)
        {
            mMediaPlayer.release();
            mMediaPlayer = null;
        }
    }

});

这code将不显示播放/暂停按钮复制在多个行,因为现在,我们正在检查在 getView 才把设置按钮的可见性。

This code will not show the play/pause button duplicated on multiple rows, because now, we are checking the play/pause of audio for every row in getView and only then setting the buttons visibility.