代码之家  ›  专栏  ›  技术社区  ›  Code Lover

listview外部的Listen按钮并修改第一项

  •  0
  • Code Lover  · 技术社区  · 7 年前

    我想 Play (只需更改按钮图标)中的第一项 ListView 当击中 Play All button which is outside of the

    enter image description here

    你可以在上找到整个项目 Bitbucket . 虽然在这里添加了一些必需的代码。

    活动_相册.xml

    <?xml version="1.0" encoding="utf-8"?><!-- Root Container - ConstraintLayout star -->
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:orientation="vertical"
        tools:context=".DreamVoyage">
    
        <android.support.constraint.ConstraintLayout
            android:id="@+id/album_cover_layout"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:layout_constraintHeight_percent="0.3">
    
            <ImageView
                android:id="@+id/album_cover"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:scaleType="centerCrop"
                android:src="@drawable/album_four" />
    
            <View
                android:id="@+id/view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#80000000" />
    
            <TextView
                android:id="@+id/album_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:layout_marginEnd="8dp"
                android:text="Album Title"
                android:textColor="#FFFFFF"
                android:textStyle="bold"
                android:textSize="24sp"
                app:layout_constraintBottom_toTopOf="@+id/album_band"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="@+id/album_cover" />
    
            <TextView
                android:id="@+id/album_band"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="24dp"
                android:text="Album Band"
                android:textColor="#FFFFFF"
                android:textSize="14sp"
                app:layout_constraintBottom_toTopOf="@+id/album_play_all_button"
                app:layout_constraintEnd_toEndOf="@+id/album_title"
                app:layout_constraintStart_toStartOf="@+id/album_title" />
    
            <Button
                android:id="@+id/album_play_all_button"
                style="@style/AppTheme.RoundedCornerMaterialButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:layout_marginEnd="8dp"
                android:layout_marginBottom="24dp"
                android:text="Play ALl"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="@+id/view"
                app:layout_constraintStart_toStartOf="parent" />
    
        </android.support.constraint.ConstraintLayout>
    
        <!-- ScrollView start -->
        <ScrollView
            android:id="@+id/playlist"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:fillViewport="true"
            android:orientation="vertical"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHeight_percent="0.7"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/album_cover_layout">
    
            <!-- playlist layout start -->
            <ListView
                android:id="@+id/playlist_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
    
            </ListView><!-- playlist layout end -->
    
        </ScrollView><!-- ScrollView end -->
    
    </android.support.constraint.ConstraintLayout><!-- Root Container - ConstraintLayout star -->
    

    SongAdapter.java

    public class SongAdapter extends ArrayAdapter<Song> {
    
        int     playingIndex = -1;
        boolean currentTrack = false;
    
        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;
            final ViewHolder holder;
    
            if (playlistItemView == null) {
                playlistItemView = LayoutInflater.from(getContext()).inflate(R.layout.playlist_item, parent, false);
    
                holder = new ViewHolder();
    
                holder.albumCoverThumbnail = playlistItemView.findViewById(R.id.playlist_album_thumbnail);
                holder.songTitle = playlistItemView.findViewById(R.id.playlist_song_title);
                holder.songAlbumTitle = playlistItemView.findViewById(R.id.playlist_song_album_title);
                holder.songArtist = playlistItemView.findViewById(R.id.playlist_song_artist);
                holder.songPlayButton = playlistItemView.findViewById(R.id.playlist_play_button);
    
                playlistItemView.setTag(holder);
            } else {
                holder = (ViewHolder) playlistItemView.getTag();
            }
    
            final Song currentSong = getItem(position);
    
            // set data to the list item
            assert currentSong != null;
            holder.albumCoverThumbnail.setImageResource(currentSong.getSongAlbumCoverId());
            holder.songTitle.setText(currentSong.getSongTitle());
            holder.songAlbumTitle.setText(currentSong.getSongAlbumTitle());
            holder.songArtist.setText(currentSong.getSongSingers());
    
            // check the play status of the song item
            if (playingIndex == position) {
                holder.songPlayButton.setImageResource(R.drawable.ic_pause_black_24dp);
            } else {
                holder.songPlayButton.setImageResource(R.drawable.ic_play_arrow_black_24dp);
            }
    
            // set song button action
            holder.songPlayButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                    holder.songPlayButton.setImageResource(R.drawable.ic_pause_black_24dp);
    
                    // pause current song on other click
                    if (position == playingIndex) {
                        playingIndex = -1;
                    } else {
                        playingIndex = position;
                        notifyDataSetChanged();
                    }
    
                    // pause play current track
                    if (currentTrack) {
                        holder.songPlayButton.setImageResource(R.drawable.ic_play_arrow_black_24dp);
                        currentTrack = !currentTrack;
                    } else {
                        holder.songPlayButton.setImageResource(R.drawable.ic_pause_black_24dp);
                    }
                    currentTrack = !currentTrack;
                }
            });
    
            return playlistItemView;
        }
    
        static class ViewHolder {
            ImageView   albumCoverThumbnail;
            TextView    songTitle;
            TextView    songAlbumTitle;
            TextView    songArtist;
            ImageButton songPlayButton;
        }
    }
    

    希登特鲁斯.java

    public class HiddenTruth extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_album);
    
            // 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_two_title");
                String albumBand = bundle.getString("album_two_band");
                int albumCover = bundle.getInt("album_two_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>();
    
                // get the thumbnail resource id
                int albumThumb = R.drawable.album_two_thumbnail;
    
                songs.add(new Song(albumTitle, "Hidden valley in the north", "Nicolo Viarina", albumThumb));
                songs.add(new Song(albumTitle, "The sea and the sky", "Mali nigara", albumThumb));
                songs.add(new Song(albumTitle, "Hidden valley in the north", "Nicolo Viarina", albumThumb));
                songs.add(new Song(albumTitle, "The sea and the sky", "Mali nigara", albumThumb));
                songs.add(new Song(albumTitle, "Hidden valley in the north", "Nicolo Viarina", albumThumb));
                songs.add(new Song(albumTitle, "The sea and the sky", "Mali nigara", albumThumb));
                songs.add(new Song(albumTitle, "Hidden valley in the north", "Nicolo Viarina", albumThumb));
                songs.add(new Song(albumTitle, "The sea and the sky", "Mali nigara", albumThumb));
                songs.add(new Song(albumTitle, "Hidden valley in the north", "Nicolo Viarina", albumThumb));
                songs.add(new Song(albumTitle, "The sea and the sky", "Mali nigara", albumThumb));
                songs.add(new Song(albumTitle, "Hidden valley in the north", "Nicolo Viarina", albumThumb));
                songs.add(new Song(albumTitle, "The sea and the sky", "Mali nigara", albumThumb));
                songs.add(new Song(albumTitle, "Hidden valley in the north", "Nicolo Viarina", albumThumb));
                songs.add(new Song(albumTitle, "The sea and the sky", "Mali nigara", albumThumb));
                songs.add(new Song(albumTitle, "Hidden valley in the north", "Nicolo Viarina", albumThumb));
                songs.add(new Song(albumTitle, "The sea and the sky", "Mali nigara", albumThumb));
                songs.add(new Song(albumTitle, "Hidden valley in the north", "Nicolo Viarina", albumThumb));
                songs.add(new Song(albumTitle, "The sea and the sky", "Mali nigara", albumThumb));
    
                SongAdapter songAdapter = new SongAdapter(this, 0, songs);
    
                ListView listView = findViewById(R.id.playlist_view);
                listView.setAdapter(songAdapter);
    
            } // @TODO move this brace after albumBand.setText(bundle.getString("album_two_band"));
        }
    }
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Code Lover    7 年前

    答案就在这里。

    我已将另一个构造函数添加到 SongAdapter

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

    比添加到下面

    // play first track on Play All button clicked
    if (isAllPlayClicked) {
        playingIndex = 0;
        holder.songPlayButton.setImageResource(R.drawable.ic_pause_black_24dp);
        isAllPlayClicked = false;
        currentTrack = !currentTrack;
    }
    

    // get the Play All button
    Button playAll = findViewById(R.id.album_play_all_button);
    
    // Play All button click event
    playAll.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            SongAdapter newAdapter = new SongAdapter(HiddenTruth.this, 0, songs, true);
            listView.setAdapter(newAdapter);
        }
    });
    

    我希望这会对某人有所帮助。