Android-ArrayAdapter在ListView的播放和暂停之间切换ImageButton src

时间:2018-12-10 07:41:58

标签: java android

我仍在学习android。在这里,我需要帮助来在Play的{​​{1}}中的PauseImageButton源之间切换。

  

一次只能在播放状态下播放一首歌曲。因此,如果单击其他,则应停止。

enter image description here

SongAdapter.java

ListView

Song.java

public class SongAdapter extends ArrayAdapter<Song> {

    public SongAdapter(@NonNull Context context, int resource, @NonNull List<Song> objects) {
        super(context, resource, objects);
    }

    @NonNull
    @Override
    public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        View playlistItemView = convertView;

        if (playlistItemView == null) {
            playlistItemView = LayoutInflater.from(getContext()).inflate(R.layout.playlist_item, parent, false);
        }

        Song currentSong = getItem(position);

        // get list item elements
        ImageView albumCoverThumbnail = playlistItemView.findViewById(R.id.playlist_album_thumbnail);
        TextView songTitle = playlistItemView.findViewById(R.id.playlist_song_title);
        TextView songAlbumTitle = playlistItemView.findViewById(R.id.playlist_song_album_title);
        TextView songArtist = playlistItemView.findViewById(R.id.playlist_song_artist);
        final ImageButton songPlayButton = playlistItemView.findViewById(R.id.playlist_play_button);

        // set data to the list item
        assert currentSong != null;
        albumCoverThumbnail.setImageResource(currentSong.getSongAlbumCoverId());
        songTitle.setText(currentSong.getSongTitle());
        songAlbumTitle.setText(currentSong.getSongAlbumTitle());
        songArtist.setText(currentSong.getSongSingers());

        // set song button action
        songPlayButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                songPlayButton.setImageResource(R.drawable.ic_pause_black_24dp);
                Toast.makeText(getContext(), "Button clicked for item " + position, Toast.LENGTH_LONG).show();
            }
        });

        return playlistItemView;
    }

}

Activity.java

public class Song {
    private String songAlbumTitle;
    private String songTitle;
    private String songSingers;
    private int    songAlbumCoverId;

    public Song(String albumTitle, String title, String singers, int albumCoverId) {

        songAlbumTitle = albumTitle;
        songTitle = title;
        songSingers = singers;
        songAlbumCoverId = albumCoverId;

    }

    public String getSongAlbumTitle() {
        return songAlbumTitle;
    }

    public String getSongTitle() {
        return songTitle;
    }

    public String getSongSingers() {
        return songSingers;
    }

    public int getSongAlbumCoverId() {
        return songAlbumCoverId;
    }
}

playlist_item.xml

public class DreamVoyage extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dream_voyage);

        // get view ids
        ImageView albumCoverImage = findViewById(R.id.album_cover);

        // get intent extras
        Bundle bundle = getIntent().getExtras();
        // check if bundle in not null and containing value
        if (bundle != null) {

            String albumTitle = bundle.getString("album_one_title");
            String albumBand = bundle.getString("album_one_band");
            int albumCover = bundle.getInt("album_one_cover");

            albumCoverImage.setImageResource(albumCover);

            TextView albumTitleText = findViewById(R.id.album_title);
            TextView albumBandText = findViewById(R.id.album_band);

            albumTitleText.setText(albumTitle);
            albumBandText.setText(albumBand);

            ArrayList<Song> songs = new ArrayList<Song>();

            songs.add(new Song(albumTitle, "I do it for you", "Bryn Adams", albumCover));
            songs.add(new Song(albumTitle, "Here I am", "Bryn Adams", albumCover));

            SongAdapter songAdapter = new SongAdapter(this, 0, songs);

            ListView listView = findViewById(R.id.playlist_view);
            listView.setAdapter(songAdapter);


        }
    }
}

1 个答案:

答案 0 :(得分:1)

使用变量存储当前的播放项目索引

SongAdapter {
   int playingIndex = -1 //-1 means no song is playing
   .
   .
   .
}

根据playingIndex设置播放/暂停可绘制对象,并在playingIndex中设置songPlayButton.setOnClickListener

public View getView(final int position, ...) {
    if(playingIndex == position)
       songPlayButton.setImageResource(R.drawable.ic_play_black_24dp);
    else
       songPlayButton.setImageResource(R.drawable.ic_pause_black_24dp);

    songPlayButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(position == playIndex)
                playIndex = -1;
            else
                playIndex = position;
            notifyDataSetChanged();
        }
    });
}

它应该完成工作。但这会强制重新绘制所有行。在RecyclerView中,有一种notifyItemChanged方法可以强制在单个项目上重绘。

您可以尝试使用此处的方法更新单行https://stackoverflow.com/a/3727813/4907678

我的建议是迁移到RecyclerView并使用notifyItemChanged

欢呼!