Majid
Majid

Reputation: 201

play pause toggle in android listview

I have a listview with a play image button in every row ...

Play and Pause function

@Override
public void playPauseOnClick(int position) {
   Product product = songList.get(position);
    if (product.paused) {
        product.setPlayPauseId(R.drawable.ic_pause);
        product.paused=false;
    }else {
        product.setPlayPauseId(R.drawable.ic_play);
        product.paused = true;
    }
    adapter.notifyDataSetChanged();
}

This works fine and I have no problem...

But now what I need to achieve is to set the previously clicked position back into Play icon before setting the new position to Pause.

Can you help me with this, please !!?

Upvotes: 1

Views: 446

Answers (2)

S.R
S.R

Reputation: 2829

I've modified Hitesh Gehlot's answer:

@Override
public void playPauseOnClick(int position) {
    Product product = movieList.get(position);
    if (product.paused) {
        for(int i=0;i< movieList.size();i++)
        {
            movieList.get(i).setPlayPauseId(R.drawable.ic_play);
            movieList.get(i).paused = true;
        }
        product.setPlayPauseId(R.drawable.ic_pause);
        product.paused=false;
    }else {
        product.setPlayPauseId(R.drawable.ic_play);
        product.paused = true;
    }
    adapter.notifyDataSetChanged();
}

Upvotes: 1

Hitesh Gehlot
Hitesh Gehlot

Reputation: 1347

try this-

 @Override
public void playPauseOnClick(int position) {
    Product product = songList.get(position);
    for(int i=0;i< songList.size();i++)
    {
        songList.get(i).product.setPlayPauseId(R.drawable.ic_play);
        songList.get(i).product.paused = true;
    }

    if (product.paused) {
        product.setPlayPauseId(R.drawable.ic_pause);
        product.paused = false;
    } else {
        product.setPlayPauseId(R.drawable.ic_play);
        product.paused = true;
    }
    adapter.notifyDataSetChanged();
}

Upvotes: 1

Related Questions