Recyclerview中的对话框-IllegalStateException:指定的子代已经有一个父代

时间:2019-04-22 17:56:20

标签: java android android-recyclerview view dialog

我有一个对话框,我设置为在recyclerview中单击按钮以全屏播放视频。因此,每当我在视频播放器上单击全​​屏按钮时,它就会崩溃,IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

当我添加回相同视图时,我不想从recyclerview cuz中删除视图,因为它与recyclerview上的项目重叠。因此,我创建了一个新的视频播放器实例,并将其提供给对话框的addContentView方法,但它给出了相同的错误。

public class ExoPlayer_Main {

    public static final String TAG="###ExoPlayer###";
    MediaSource mediaSource;
    Context context;
    PlayerView playerView;
    SimpleExoPlayer player;
    boolean playWhenReady=false;
    long playbackPosition;
    int currentWindow;
    int newPlayerState=1;
    List<Uri> mediaList=new ArrayList<>();
    ComponentListener componentListener=new ComponentListener();
    private static final DefaultBandwidthMeter BANDWIDTH_METER = new DefaultBandwidthMeter();
    private FrameLayout mFullScreenButton;
    //Fullscreen Setting
    ImageView mFullScreenIcon;
    private boolean mExoPlayerFullscreen = false;
    private Dialog mFullScreenDialog;




    public ExoPlayer_Main(Context context, PlayerView playerView) {
        this.context = context;
        this.playerView=playerView;

    }

    public void initializePlayer(Uri uri,int mediaType){
        Log.d(TAG,"Init Player Calling ");
        if (player==null){
            player = ExoPlayerFactory.newSimpleInstance(context);           
            player.clearVideoSurface();
            player.addListener(componentListener);
            player.setPlayWhenReady(playWhenReady);
            player.seekTo(currentWindow,playbackPosition);
            player.clearVideoSurface();
            mediaSource = buildMediaSourceNew(uri,mediaType);
            playerView.setPlayer(player);
            player.prepare(mediaSource);
            initFullscreenButton();
        }


    }






    private void initFullscreenButton() {


        PlayerControlView controlView = playerView.findViewById(R.id.exo_controller);
        mFullScreenIcon = controlView.findViewById(R.id.exo_fullscreen_icon);
        mFullScreenButton = controlView.findViewById(R.id.exo_fullscreen_button);
        mFullScreenButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!mExoPlayerFullscreen){
                    openFullscreenDialog();
                }else{
                    closeFullscreenDialog();
                }

            }
        });

    }
    private void openFullscreenDialog() {
        initFullscreenDialog();
        ((ViewGroup)playerView.getParent()).removeView(playerView);
        player.setPlayWhenReady(false);
        mFullScreenDialog.addContentView(playerView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        mFullScreenIcon.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_fullscreen_skrink));
        mExoPlayerFullscreen = true;
        mFullScreenDialog.show();
    }


    private void closeFullscreenDialog() {

        ((ViewGroup) playerView.getParent()).removeView(playerView);
        FrameLayout playerAdd =  ((Activity)context).findViewById(R.id.main_media_frame);
        playerAdd.addView(playerView);
        mExoPlayerFullscreen = false;
        mFullScreenDialog.dismiss();
        mFullScreenIcon.setImageDrawable(context.getResources().getDrawable(R.drawable.ic_fullscreen_expand));
    }

    private void initFullscreenDialog() {

        mFullScreenDialog = new Dialog(context, android.R.style.Theme_Black_NoTitleBar_Fullscreen) {
            public void onBackPressed() {
                if (mExoPlayerFullscreen)
                    closeFullscreenDialog();
                super.onBackPressed();
            }
        };
    }

我需要使用视频播放器打开全屏对话框,当我关闭视频时,它不会弄乱我的recyclerview

1 个答案:

答案 0 :(得分:-1)

我没有在您的代码上找到问题,但是创建全屏活动不是更好吗?您可以创建一个片段类来处理exoplayer,然后只需在全屏布局和recyclerview上对其进行充气。
用户单击recyclerview时,您只需要在视频的实际位置上设置额外内容,别忘了在清单文件的全屏活动标签中设置全屏样式。