ImageView Toggle inside listview

て烟熏妆下的殇ゞ 提交于 2019-12-25 07:46:45

问题


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.



来源:https://stackoverflow.com/questions/42595672/imageview-toggle-inside-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!