当 videoview 全屏时 RecyclerView 被重新创建

RecyclerView gets Recreated when the videoview is in fullscreen

我有一个 recyclerview,它的 onClick 显示一个带有全屏按钮的 videoview..

然而,当我单击 videoview 上的全屏按钮时。我的 RecyclerView 再次显示(或创建)..

我只想在全屏模式下显示 videoview 中的视频..

以下是我的 Acticity OnCreate() 方法:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mangal_stuti_activity);
toolbar = findViewById(R.id.toolbar);
toolbar.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
setSupportActionBar(toolbar);
recyclerView = (findViewById(R.id.recycler_view));
mAdapter = new Adapter(this, movieList, this);

RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.addItemDecoration(new MyDividerItemDecoration(this, LinearLayoutManager.VERTICAL,16));
recyclerView.setAdapter(mAdapter);
recyclerView.setBackgroundColor(getResources().getColor(R.color.recyclerViewBackground));
recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(), this.recyclerView, new RecyclerTouchListener.ClickListener()
{
  public void onClick(View view, int position) {
    toolbar.setVisibility(View.GONE);
      playVideo();

  }

  public void onLongClick(View paramAnonymousView, int paramAnonymousInt) {}
}));

prepareMovieData();  // possibly this function is creating the recyclerview again when the fullScreen is called..

mVideoView = findViewById(R.id.videoView);
mYouTuDraggingView = findViewById(R.id.youtube_view);
mYouTuDraggingView.setCallback(this);

controlPanel = new YoutubeControlPanel(this);
controlPanel.setYouTuDraggingView(mYouTuDraggingView);
mVideoView.setControlPanel(controlPanel);
controlPanel.getFullScreenIv().setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
    mYouTuDraggingView.fullScreenChange();
  }
});
controlPanel.getDownIv().setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
    toolbar.setVisibility(View.VISIBLE);
    mYouTuDraggingView.fullScreenGoMin();
  }
});
}

YouTuDraggingViewclass中的fullScreenChange()如下:

void fullScreenChange() {
    if (isLandscape()) {
        isLandscapeToPortrait = true;
        mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
    } else {
        isPortraitToLandscape = true;
        mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
    }
}

mangal_stuti_activity.xml

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
tools:context="com.amitabh.dhamma_jaagran.MangalStuti"
android:orientation="vertical"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="@color/recyclerViewBackground"
android:id="@+id/activity_main"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
<include layout="@layout/toolbar_stuti_activities"/>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </android.support.v7.widget.RecyclerView>

    <com.amitabh.YouTuDraggingView
        android:id="@+id/youtube_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="bottom"
        android:orientation="vertical"
        android:visibility="invisible"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:id="@+id/footer_text"
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom"
        android:background="@color/colorPrimaryDark"
        android:text="@string/copyright_footer"
        android:textAlignment="center"
        android:textColor="@color/white" />


</RelativeLayout>
</LinearLayout>

当我单击 videoview 上的全屏按钮时.. videoview 应该只显示在全屏上..而不是 recyclerview...

我觉得问题可能出在 mAdapter = new Adapter(this, movieList, this);videoview 进入全屏时,另一个 movieList 被创建..

关于这方面的任何帮助都会对我有很大帮助.. 提前致谢..

能否分享 mangal_stuti_activity.xml 代码。

确保 youtube_view 宽度和高度设置为 match_parent。

您似乎要求在全屏显示视频时更改方向。当 activity 方向改变时,activity 被销毁并重新创建。这是 activity 和 RecyclerView 的 Activity Life Cycle. What you're going to have to do save and restore your state 的一部分。

这是一个关于如何做到这一点的例子:

https://guides.codepath.com/android/Handling-Configuration-Changes#recyclerview

我刚刚将此行添加到我的 activity 中,其中正在 AndroidManifest.xml 文件中播放视频

android:configChanges="orientation|screenSize" 

这阻止了 activity 的重新创建。

感谢vignesh-ramachandra for his answer which is provided in