如何将当前ID作为回收站视图中的位置传递?

问题描述 投票:0回答:1

我正在使用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类的OnClick()中看到的那样对单个值进行硬编码?

android android-youtube-api
1个回答
0
投票
您可以像处理onbindViewHolder()上的click事件一样>

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); holder.playButton.setOnClickListener(new OnClickLister() { @Override public void onClick(View view) { Intent intent = YouTubeStandalonePlayer.createVideoIntent((Activity) context, //here you can get the api key YoutubeConfig.getApiKey(), mymodel.getId(), 100, true, true); context.startActivity(intent); } }); 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; ImageView playButton; public ViewHolder(@NonNull View itemView) { super(itemView); playButton = (ImageView) itemView.findViewById(R.id.btnYoutube_player); relativeLayoutOverYouTubeThumbnailView = (RelativeLayout) itemView.findViewById(R.id.relativeLayout_over_youtube_thumbnail); youTubeThumbnailView = (YouTubeThumbnailView) itemView.findViewById(R.id.youtube_thumbnail); } } }

© www.soinside.com 2019 - 2024. All rights reserved.