当全屏视频观看时,将重新创建RecyclerView

时间:2019-01-23 16:51:01

标签: android android-recyclerview fullscreen android-videoview android-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();
  }
});
}

YouTuDraggingView类中的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>

当我单击视频视图上的全屏按钮时..仅将视频视图显示在全屏上..而不是recyclerview ...

我认为问题可能出在 mAdapter = new Adapter(this, movieList, this); movieList进入全屏模式时,会在其中创建另一个videoview

任何对此的帮助将对我有很大帮助。 预先感谢。.

3 个答案:

答案 0 :(得分:0)

请分享mangal_stuti_activity.xml代码。

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

答案 1 :(得分:0)

使视频全屏显示时,您似乎要求更改方向。当活动方向改变时,活动将被销毁并重新创建。这是Activity Life Cycle的一部分。发生这种情况时,您需要对活动和RecyclerView做save and restore your state

这是一个有关如何执行此操作的示例:

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

答案 2 :(得分:0)

我刚刚将此行添加到了在AndroidManifest.xml文件中播放视频的活动中

android:configChanges="orientation|screenSize" 

这阻止了重新创建活动。

感谢vignesh-ramachandrahere中提供了答案