【发布时间】:2018-09-12 19:39:22
【问题描述】:
我想在 listView 中显示一个 youtube 视频列表。 我使用的是片段而不是活动。 这是片段:
public class VideosFragment extends Fragment{
...
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_videos, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState){
//assign video
mVideosListView = getView().findViewById(R.id.videoLV);
fetchVideos();
/***populate video list to adapter**/
mVideoAdapter = new VideoAdapter(getContext(), mVideosList, this);
mVideosListView.setAdapter(mVideoAdapter);
}
}
fragment_videos 布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorBackground"
android:orientation="vertical"
tools:context=".VideosFragment">
<ListView
android:id="@+id/videoLV"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
在我的适配器中,我试图动态插入多个 YouTubePlayerSupportFragment。
视频适配器:
public class VideoAdapter extends ArrayAdapter<Video> {
private Context mContext;
private Fragment frgmt;
private List<Video> mVideos;
public VideoAdapter(@NonNull Context context, @NonNull List<Video> objects, Fragment frg) {
super(context, R.layout.single_row_video, objects);
mContext = context;
mVideos = objects;
frgmt = frg;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
YouTubePlayerSupportFragment videoFragment = new YouTubePlayerSupportFragment();
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.single_row_video, null);
holder = new ViewHolder();
holder.videoView = convertView.findViewById(R.id.videoView);
FragmentTransaction transaction = frgmt.getChildFragmentManager().beginTransaction();
transaction.add(R.id.videoView, videoFragment).commit();
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}try {
Video video = mVideos.get(position);
final String url = video.getVideoUrl(); // your URL here
videoFragment.initialize("AIza.....13Ao", new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
if (!b) {
youTubePlayer.cueVideo(url);
}
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult result) {
if (result.isUserRecoverableError()) {
result.getErrorDialog(frgmt.getActivity(), 1).show();
} else {
Toast.makeText(frgmt.getActivity(), "YouTubePlayer.onInitializationFailure(): " + result.toString(), Toast.LENGTH_LONG).show();
}
}
});
} catch (Exception e) {
Log.e("Youtube Video error", e.getMessage());
}
return convertView;
}
public static class ViewHolder {
FrameLayout videoView;
}
}
这是 single_row_video 布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<FrameLayout
android:layout_gravity="center"
android:layout_margin="2dp"
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_height="200dp" />
</android.support.v7.widget.CardView>
问题是我从列表中显示了一个视频,其他片段看起来是空的,我不知道这是否是实现我目标的干净方法。 有什么更好的主意或改变吗?谢谢。
【问题讨论】:
-
您可以在这里找到一个示例:github.com/PierfrancescoSoffritti/android-youtube-player/tree/… 这是使用不同的 YouTube 播放器,但概念是相同的。
标签: android listview android-fragments android-youtube-api