Skip to content

Commit

Permalink
增加了fragment下的播放demo (2017-04-18)
Browse files Browse the repository at this point in the history
  • Loading branch information
CarGuo committed Apr 18, 2017
1 parent b7f8683 commit 5a23283
Show file tree
Hide file tree
Showing 11 changed files with 231 additions and 4 deletions.
1 change: 0 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,5 @@ dependencies {
compile viewDependencies.ndkbitmap_x86
compile androidDependencies.support_v4
//debugCompile dataDependencies.leakcanary

compile project(':gsyVideoPlayer')
}
5 changes: 5 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@
android:name=".DanmkuVideoActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:screenOrientation="portrait" />
<activity
android:name=".FragmentVideoActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:screenOrientation="portrait" />

</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.example.gsyvideoplayer;

import android.os.Build;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.transition.Explode;
import android.view.Window;

import com.example.gsyvideoplayer.fragment.VideoFragment;

public class FragmentVideoActivity extends AppCompatActivity {
VideoFragment newFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
// 设置一个exit transition
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
getWindow().setEnterTransition(new Explode());
getWindow().setExitTransition(new Explode());
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);

newFragment = new VideoFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frameLayout, newFragment);
transaction.addToBackStack(null);
transaction.commit();
}

@Override
public void onBackPressed() {
if (newFragment.onBackPressed()) {
return;
}
finish();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ protected void onCreate(Bundle savedInstanceState) {
ButterKnife.bind(this);
}

@OnClick({R.id.open_btn, R.id.list_btn, R.id.list_btn_2, R.id.list_detail, R.id.clear_cache, R.id.recycler, R.id.recycler_2, R.id.list_detail_list, R.id.web_detail, R.id.danmaku_video})
@OnClick({R.id.open_btn, R.id.list_btn, R.id.list_btn_2, R.id.list_detail, R.id.clear_cache, R.id.recycler, R.id.recycler_2, R.id.list_detail_list, R.id.web_detail, R.id.danmaku_video, R.id.fragment_video})
public void onClick(View view) {
switch (view.getId()) {
case R.id.open_btn:
Expand Down Expand Up @@ -65,6 +65,9 @@ public void onClick(View view) {
case R.id.danmaku_video:
//播放一个弹幕视频
JumpUtils.gotoDanmaku(this);
case R.id.fragment_video:
//播放一个弹幕视频
JumpUtils.gotoFragment(this);
case R.id.clear_cache:
//清理缓存
GSYVideoManager.clearAllDefaultCache(MainActivity.this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import com.example.gsyvideoplayer.adapter.RecyclerBaseAdapter;
import com.example.gsyvideoplayer.adapter.RecyclerNormalAdapter;

import com.example.gsyvideoplayer.holder.RecyclerItemNormalHolder;
import com.example.gsyvideoplayer.model.VideoModel;
import com.shuyu.gsyvideoplayer.GSYVideoManager;
import com.shuyu.gsyvideoplayer.GSYVideoPlayer;
Expand Down Expand Up @@ -74,7 +75,7 @@ public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
//当前播放的位置
int position = GSYVideoManager.instance().getPlayPosition();
//对应的播放列表TAG
if (GSYVideoManager.instance().getPlayTag().equals(ListNormalAdapter.TAG)
if (GSYVideoManager.instance().getPlayTag().equals(RecyclerItemNormalHolder.TAG)
&& (position < firstVisibleItem || position > lastVisibleItem)) {

//如果滑出去了上面和下面就是否,和今日头条一样
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package com.example.gsyvideoplayer.fragment;


import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.transition.Explode;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import com.example.gsyvideoplayer.R;
import com.example.gsyvideoplayer.adapter.ListNormalAdapter;
import com.example.gsyvideoplayer.adapter.RecyclerBaseAdapter;
import com.example.gsyvideoplayer.adapter.RecyclerNormalAdapter;
import com.example.gsyvideoplayer.holder.RecyclerItemNormalHolder;
import com.example.gsyvideoplayer.model.VideoModel;
import com.shuyu.gsyvideoplayer.GSYVideoManager;
import com.shuyu.gsyvideoplayer.GSYVideoPlayer;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class VideoFragment extends Fragment {


@BindView(R.id.list_item_recycler)
RecyclerView videoList;

LinearLayoutManager linearLayoutManager;

RecyclerBaseAdapter recyclerBaseAdapter;

List<VideoModel> dataList = new ArrayList<>();

public VideoFragment() {
// Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_video, container, false);

ButterKnife.bind(this, view);

resolveData();

final RecyclerNormalAdapter recyclerNormalAdapter = new RecyclerNormalAdapter(getActivity(), dataList);
linearLayoutManager = new LinearLayoutManager(getActivity());
videoList.setLayoutManager(linearLayoutManager);
videoList.setAdapter(recyclerNormalAdapter);

videoList.addOnScrollListener(new RecyclerView.OnScrollListener() {

int firstVisibleItem, lastVisibleItem;

@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}

@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
firstVisibleItem = linearLayoutManager.findFirstVisibleItemPosition();
lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
//大于0说明有播放
if (GSYVideoManager.instance().getPlayPosition() >= 0) {
//当前播放的位置
int position = GSYVideoManager.instance().getPlayPosition();
//对应的播放列表TAG
if (GSYVideoManager.instance().getPlayTag().equals(RecyclerItemNormalHolder.TAG)
&& (position < firstVisibleItem || position > lastVisibleItem)) {

//如果滑出去了上面和下面就是否,和今日头条一样
GSYVideoPlayer.releaseAllVideos();
recyclerNormalAdapter.notifyDataSetChanged();
}
}
}
});

return view;
}

public boolean onBackPressed() {
if (StandardGSYVideoPlayer.backFromWindowFull(getActivity())) {
return true;
}
return false;
}

@Override
public void onPause() {
super.onPause();
GSYVideoManager.onPause();
}

@Override
public void onResume() {
super.onResume();
GSYVideoManager.onResume();
}

@Override
public void onDestroy() {
super.onDestroy();
GSYVideoPlayer.releaseAllVideos();
}


private void resolveData() {
for (int i = 0; i < 19; i++) {
VideoModel videoModel = new VideoModel();
dataList.add(videoModel);
}
if (recyclerBaseAdapter != null)
recyclerBaseAdapter.notifyDataSetChanged();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
import com.example.gsyvideoplayer.DanmkuVideoActivity;
import com.example.gsyvideoplayer.DetailListPlayer;
import com.example.gsyvideoplayer.DetailPlayer;
import com.example.gsyvideoplayer.FragmentVideoActivity;
import com.example.gsyvideoplayer.ListVideo2Activity;
import com.example.gsyvideoplayer.ListVideoActivity;
import com.example.gsyvideoplayer.PlayActivity;
import com.example.gsyvideoplayer.R;
import com.example.gsyvideoplayer.RecyclerView2Activity;
import com.example.gsyvideoplayer.RecyclerViewActivity;
import com.example.gsyvideoplayer.WebDetailActivity;
import com.example.gsyvideoplayer.adapter.RecyclerNormalAdapter;

/**
* Created by shuyu on 2016/11/11.
Expand Down Expand Up @@ -129,4 +129,14 @@ public static void gotoDanmaku(Activity activity) {
Intent intent = new Intent(activity, DanmkuVideoActivity.class);
activity.startActivity(intent);
}

/**
* 跳转到弹幕
*
* @param activity
*/
public static void gotoFragment(Activity activity) {
Intent intent = new Intent(activity, FragmentVideoActivity.class);
activity.startActivity(intent);
}
}
12 changes: 12 additions & 0 deletions app/src/main/res/layout/activity_fragment.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/primary_material_dark">

<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</RelativeLayout>
7 changes: 7 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@
android:layout_marginTop="20dp"
android:text="Danmaku Video" />

<Button
android:id="@+id/fragment_video"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Fragment" />

<Button
android:id="@+id/clear_cache"
android:layout_width="120dp"
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/res/layout/fragment_video.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v7.widget.RecyclerView
android:id="@+id/list_item_recycler"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,7 @@
发阿斯蒂芬撒地方我发斯蒂芬是打发阿斯蒂芬撒地方方撒地方芬是打发阿斯蒂芬撒地方我发斯蒂芬是打发阿
芬撒地方方撒地方芬是打发阿斯蒂芬撒地方我发斯蒂芬是打发阿斯蒂芬撒地方方撒地方我发斯蒂芬是打发阿
撒地方芬是打发阿斯蒂芬撒地方我发斯蒂芬是打发阿斯蒂芬撒地方方</string>

<!-- TODO: Remove or change this placeholder text -->
<string name="hello_blank_fragment">Hello blank fragment</string>
</resources>

0 comments on commit 5a23283

Please sign in to comment.