【发布时间】:2019-12-07 21:15:46
【问题描述】:
我正在使用 Youtube API 在 recyclerView 中播放视频
但我每次都必须对 videoID 进行硬编码,我还创建了一个 videoID 列表,如何传递各自的 videoID 而不是硬编码。
这是我的 VideoActivity.java :
RecyclerView recyclerView;
List<com.devapps.masjid.CustomModel> myModelList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
myModelList = new ArrayList<>();
myModelList.add(new com.devapps.masjid.CustomModel("hMy5za-m5Ew"));
myModelList.add(new com.devapps.masjid.CustomModel("unU9vpLjHRk"));
myModelList.add(new com.devapps.masjid.CustomModel("k9zTr2MAFRg"));
recyclerView.setAdapter(new CustomAdapter(this,myModelList));
这是 MyAdapter 类:
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
Context context;
List<com.devapps.masjid.CustomModel> myModelList;
public CustomAdapter(Context context, List<com.devapps.masjid.CustomModel> myModelList) {
this.context = context;
this.myModelList = myModelList;
}
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.custom_layout, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, final int position) {
final com.devapps.masjid.CustomModel myModel = myModelList.get(position);
final YouTubeThumbnailLoader.OnThumbnailLoadedListener onThumbnailLoadedListener = new YouTubeThumbnailLoader.OnThumbnailLoadedListener() {
@Override
public void onThumbnailError(YouTubeThumbnailView youTubeThumbnailView, YouTubeThumbnailLoader.ErrorReason errorReason) {
Toast.makeText(context, "Some Error Occured !", Toast.LENGTH_SHORT).show();
Log.e("Error : " , String.valueOf(errorReason));
}
@Override
public void onThumbnailLoaded(YouTubeThumbnailView youTubeThumbnailView, String s) {
ProgressDialog progressDialog = new ProgressDialog(context);
progressDialog.show();
youTubeThumbnailView.setVisibility(View.VISIBLE);
holder.relativeLayoutOverYouTubeThumbnailView.setVisibility(View.VISIBLE);
progressDialog.hide();
}
};
holder.youTubeThumbnailView.initialize(YoutubeConfig.getApiKey(), new YouTubeThumbnailView.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubeThumbnailView youTubeThumbnailView, YouTubeThumbnailLoader youTubeThumbnailLoader) {
youTubeThumbnailLoader.setVideo(myModel.getId());
youTubeThumbnailLoader.setOnThumbnailLoadedListener(onThumbnailLoadedListener);
}
@Override
public void onInitializationFailure(YouTubeThumbnailView youTubeThumbnailView, YouTubeInitializationResult youTubeInitializationResult) {
Toast.makeText(context, "Some Error Occured OnIntitialisationFailure!", Toast.LENGTH_SHORT).show();
Toast.makeText(context, "Details : " + youTubeInitializationResult, Toast.LENGTH_LONG).show();
}
});
}
@Override
public int getItemCount() {
return myModelList.size();
}
class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
protected RelativeLayout relativeLayoutOverYouTubeThumbnailView;
YouTubeThumbnailView youTubeThumbnailView;
protected ImageView playButton;
public ViewHolder(@NonNull View itemView) {
super(itemView);
playButton = (ImageView) itemView.findViewById(R.id.btnYoutube_player);
playButton.setOnClickListener(this);
relativeLayoutOverYouTubeThumbnailView = (RelativeLayout) itemView.findViewById(R.id.relativeLayout_over_youtube_thumbnail);
youTubeThumbnailView = (YouTubeThumbnailView) itemView.findViewById(R.id.youtube_thumbnail);
}
@Override
public void onClick(View view) {
Intent intent = YouTubeStandalonePlayer.createVideoIntent((Activity) context, YoutubeConfig.getApiKey(), "unU9vpLjHRk", 100,
true,
true);
context.startActivity(intent);
}
}
}
这是我的模型类:
public class CustomModel {
String id;
public CustomModel(){
}
public CustomModel(String id) {
this.id = id;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
如何从列表中传递当前 VideoId,而不是硬编码单个值,如您在 Adapter Class 中的 OnClick() 中看到的那样?
【问题讨论】:
-
在这个way上实现OnClickListener
-
但是为什么我不能这样做,我的意思是我做错了什么?
-
String id = myModelList.get(getAdapterPosition()).getId();.
标签: android android-youtube-api