Youtube Video is not Displaying in RecyclerView

非 Y 不嫁゛ 提交于 2019-12-23 08:44:02

问题


I am facing issue where youtube video is not displaying only last one is getting played and that too when I scroll the video disappears. What i wanted is every video should be displayed and every video is different.

public class MyRecyclerViewAdapter extends Adapter<MyRecyclerViewAdapter
.DataObjectHolder> {
private static String LOG_TAG = "MyRecyclerViewAdapter";
private ArrayList<DataObject> mDataset;
private static MyClickListener myClickListener;
private static final int RECOVERY_DIALOG_REQUEST = 10;
public  static  String key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
Context ctx;
View view;

public static class DataObjectHolder extends ViewHolder
    implements View
    .OnClickListener {
    YouTubePlayerView youTubeView;
public DataObjectHolder(View itemView) {
    super(itemView);
    youTubeView = (YouTubePlayerView) itemView.findViewById(R.id.youtube_view);
    Log.i(LOG_TAG, "Adding Listener");
    itemView.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    myClickListener.onItemClick(getAdapterPosition(), v);
}
}

public void setOnItemClickListener(MyClickListener myClickListener) {
this.myClickListener = myClickListener;
}

public MyRecyclerViewAdapter(Context context,ArrayList<DataObject> myDataset) {
    mDataset = myDataset;
    ctx=context;
}

@Override
public DataObjectHolder onCreateViewHolder(ViewGroup parent,
                                       int viewType) {
    view = LayoutInflater.from(parent.getContext())
        .inflate(R.layout.playerview_demo, parent, false);

DataObjectHolder dataObjectHolder = new DataObjectHolder(view);
return dataObjectHolder;
}



@Override
public void onBindViewHolder(DataObjectHolder holder, final int position) {
holder.youTubeView.initialize(key, new YouTubePlayer.OnInitializedListener() {
    @Override
    public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean b) {
        player.cueVideo(mDataset.get(position).getmText1());
    }

    @Override
    public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult errorReason) {
        if (errorReason.isUserRecoverableError()) {
            errorReason.getErrorDialog((Activity) ctx, RECOVERY_DIALOG_REQUEST).show();
        } else {
            String errorMessage = String.format("YouTube Error (%1$s)",
                    errorReason.toString());
            Toast.makeText(ctx, errorMessage, Toast.LENGTH_LONG).show();
        }
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == RECOVERY_DIALOG_REQUEST) {
            // Retry initialization if user performed a recovery action
            getYouTubePlayerProvider().initialize(key, this);
        }
    }

    protected YouTubePlayer.Provider getYouTubePlayerProvider() {
        Toast.makeText(ctx, view.toString(), Toast.LENGTH_LONG).show();
        return (YouTubePlayerView) view.findViewById(R.id.youtube_view);
    }


});
}

public void addItem(DataObject dataObj, int index) {
mDataset.add(index, dataObj);
notifyItemInserted(index);
}

public void deleteItem(int index) {
mDataset.remove(index);
notifyItemRemoved(index);
}

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

public interface MyClickListener {
public void onItemClick(int position, View v);
}
}

回答1:


If I understand right, the items in your RecyclerView aren't displayed properly/fully? Some screenshots and your xml file would be helpfull.

If this is indeed the problem, check your playerview_demo.xml if it's layout_height is set to wrap_content

If it was not set to wrap_content this was the issue. If not, let us know.




回答2:


You cannot do that as the YoutubePlayer view is too large to be added to a recyclerview. Use a YouTubeThumbnailView instead to display the thumbnails. When the user clicks on one of them, you can start a YouTubePlayerFragment or an activity with a YouTubeplayerView view.

In your case this would be like this

public class MyRecyclerViewAdapter extends Adapter<MyRecyclerViewAdapter
.DataObjectHolder> {
private static String LOG_TAG = "MyRecyclerViewAdapter";
private ArrayList<DataObject> mDataset;
private static MyClickListener myClickListener;
private static final int RECOVERY_DIALOG_REQUEST = 10;
public  static  String key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
Context ctx;
View view;

public static class DataObjectHolder extends ViewHolder
    implements View
    .OnClickListener {
    YouTubeThumbnailView youTubeThumbnailView ;
public DataObjectHolder(View itemView) {
    super(itemView);
    youTubeThumbnailView = (YouTubeThumbnailView ) itemView.findViewById(R.id.yourId);
    itemView.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    myClickListener.onItemClick(getAdapterPosition(), v);
}
}

public void setOnItemClickListener(MyClickListener myClickListener) {
this.myClickListener = myClickListener;
}

public MyRecyclerViewAdapter(Context context,ArrayList<DataObject> myDataset) {
    mDataset = myDataset;
    ctx=context;
}

@Override
public DataObjectHolder onCreateViewHolder(ViewGroup parent,
                                       int viewType) {
    view = LayoutInflater.from(parent.getContext())
        .inflate(R.layout.playerview_demo, parent, false);

DataObjectHolder dataObjectHolder = new DataObjectHolder(view);
return dataObjectHolder;
}



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

holder.youTubeThumbnailView.initialize(key, new YouTubePlayer.OnInitializedListener() {
    @Override
    public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean b) {
    }

    @Override
    public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult errorReason) {
        if (errorReason.isUserRecoverableError()) {
            errorReason.getErrorDialog((Activity) ctx, RECOVERY_DIALOG_REQUEST).show();
        } else {
            String errorMessage = String.format("YouTube Error (%1$s)",
                    errorReason.toString());
            Toast.makeText(ctx, errorMessage, Toast.LENGTH_LONG).show();
        }
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == RECOVERY_DIALOG_REQUEST) {
            // Retry initialization if user performed a recovery action
            getYouTubePlayerProvider().initialize(key, this);
        }
    }

    protected YouTubePlayer.Provider getYouTubePlayerProvider() {
        Toast.makeText(ctx, view.toString(), Toast.LENGTH_LONG).show();
        return (YouTubePlayerView) view.findViewById(R.id.youtube_view);
    }


});
}

public void addItem(DataObject dataObj, int index) {
mDataset.add(index, dataObj);
notifyItemInserted(index);
}

public void deleteItem(int index) {
mDataset.remove(index);
notifyItemRemoved(index);
}

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

public interface MyClickListener {
public void onItemClick(int position, View v);
}
}

Edit Thanks to Irfan Raza for pointing this out. See this answer, it seems as if it can done



来源:https://stackoverflow.com/questions/36474281/youtube-video-is-not-displaying-in-recyclerview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!