Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在选项卡式活动片段中制作视频的recyclerview(使用Youtube API)?_Android_Android Recyclerview_Fragment_Adapter_Youtube Data Api - Fatal编程技术网

Android 如何在选项卡式活动片段中制作视频的recyclerview(使用Youtube API)?

Android 如何在选项卡式活动片段中制作视频的recyclerview(使用Youtube API)?,android,android-recyclerview,fragment,adapter,youtube-data-api,Android,Android Recyclerview,Fragment,Adapter,Youtube Data Api,video_template.xml: <?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:orientation="ver

video_template.xml:

<?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:orientation="vertical">
    <FrameLayout
        android:name="com.google.android.youtube.player.YouTubePlayerSupportFragment"
        android:id="@+id/youtubePlayerViewer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="10dp"
        android:background="@color/colorAccent"/>

</LinearLayout>

我在网上搜索时看到的所有示例都是在activity中制作的,而不是在recyclerview的fragment中制作的。

而是制作一个Thumbnail的recycler视图。您能告诉我们到目前为止您一直在尝试做什么吗?请阅读。@Dima Kizhevin添加了我的代码和我正在尝试做的图片do@2Dee好的。非常感谢。
    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.zeina.car.Cars">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/List_of_videos_and_images"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="5dp"
        />

</LinearLayout>
    import android.support.annotation.Nullable;
    import android.support.v4.app.Fragment;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import java.util.List;


    public class Cars extends Fragment {


     public boolean FragmentAdded =false;
     public List<Fragment> fragments;
     public VidOrImage a = new VidOrImage();
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


            filldata();
            return inflater.inflate(R.layout.cars_fragment, container, false);

        }

        @Override
        public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {



             isFragmentAdded();
RecyclerViewposts=getView().findViewById(R.id.List_of_videos_and_images);
            LinearLayoutManager llm = new LinearLayoutManager(getActivity());
            llm.setOrientation(LinearLayoutManager.VERTICAL);
            posts.setLayoutManager(llm);
            SliderAdapter adapter = new SliderAdapter(getActivity(),a.data);
            posts.setAdapter(adapter);
        }

        void filldata(){
            a.add("https://www.youtube.com/watch?v=EoojNzIL2cQ");
            a.add("https://www.youtube.com/watch?v=6Viyt2aIOG8");
        }

        boolean isFragmentAdded(){
            if (!isAdded()) {
                FragmentAdded=false;
            return false;
            }
            else{
                FragmentAdded=true;
                fragments = getChildFragmentManager().getFragments();
                return true;
            }
        }

    }
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayerSupportFragment;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;


import static android.content.ContentValues.TAG;

public class SliderAdapter extends  RecyclerView.Adapter<SliderAdapter.ViewHolder>{

    Context context;
    ArrayList<String> myDataSet;
    int flag;
    String videoId;

    public SliderAdapter (Context context, ArrayList<String> data)
    {
        this.context = context;
        myDataSet=data;
    }

    @Override
    public int getItemViewType(int position) {
        if(myDataSet.get(position).toString().contains("http")==true) {
            flag=1;
            return 1;
        }
        else {
            flag=0;
            return 0;
        }
    }

    @Override
    public SliderAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //flag 1 for video , 0 for image
        View view = null;

        if(viewType==1)
            view =LayoutInflater.from(parent.getContext()).inflate(R.layout.video_template, parent, false); 
        else
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.image_template, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {



        if(flag==1) {

            videoId = myDataSet.get(position).toString().replace("https://www.youtube.com/watch?v=","");
            Log.i(TAG, "onBindViewHolder: "+videoId.toString());
            playVideo(videoId,holder.youTubePlayerFragment);
            Log.i(TAG, "onBindViewHolder: entered if flag ==1");
        }

        if (flag==0){
            Picasso.get().load(myDataSet.get(position).toString()).placeholder(R.drawable.car2).into(holder.imageView);
        }
    }
    public void playVideo(final String videoId, YouTubePlayerSupportFragment youTubePlayerView) {
        //initialize youtube player view
        Log.i(TAG, "playVideo: before initialization");
        youTubePlayerView.initialize("AIzaSyAsz0MzCEsVkmfsgZC6yh-qQZ_KC77JC08",
                new YouTubePlayer.OnInitializedListener() {
                    @Override
                    public void onInitializationSuccess(YouTubePlayer.Provider provider,
                                                        YouTubePlayer youTubePlayer, boolean b) {
                        youTubePlayer.cueVideo(videoId);
                    }

                    @Override
                    public void onInitializationFailure(YouTubePlayer.Provider provider,
                                                        YouTubeInitializationResult youTubeInitializationResult) {
                        Log.i(TAG, "onInitializationFailure: error ");
                    }
                });
    }


    @Override
    public int getItemCount() {
        return myDataSet.size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder {
       // public VideoView videoView;
        public ImageView imageView;
        public YouTubePlayerSupportFragment youTubePlayerFragment = YouTubePlayerSupportFragment.newInstance();
        Cars b =new Cars();


        public ViewHolder(View itemView) {
            super(itemView);
            imageView= itemView.findViewById(R.id.imageViewer);
            Log.i(TAG, "ViewHolder: before youtube viewer");
            if( b.FragmentAdded==true){
                FragmentTransaction transaction =b.getChildFragmentManager().beginTransaction();
                transaction.add(R.id.youtubePlayerViewer, youTubePlayerFragment).commit();
                Log.i(TAG, "ViewHolder: transaction added");
            }
            Log.i(TAG, "ViewHolder: after youtube viewer");
        }
    }
}
 if( b.FragmentAdded==true){
                    FragmentTransaction transaction =b.getChildFragmentManager().beginTransaction();
                    transaction.add(R.id.youtubePlayerViewer, youTubePlayerFragment).commit();