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

Posted

技术标签:

【中文标题】Recyclerview 中的对话框 - IllegalStateException:指定的孩子已经有一个父母【英文标题】:Dialog in Recyclerview - IllegalStateException: The specified child already has a parent 【发布时间】:2019-04-22 17:56:19 【问题描述】:

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

我不想从 recyclerview 中删除视图,因为当我添加回相同的视图时,它与 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】:

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

【讨论】:

是的,但我们的要求是对话框,每当我尝试打开全屏对话框时,我就会遇到异常 您是否已经尝试在 initFullscreenDialog 方法之前移除视图? 如果我删除视图然后返回到 recyclerview 它会弄乱 recycler 视图布局

以上是关于Recyclerview 中的对话框 - IllegalStateException:指定的孩子已经有一个父母的主要内容,如果未能解决你的问题,请参考以下文章

当RecyclerView位于对话框上时,不显示分隔符

如何在 RecyclerView 适配器中的 ClickListener 上显示 AlertDialog

将 RecyclerView(RecyclerFragment) 添加到对话框

如何在 TabLayout 中找到由 RecyclerView 打开的对话框的上下文?

在 recyclerview 项目上单击 show recyclerview data custom in toDialog Box

recyclerview实战——对话界面