适配器位置不正确

时间:2018-11-27 19:12:03

标签: android android-recyclerview youtube-api adapter recycler-adapter

我有一个custom adapter,其中有混合数据,数据列表中有视频和图像URL,并根据数据类型将它们绑定到不同的视图持有者。我正在使用SnapHelper使其像滑块一样。我在水平列表中显示youtube video和图像。当我向左滑动适配器时,适配器将提供正确的视图位置;向右滑动时,适配器将为我提供错误的随机位置,因此我的YouTube播放器在达到其视图类型之前会崩溃。向左打印位置为0 1 2 3 4 5,向右打印位置为6 2 3 1然后崩溃,RecyclerView中有一个视频,其他是图像。向后滑动时应用崩溃。找不到片段YouTubePlayerFragment的ID 0x7f0901b0(com.array151.mypu:id / youtube_holder)的视图,出现此错误。

import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DownloadManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;



import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayerFragment;
import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;
import java.util.List;


public class ContentAdapter extends RecyclerView.Adapter{

    private List<PhotosModel> photosList;
    Activity context1;
    private static final int RECOVERY_REQUEST = 1;
    YouTubePlayer youTubePlayer1;
    private String youtubeAPI = "AIzaSyCceeIhnkIDRvCqzPoxsw8yUPRnvQO9Wto";
    public ContentAdapter(List<PhotosModel> photosList, Activity context) {
        this.context1 = context;
        this.photosList = photosList;
    }
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        switch (viewType) {
            case PhotosModel.VIDEO_TYPE:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.video_item, parent, false);
                return new VideoViewHolder(view);
            case PhotosModel.IMAGE_TYPE:
                view = LayoutInflater.from(parent.getContext()).inflate(R.layout.image_item, parent, false);
                return new ImageViewHolder(view);

        }
        return null;
    }

    @Override
    public int getItemViewType(int position) {
        Log.e("getType",photosList.get(position).getType()+"");
        switch (photosList.get(position).getType()) {
            case 0:
                return PhotosModel.VIDEO_TYPE;
            case 1:
                return PhotosModel.IMAGE_TYPE;
            default:
                return -1;
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int i) {
        holder.setIsRecyclable(false);
        final PhotosModel model = photosList.get(i);
        Log.e("adapter position", i + "model.getType()"+model.getType()+"psoiion"+holder.getAdapterPosition());
        if (model != null) {
            switch (model.getType()) {
                case PhotosModel.VIDEO_TYPE:
                    Log.e("adapter position", i + "inside"+model.getType()+((VideoViewHolder) holder).containerYouTubePlayer.getId());
                    final YouTubePlayerFragment youTubePlayerFragment = YouTubePlayerFragment.newInstance();
                    ((Activity) context1).getFragmentManager().beginTransaction().replace(((VideoViewHolder) holder).containerYouTubePlayer.getId(), youTubePlayerFragment).commit();
                    youTubePlayerFragment.initialize(youtubeAPI, new YouTubePlayer.OnInitializedListener() {
                        @Override
                        public void onInitializationSuccess(YouTubePlayer.Provider provider, final YouTubePlayer youTubePlayer, boolean b) {
                            Log.e("player", "onInitializationSuccess");
                            youTubePlayer1 = youTubePlayer;
                            youTubePlayer.loadVideo("fhWaJi1Hsfo");
                            youTubePlayer.setShowFullscreenButton(true);
                        }
                        @Override
                        public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
                            Log.e("player", "failure" + youTubeInitializationResult);
                        }
                    });

                break;
                case PhotosModel.IMAGE_TYPE:
                    Picasso.with(context1)
                            .load(model.getImageUrl())
                            .placeholder(R.drawable.banner)
                            .noFade()
                            .into(((ImageViewHolder) holder).imageView, new Callback() {
                                        @Override
                                        public void onSuccess() {
                                        }
                                        @Override
                                        public void onError() {
                                        }
                                    }
                            );
                    ((ImageViewHolder) holder).relativeLayout.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Download download = new Download(context1);
                            download.savePic(model.getImageUrl());

                        }
                    });
                    break;
            }
        }
    }

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

    public class ImageViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        RelativeLayout relativeLayout,rl_slider;
        protected FrameLayout containerYouTubePlayer;
        ImageViewHolder(View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.iv_slider);
            relativeLayout = itemView.findViewById(R.id.rl_download);
            rl_slider= itemView.findViewById(R.id.rl_slider);
        }
    }

    public class VideoViewHolder extends RecyclerView.ViewHolder {
        protected FrameLayout containerYouTubePlayer;
        VideoViewHolder(View itemView) {
            super(itemView);
            containerYouTubePlayer= itemView.findViewById(R.id.youtube_holder);
        }
    }
}

0 个答案:

没有答案