Skip to content

Commit

Permalink
修复RecyclerView列表中全屏有可能崩溃的Bug
Browse files Browse the repository at this point in the history
  • Loading branch information
jianjunxiao committed Jul 13, 2017
1 parent 44881de commit e55a124
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 56 deletions.
13 changes: 4 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ allprojects {
}
dependencies {
compile 'com.github.xiaoyanger0825:NiceVieoPlayer:v1.8'
compile 'com.github.xiaoyanger0825:NiceVieoPlayer:v1.9'
}
```
**在对应视频界面所在的Activity的Manifest.xml中需要添加如下配置:**
Expand Down Expand Up @@ -122,15 +122,10 @@ public List<Clarity> getClarites() {
#### 4.在RecyclerView列表中使用
在ReclerView列表中使用时需要监听itemView的detach,以此释放掉对应的播放器
```
mRecyclerView.addOnChildAttachStateChangeListener(new RecyclerView.OnChildAttachStateChangeListener() {
mRecyclerView.setRecyclerListener(new RecyclerView.RecyclerListener() {
@Override
public void onChildViewAttachedToWindow(View view) {
}
@Override
public void onChildViewDetachedFromWindow(View view) {
NiceVideoPlayer niceVideoPlayer = (NiceVideoPlayer) view.findViewById(R.id.nice_video_player);
public void onViewRecycled(RecyclerView.ViewHolder holder) {
NiceVideoPlayer niceVideoPlayer = ((VideoViewHolder) holder).mVideoPlayer;
if (niceVideoPlayer == NiceVideoPlayerManager.instance().getCurrentNiceVideoPlayer()) {
NiceVideoPlayerManager.instance().releaseNiceVideoPlayer();
}
Expand Down
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ dependencies {
testCompile rootProject.ext.dependencies.junit
compile rootProject.ext.dependencies.recyclerview
compile rootProject.ext.dependencies.glide
compile 'com.android.support.constraint:constraint-layout:1.0.2'
}
6 changes: 0 additions & 6 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,6 @@
android:label="Activity中时处理播放时按下Home键"
android:screenOrientation="portrait">
</activity>
<activity
android:name=".example.ProcessHome2Activity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="Fragment中时处理播放时按下Home键"
android:screenOrientation="portrait">
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.xiao.nicevideoplayer.NiceVideoPlayerManager;
import com.xiao.nicevieoplayer.R;
import com.xiao.nicevieoplayer.example.adapter.VideoAdapter;
import com.xiao.nicevieoplayer.example.adapter.holder.VideoViewHolder;
import com.xiao.nicevieoplayer.example.util.DataUtil;

/**
Expand Down Expand Up @@ -40,15 +41,10 @@ private void init() {
mRecyclerView.setHasFixedSize(true);
VideoAdapter adapter = new VideoAdapter(getActivity(), DataUtil.getVideoListData());
mRecyclerView.setAdapter(adapter);
mRecyclerView.addOnChildAttachStateChangeListener(new RecyclerView.OnChildAttachStateChangeListener() {
mRecyclerView.setRecyclerListener(new RecyclerView.RecyclerListener() {
@Override
public void onChildViewAttachedToWindow(View view) {

}

@Override
public void onChildViewDetachedFromWindow(View view) {
NiceVideoPlayer niceVideoPlayer = (NiceVideoPlayer) view.findViewById(R.id.nice_video_player);
public void onViewRecycled(RecyclerView.ViewHolder holder) {
NiceVideoPlayer niceVideoPlayer = ((VideoViewHolder) holder).mVideoPlayer;
if (niceVideoPlayer == NiceVideoPlayerManager.instance().getCurrentNiceVideoPlayer()) {
NiceVideoPlayerManager.instance().releaseNiceVideoPlayer();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.xiao.nicevideoplayer.NiceVideoPlayerManager;
import com.xiao.nicevieoplayer.R;
import com.xiao.nicevieoplayer.example.adapter.VideoAdapter;
import com.xiao.nicevieoplayer.example.adapter.holder.VideoViewHolder;
import com.xiao.nicevieoplayer.example.base.CompatHomeKeyFragment;
import com.xiao.nicevieoplayer.example.util.DataUtil;

Expand Down Expand Up @@ -40,15 +41,10 @@ private void init() {
mRecyclerView.setHasFixedSize(true);
VideoAdapter adapter = new VideoAdapter(getActivity(), DataUtil.getVideoListData());
mRecyclerView.setAdapter(adapter);
mRecyclerView.addOnChildAttachStateChangeListener(new RecyclerView.OnChildAttachStateChangeListener() {
mRecyclerView.setRecyclerListener(new RecyclerView.RecyclerListener() {
@Override
public void onChildViewAttachedToWindow(View view) {

}

@Override
public void onChildViewDetachedFromWindow(View view) {
NiceVideoPlayer niceVideoPlayer = (NiceVideoPlayer) view.findViewById(R.id.nice_video_player);
public void onViewRecycled(RecyclerView.ViewHolder holder) {
NiceVideoPlayer niceVideoPlayer = ((VideoViewHolder) holder).mVideoPlayer;
if (niceVideoPlayer == NiceVideoPlayerManager.instance().getCurrentNiceVideoPlayer()) {
NiceVideoPlayerManager.instance().releaseNiceVideoPlayer();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.xiao.nicevideoplayer.NiceVideoPlayer;
import com.xiao.nicevideoplayer.NiceVideoPlayerManager;
import com.xiao.nicevieoplayer.R;
import com.xiao.nicevieoplayer.example.adapter.VideoAdapter;
import com.xiao.nicevieoplayer.example.adapter.holder.VideoViewHolder;
import com.xiao.nicevieoplayer.example.base.CompatHomeKeyActivity;
import com.xiao.nicevieoplayer.example.util.DataUtil;

Expand All @@ -33,15 +33,10 @@ private void init() {
mRecyclerView.setHasFixedSize(true);
VideoAdapter adapter = new VideoAdapter(this, DataUtil.getVideoListData());
mRecyclerView.setAdapter(adapter);
mRecyclerView.addOnChildAttachStateChangeListener(new RecyclerView.OnChildAttachStateChangeListener() {
mRecyclerView.setRecyclerListener(new RecyclerView.RecyclerListener() {
@Override
public void onChildViewAttachedToWindow(View view) {

}

@Override
public void onChildViewDetachedFromWindow(View view) {
NiceVideoPlayer niceVideoPlayer = (NiceVideoPlayer) view.findViewById(R.id.nice_video_player);
public void onViewRecycled(RecyclerView.ViewHolder holder) {
NiceVideoPlayer niceVideoPlayer = ((VideoViewHolder) holder).mVideoPlayer;
if (niceVideoPlayer == NiceVideoPlayerManager.instance().getCurrentNiceVideoPlayer()) {
NiceVideoPlayerManager.instance().releaseNiceVideoPlayer();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.xiao.nicevideoplayer.NiceVideoPlayer;
import com.xiao.nicevideoplayer.NiceVideoPlayerManager;
import com.xiao.nicevieoplayer.R;
import com.xiao.nicevieoplayer.example.adapter.VideoAdapter;
import com.xiao.nicevieoplayer.example.adapter.holder.VideoViewHolder;
import com.xiao.nicevieoplayer.example.util.DataUtil;

public class RecyclerViewActivity extends AppCompatActivity {
Expand All @@ -29,15 +29,10 @@ private void init() {
mRecyclerView.setHasFixedSize(true);
VideoAdapter adapter = new VideoAdapter(this, DataUtil.getVideoListData());
mRecyclerView.setAdapter(adapter);
mRecyclerView.addOnChildAttachStateChangeListener(new RecyclerView.OnChildAttachStateChangeListener() {
mRecyclerView.setRecyclerListener(new RecyclerView.RecyclerListener() {
@Override
public void onChildViewAttachedToWindow(View view) {

}

@Override
public void onChildViewDetachedFromWindow(View view) {
NiceVideoPlayer niceVideoPlayer = (NiceVideoPlayer) view.findViewById(R.id.nice_video_player);
public void onViewRecycled(RecyclerView.ViewHolder holder) {
NiceVideoPlayer niceVideoPlayer = ((VideoViewHolder) holder).mVideoPlayer;
if (niceVideoPlayer == NiceVideoPlayerManager.instance().getCurrentNiceVideoPlayer()) {
NiceVideoPlayerManager.instance().releaseNiceVideoPlayer();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.bumptech.glide.Glide;
import com.xiao.nicevideoplayer.NiceVideoPlayer;
Expand All @@ -15,16 +16,22 @@

public class VideoViewHolder extends RecyclerView.ViewHolder {

private TxVideoPlayerController mController;
private NiceVideoPlayer mVideoPlayer;
public TxVideoPlayerController mController;
public NiceVideoPlayer mVideoPlayer;

public VideoViewHolder(View itemView) {
super(itemView);
mVideoPlayer = (NiceVideoPlayer) itemView.findViewById(R.id.nice_video_player);
// 将列表中的每个视频设置为默认16:9的比例
ViewGroup.LayoutParams params = mVideoPlayer.getLayoutParams();
params.width = itemView.getResources().getDisplayMetrics().widthPixels; // 宽度为屏幕宽度
params.height = (int) (params.width * 9f / 16f); // 高度为宽度的9/16
mVideoPlayer.setLayoutParams(params);
}

public void setController(TxVideoPlayerController controller) {
mController = controller;
mVideoPlayer.setController(mController);
}

public void bindData(Video video) {
Expand All @@ -35,7 +42,6 @@ public void bindData(Video video) {
.placeholder(R.drawable.img_default)
.crossFade()
.into(mController.imageView());
mVideoPlayer.setController(mController);
mVideoPlayer.setUp(video.getVideoUrl(), null);
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/layout/item_video.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
<com.xiao.nicevideoplayer.NiceVideoPlayer
android:id="@+id/nice_video_player"
android:layout_width="match_parent"
android:layout_height="180dp"/>
android:layout_height="200dp"/>
</LinearLayout>
4 changes: 2 additions & 2 deletions config.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ ext {
buildToolsVersion: "25.0.2",
minSdkVersion : 16,
targetSdkVersion : 25,
versionCode : 18,
versionName : "1.8"
versionCode : 19,
versionName : "1.9"
]

dependVersion = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,10 @@ public void setUp(String url, Map<String, String> headers) {
}

public void setController(NiceVideoPlayerController controller) {
mContainer.removeView(mController);
mController = controller;
mController.reset();
mController.setNiceVideoPlayer(this);
mContainer.removeView(mController);
LayoutParams params = new LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
Expand Down

0 comments on commit e55a124

Please sign in to comment.