在ListView中显示Android中的YouTube视频列表

时间:2018-09-12 19:39:23

标签: android listview android-fragments android-youtube-api

我想在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);
}
}

片段视频布局

<?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。

VideoAdapter

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>

问题是我正在从列表中显示单个视频,其他片段看起来是空的,我不知道这是否是实现目标的干净方法。 有什么更好的主意或改变吗?谢谢。

1 个答案:

答案 0 :(得分:0)

问题出在这一行:

YouTubePlayerSupportFragment videoFragment = new YouTubePlayerSupportFragment();

每次调用getView时,都会创建此片段的实例,但是一次只能将其添加到一个查看器中(这就是为什么您只有一个视频)。 ViewHolder的概念是-创建视图并重新使用它。但是您不会重复使用它。

您的代码应如下所示:

    @Override
public View getView(final int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;
    YouTubePlayerSupportFragment videoFragment;
    if (convertView == null) {
        convertView = LayoutInflater.from(mContext).inflate(R.layout.single_row_video, null);
        holder = new ViewHolder();
        holder.videoView = convertView.findViewById(R.id.videoView);
        videoFragment = new YouTubePlayerSupportFragment()
        FragmentTransaction transaction = frgmt.getChildFragmentManager().beginTransaction();
        transaction.add(R.id.videoView, videoFragment).commit();
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
        videoFragment = (YouTubePlayerSupportFragment) frgmt.getChildFragmentManager().findFragmentById(R.id.videoView)
    } 
    videoFragment.initialize(...)

更新

this thread来看,似乎YouTube Android Player SDK不允许同时播放多个youtube视频。