扩展YouTubeBaseActivity

时间:2018-09-17 17:11:13

标签: android android-recyclerview recycler-adapter

在我的应用程序中,需要在AlertDialog的扩展类RecyclerView中显示YouTube视频,但是我需要扩展或实现YouTubeBaseActivity类有什么办法吗?

我在这里留下我的代码:

/**
 * Adapter to show the most viewed in the start section
 */
public class AdapterStart extends RecyclerView.Adapter<AdapterStart.ViewHolder> implements YouTubePlayer.OnInitializedListener {

    Context context;
    final String claveYouTube = "" + R.string.clave_youtube;

    public static class ViewHolder extends RecyclerView.ViewHolder {
        //Respectives fields of items
        public TextView name;
        public TextView year;
        public ImageView imagen;
        LinearLayout parentLayout;

        public ViewHolder(View v) {
            super(v);
            name = v.findViewById(R.id.name);
            year = v.findViewById(R.id.year);
            imagen = v.findViewById(R.id.miniature_show);
            parentLayout = v.findViewById(R.id.parentLayout_Start);
        }
    }

    public AdapterStart(Context context) {
        this.context = context;
    }

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

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext())
                .inflate(R.layout.item_list_start, viewGroup, false);
        return new ViewHolder(v);
    }


    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int i) {
        show_model item = show_model.RECOMENDED.get(i);

        Glide.with(viewHolder.itemView.getContext())
                .load(item.getIdDrawable())
                .centerCrop()
                .into(viewHolder.imagen);
        viewHolder.name.setText(item.getName());
        viewHolder.year.setText("Año: " + item.getYear());

        final String tittleInfo = item.getName();
        final int yearInfo = item.getYear();
        final int imageInfo = item.getIdDrawable();
        final int info = item.getInfo();
        final int trailer = item.getTrailer();

        viewHolder.imagen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                infoDialog(tittleInfo, yearInfo, imageInfo, info, trailer).show();
            }
        });
    }

    public AlertDialog infoDialog(String name, int year, int idDrawable, int info, int trailer) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        LayoutInflater inflater = ((AppCompatActivity)context).getLayoutInflater();
        View v = inflater.inflate(R.layout.item_selected_info, null);
        builder.setView(v);

        TextView textInfo = v.findViewById(R.id.text_info);
        TextView tittleInfo = v.findViewById(R.id.name_info);
        TextView yearInfo = v.findViewById(R.id.year_info);
        YouTubePlayerView youTubePlayerView = v.findViewById(R.id.video_info);

        youTubePlayerView.initialize(claveYouTube, this);

        tittleInfo.setText(name);
        yearInfo.setText(String.valueOf(year));

        textInfo.setText(info);

        return builder.create();
    }

    @Override
    public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean wasRestored) {

        if (!wasRestored){
            youTubePlayer.cueVideo("qU1-h-KdYdM");

        }

    }

    @Override
    public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {

        if (youTubeInitializationResult.isUserRecoverableError()){
            youTubeInitializationResult.getErrorDialog((AppCompatActivity)context, 1).show();
        } else {
            String error = "Error al inicializar YouTube" + youTubeInitializationResult.toString();
            Toast.makeText(context, error, Toast.LENGTH_SHORT).show();
        }

    }

    //This does not work because I have not extended YouTubeBaseActivity
    protected void onActivityResult (int requestCode, int resultCode, Intent data){

        if (resultCode == 1){
            getYoutubePlayerProvider().initialize(claveYouTube, this);
        }
    }

    protected YouTubePlayer.Provider getYoutubePlayerProvider(){
        return getYoutubePlayerProvider();
    }
}

0 个答案:

没有答案