简体   繁体   中英

Listview's row item imageview change background android

I have a listview with three icons per every row. If I press an icon(play) the icon needs to change the background to another(stop). And then if I click the same icon in a different row, the icon that is changed before needs to switch to the previous background (stop to play), and the newly clicked icon needs to change the background to second state(play to stop).

Here is the code i'm using but seems it is not working properly. It changes the clicked one but if I click the same icon in a new row it doesn't changes the icon in the previously clicked row.

From the adapter:

playSong.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mediaPlayer.isPlaying() && mediaPlayer != null) {
                    mediaPlayer.release();
                    if (oldPosition != -1) {
                        ((AlbumDetails) activity).changePicToPlay(oldPosition,
                                playSong);
                    }
                    mediaPlayer = new MediaPlayer();
                } else {
                    oldPosition = position;
                    try {
                        mediaPlayer.setDataSource(data.get(position)
                                .getSONG_MP3());
                        final ProgressDialog bufferingDialog = new ProgressDialog(
                                activity);

                        bufferingDialog.setMessage(activity
                                .getString(R.string.buffering));
                        bufferingDialog.show();
                        mediaPlayer.prepareAsync();
                        mediaPlayer
                                .setOnPreparedListener(new OnPreparedListener() {

                                    @Override
                                    public void onPrepared(MediaPlayer arg0) {
                                        bufferingDialog.dismiss();
                                        mediaPlayer.start();

                                    }
                                });
                        postData(1, position);
                        mediaPlayer
                                .setOnCompletionListener(new OnCompletionListener() {

                                    @Override
                                    public void onCompletion(MediaPlayer arg0) {
                                        playSong.setBackgroundResource(R.drawable.play_nr);
                                        mediaPlayer.release();
                                    }
                                });
                        ((AlbumDetails) activity).changePicToStop(oldPosition);
                    } catch (IllegalArgumentException e) {
                        e.printStackTrace();
                    } catch (SecurityException e) {
                        e.printStackTrace();
                    } catch (IllegalStateException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        });

From my activity:

public void changePicToStop(int position) {
        View vi = listView.getChildAt(position);
        if (vi != null) {
            ImageView playSong = (ImageView) vi
                    .findViewById(R.id.songs_item_play);
            playSong.setBackgroundResource(R.drawable.stop);
        }
    }

    public void changePicToPlay(int position, ImageView image) {
        View vi = listView.getChildAt(position);
        if (vi != null) {
            image.setBackgroundResource(R.drawable.play_nr);
            listView.refreshDrawableState();
        }
    }

simply save the active position whereever you use your listadapter - then add some switch to your getView() Method like :

if (position==activePosition)
{
    playSong.setBackgroundResource(R.drawable.play_nr);
}
else
{
    playSong.setBackgroundResource(R.drawable.stop);
}

with your

public void changePicToPlay(int position) {
   activePosition = position;
   adapter.notifyDataSetChanged();
}

you don't have to set the mediaplayers listeners so often then too - in onCompletion you could simply call:

activePostion=-1;
adapter.notifyDataSetChanged();

here are the same solution about your question. There is changing backgroundColor, you can replace it with setBackground();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM