在RecyclerView中不显示Youtube视频

时间:2016-04-07 11:03:40

标签: android youtube android-recyclerview youtube-data-api

我面临的问题是youtube视频没有显示,只有最后一个正在播放,当我滚动视频时也会消失。我想要的是每个视频都应该显示,每个视频都不同。

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);
}
}

2 个答案:

答案 0 :(得分:0)

如果我理解正确,RecyclerView中的项目未正确/完整显示?一些屏幕截图和你的xml文件会有所帮助。

如果问题确实存在,请检查playerview_demo.xml layout_height是否设为wrap_content

如果未设置为wrap_content,则问题就在于此。如果没有,请告诉我们。

答案 1 :(得分:0)

由于YoutubePlayer视图太大而无法添加到recyclerview,因此无法执行此操作。请改用YouTubeThumbnailView来显示缩略图。当用户点击其中一个时,您可以使用YouTubePlayerFragment视图开始YouTubeplayerView或活动。

在您的情况下,这将是这样的

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);
}
}

修改 感谢Irfan Raza指出这一点。请参阅this回答,好像可以完成