S.R
S.R

Reputation: 2829

Toggle ImageView Button inside custom listview

I have a List view of ringtones with a play image view in each row for each ringtone ..

This is the view of it..

This is the view of it

Now obviously when user clicks on a play button it should switch into pause button.. I have implemented this and it's all good so far :

Interface(In adapter)

       public interface PlayPauseClick {
         void playPauseOnClick(int position);
        }
   private PlayPauseClick callback;
   public void setPlayPauseClickListener(PlayPauseClick listener) {
        this.callback = listener;
    }

Adapter(In getView)

    Product product = (Product) mDataItems.get(position);
    holder.playPause=(ImageView)v.findViewById(R.id.playPause); 
    holder.playPause.setImageResource(product.getPlayPauseId());
    holder.playPause.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
              if (callback != null) {
                  callback.playPauseOnClick(position);
              }      
        }
    });

Activity

    @Override
public void playPauseOnClick(int position) {
    final Product product = productList.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();     
    };

Now I have a problem :

I don't know how to track the currently playing row,I mean if user wants to play a song from another row, first I have to change currently pause toggle to play, then play the new one.

Can you help me with this please !?

Upvotes: 0

Views: 244

Answers (3)

S.R
S.R

Reputation: 2829

After some digging I found Two answers..

1: We can store the index of the current song which is played in the adapter.

So we just need to turn on pause when we click on an other button.

In Adapter :

   int currentPosition = -1;
...
if (callback != null) {
  callback.playPauseOnClick(position);
  if (currentPosition == -1){
       currentPosition = position;
  } else if (currentPosition == position){
       currentPosition = -1;
  } else if (currentPosition != -1 && currentPosition != position){
       callback.playPauseOnClick(currentPosition);
       currentPosition = position;
  }
} 

2: Set resources for every listview data item

inside callback or anywhere can get listview data:

@Override
public void playPauseOnClick(int position) {
    final Product product = productList.get(position);
    if (product.paused) {
        for(int i=0;i< productList.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

Michael Vescovo
Michael Vescovo

Reputation: 3941

You could hold a variable "mCurrentPosition" to hold the currently playing track.

Then create two separate methods, one for play and one for pause. Simply pause the current track and play the one that was clicked.

Remember to also set the current position when you play a new track.

Something like this:

@Override
public void onClick(View v) {
      if (callback != null) {
          callback.pause(mCurrentPosition);
          mCurrentPosition = position;
          callback.play(position);
      }      
}

You may need to adjust it a bit as I don't know how you've setup your "callback" or where "position" is coming from. I'm assuming something like "getAdapterPosition()". But hopefully you can see how to do it by this example. If not please comment.

Edit in response to updated question:

For the interface do something like this:

public interface PlayPauseClick {
    void pause(int position);
    void play(int position);
}

Edit2:

@Override
void pause(int position) {
    if(position != null) {
        Product product = productList.get(position);
        product.setPlayPauseId(R.drawable.ic_play);
        product.paused = true;
        adapter.notifyDataSetChanged();
    }
}

@Override
void play(int position) {
    // Homework exercise.
}

Upvotes: 0

Kiran Benny Joseph
Kiran Benny Joseph

Reputation: 6823

You can do this first, declare a global variable

int lastposition=null;

then

 @Override
public void playPauseOnClick(int position) {
    final Product product = productList.get(position);
                if(lastposition!=null)
                {
                     Product previous = productList.get(lastposition);
                     previous.setPlayPauseId(R.drawable.ic_pause);
                     previous.paused=false;
                     lastposition=position;
                } else lastposition=position;
                if (product.paused) {
                    //new loadSingleView().execute(product.image_url);
                    //Log.d(LOG, product.image_url);
                    product.setPlayPauseId(R.drawable.ic_pause);
                    product.paused=false;
                }else {
                    product.setPlayPauseId(R.drawable.ic_play);
                    product.paused = true;
                }
                adapter.notifyDataSetChanged();     
    };

Replace the above code with code in your activity

Upvotes: 0

Related Questions