【问题标题】:Change every image view resources inside listview with one click一键更改列表视图中的每个图像视图资源
【发布时间】:2017-04-11 15:25:25
【问题描述】:

我有一个歌曲列表视图,每行都有播放和暂停按钮。 我的列表视图中不能有两个暂停图标(两首播放歌曲)所以我需要先将它们全部重置为播放图标,然后将所选视图设置为暂停图标.. 我怎样才能做到这一点 ?或者您能为此提供更好的解决方案吗?

这是我的代码:

在模型类(产品)中:

public int currentPosition= -1;

适配器中:

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


    holder.playPauseHive.setImageResource(product.getPlayPauseId());
    holder.playPauseHive.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (callback != null) {
                callback.playPauseOnClick(position);
                if (position == product.currentPosition) {
                    product.setPlayPauseId(R.drawable.ic_pause);
                    //set the image to pause icon
                }else{
                    //set the image to play icon
                    product.setPlayPauseId(R.drawable.ic_play);
                }
                notifyDataSetChanged();
            }
        }
    });

我的 Activity 中的回调:

@Override
public void playPauseOnClick(int position) {
    final Product product = songList.get(position);
    if(product.currentPosition == position){
        product.currentPosition = -1; //pause the currently playing item
    }else{
        product.currentPosition = position; //play the item
    }
    this.adapter.notifyDataSetChanged();
}

【问题讨论】:

    标签: android listview adapter custom-adapter


    【解决方案1】:

    就我而言,我使用一个变量来存储当前播放项目的位置。让我们说

    int x = -1; //-1 can indicate nothing was currently playing
    

    所以在playPauseOnClick() 你可以做这样的事情

    @Override
    public void playPauseOnClick(int position) {
        if(x == position){
            x = -1; //pause the currently playing item
        }else{
            x = position; //play the item
        }
        this.adapter.notifyDataSetChanged();
    }  
    

    我删除product.setPlayPauseId() 的原因是因为你真的不需要它们。您只需要根据我之前创建的 x 变量设置播放或暂停图标。在你的getView() 中做这样的事情

    Product product = songList.get(position);
    if (position == x) { 
    
        //set the image to pause icon
    }else{
        //set the image to play icon
    }
    

    因此,一旦您致电adapter.notifyDataSetChanged(),您的适配器就会为您完成所有工作。每当x变量的值为-1时,没有一个图标会显示暂停图标,这也可以确保只有一个位置也会显示暂停图标。

    希望对你有帮助。

    【讨论】:

    • 我已经根据您的回答编辑了我的代码和我的问题,但仍然没有机会..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 2015-10-14
    • 2015-05-24
    • 2015-06-10
    相关资源
    最近更新 更多