ImageView Toggle inside listview

2019-09-14 20:26发布

问题:

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

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 ..

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 !?

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

回答1:

You can store the index of the current song which is played in the adapter.

So you just need to turn on pause when you 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:

Create in Adapter method

public void update (ArrayList<Product> productList) {
  this.productList.clear();
  this.productList.addAll(productList);
  notifyDataSetChanged();  
}

call method in playPauseOnClick

adapter.update(productList);


回答3:

This a solution:

In your activity create a class field private Product mCurrentProduct;

then :

@Override
public void playPauseOnClick(int position) {
    final Product product = productList.get(position);

                if (mCurrentProduct == null) {
                    mCurrentProduct = product)
                } 

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

                    product.setPlayPauseId(R.drawable.ic_pause);
                    product.paused=false;

                    mCurrentProduct = product;
                }

                adapter.notifyDataSetChanged();     
    };

Hope this helps.

Sorry for my poor english.