【问题标题】:How to Pass the Current ID as the position in recycler View?如何将当前 ID 作为 recycler View 中的位置传递?
【发布时间】: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


【解决方案1】:

你可以像处理 onbindViewHolder() 上的点击事件

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

【讨论】:

  • Tnx 很棒的答案
猜你喜欢
  • 1970-01-01
  • 2015-10-18
  • 2020-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-02
  • 1970-01-01
  • 2013-09-21
相关资源
最近更新 更多