Актуализировать пользовательское всплывающее окно и сохранять его постоянным - PullRequest
0 голосов
/ 26 апреля 2020

У меня есть действие с нижним меню, и когда я нажимаю на один из элементов этого меню, оно показывает мне пользовательское всплывающее окно, где я прошу пользователя выбрать musi c на своем телефоне. Более того, как только пользователь выбирает файл, я меняю видимость включаемого макета (находится в файле xml пользовательского всплывающего окна) на VISIBLE (который представляет медиаплеер). Однако, когда я закрываю всплывающее окно, макет включения больше не виден.

Итак, как мне организовать свой код, чтобы он отображал медиаплеер каждый раз, когда пользователь нажимает на меню, если он выбрал музыку c?


Это мой метод действий для показать всплывающее окно, когда пользователь нажимает на меню:

public void launch_music_popup_activity(MenuItem item) {
        LoadMusicPopup musicPopup = new LoadMusicPopup();
        musicPopup.show(getSupportFragmentManager(), "new Load Music Bottom Popup");
    }

Это мое настраиваемое всплывающее окно (LoadMusicPopup):

import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.android.material.bottomsheet.BottomSheetDialogFragment;

import java.io.File;
import java.io.IOException;
import java.util.Objects;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import static android.app.Activity.RESULT_OK;

public class LoadMusicPopup extends BottomSheetDialogFragment {

    private Button load_from_disk_button;
    private TextView music_title;
    private ImageView fast_rewind_music, fast_forward_music, play_pause_music;
    private Intent fileIntent;
    private MediaPlayer mediaPlayer = new MediaPlayer();
    private String localSongName;
    private LinearLayout music_player;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.load_music_popup, container, false);

        music_title = v.findViewById(R.id.music_title);
        fast_rewind_music = v.findViewById(R.id.fast_rewind_music);
        fast_forward_music = v.findViewById(R.id.fast_forward_music);
        play_pause_music = v.findViewById(R.id.play_pause_music);
        music_player = v.findViewById(R.id.music_player);

        load_from_disk_button = v.findViewById(R.id.load_from_disk_button);
        load_from_disk_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Load music from disk
                get_internal_music();
            }
        });

        return v;
    }

    private void get_internal_music() {
        fileIntent = new Intent(Intent.ACTION_GET_CONTENT);
        fileIntent.setType("audio/*");
        startActivityForResult(fileIntent, 10);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (requestCode == 10) {
            if(resultCode == RESULT_OK) {
                Uri localSongUri = Objects.requireNonNull(data).getData();
                localSongName = new File(Objects.requireNonNull(localSongUri).getPath()).getName();
                play_internal_music(localSongUri);
            }
        }
    }

    private void play_internal_music(Uri localSongUri) {
        try {
            mediaPlayer.reset();
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mediaPlayer.setDataSource(getContext(), localSongUri);
            mediaPlayer.prepare();
            mediaPlayer.start();
            display_music_player();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void display_music_player() {
        music_player.setVisibility(View.VISIBLE);
        music_title.setText(localSongName);
    }
}

и это мое настраиваемое всплывающее xml файл (load_music_popup) ):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingStart="24dp"
    android:paddingEnd="24dp"
    android:orientation="vertical"
    android:gravity="center_horizontal">

    <include android:id="@+id/music_player" layout="@layout/internal_music_player" android:visibility="gone"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:gravity="center_vertical"
        android:text="@string/load_music_by"
        android:paddingStart="16dp"
        android:paddingEnd="16dp"
        android:fontFamily="@font/openr"
        android:textColor="@color/grey_accent"/>

    <Button
        android:id="@+id/load_from_disk_button"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:backgroundTint="@color/white"
        android:text="@string/load_music_from_disk"
        android:fontFamily="@font/openr"
        android:textAllCaps="false"
        android:textColor="@color/black"
        android:drawableEnd="@drawable/ic_load_music_from_disk"
        android:textAlignment="textStart"
        style="?android:attr/borderlessButtonStyle"/>

    <Button
        android:id="@+id/load_from_soundcloud_button"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:textAllCaps="false"
        android:backgroundTint="@color/white"
        android:text="@string/load_music_from_soundcloud"
        android:fontFamily="@font/openr"
        android:textColor="@color/black"
        android:drawableEnd="@drawable/ic_load_music_from_soundcloud"
        android:textAlignment="textStart"
        style="?android:attr/borderlessButtonStyle"/>
</LinearLayout>

с включенным макетом:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingStart="24dp"
    android:paddingEnd="24dp"
    android:paddingTop="24dp"
    android:orientation="vertical"
    android:background="@drawable/bottom_sheet_background"
    android:gravity="center_horizontal">

    <TextView
        android:id="@+id/music_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Music Title"
        android:lines="1"
        android:paddingStart="16dp"
        android:paddingEnd="16dp"
        android:fontFamily="@font/openr"
        android:textColor="@color/black"/>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_marginBottom="15dp"
        android:layout_marginTop="15dp"
        android:layout_height="wrap_content"
        android:gravity="center_vertical">

        <ImageView
            android:id="@+id/fast_rewind_music"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            app:srcCompat="@drawable/ic_music_fast_rewind" />

        <ImageView
            android:id="@+id/play_pause_music"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingStart="24dp"
            android:paddingEnd="24dp"
            android:layout_weight="1"
            app:srcCompat="@drawable/ic_music_play" />

        <ImageView
            android:id="@+id/fast_forward_music"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            app:srcCompat="@drawable/ic_music_fast_forward" />
    </LinearLayout>

</LinearLayout>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...