单击内部回收器视图项时如何获取主循环器视图位置的位置

时间:2017-11-09 08:56:33

标签: android android-recyclerview

我的水平recyclerView包含垂直recyclerview包含文字和媒体recyclerview内的图片和视频。如何获取点击recyclerview horizontal项目的垂直recyclerview项目的位置?

先谢谢,

评论Recycler View适配器:

public class ClassReviewsRecyclerViewAdapter extends 
RecyclerView.Adapter<ClassReviewsRecyclerViewAdapter.MyViewHolder> {

public static ArrayList<Reviews> getReviewsDataSet(){
    return reviewsDataSet;
}
public static ArrayList<Reviews> reviewsDataSet =new ArrayList<>();
private Context context;

public class MyViewHolder extends RecyclerView.ViewHolder {

    ImageView profileImage;
    RatingBar rating;
    TextView reviewerName,reviewDate,reviewerRating,reviewerComment;
    public final RecyclerView reviewerImages;


    public MyViewHolder(View itemView) {
        super(itemView);

        this.profileImage = (ImageView) itemView.findViewById(R.id.reviewer_profile_image);
        this.reviewerName = (TextView) itemView.findViewById(R.id.reviewer_name);
        this.reviewDate = (TextView) itemView.findViewById(R.id.reviewer_date_of_publish);
        this.reviewerRating = (TextView) itemView.findViewById(R.id.reviewer_rating_number);
        this.reviewerComment = (TextView) itemView.findViewById(R.id.reviewer_comment);
        this.rating=(RatingBar)itemView.findViewById(R.id.class_reviewer_rating);
        reviewerImages=(RecyclerView)itemView.findViewById(R.id.reviews_images);
        pd=(ProgressBar)itemView.findViewById(R.id.image_load_progressbar);
        reviewerImages.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));

    }
}

public ClassReviewsRecyclerViewAdapter(Context context,ArrayList<Reviews> data) {
    this.reviewsDataSet = data;
    this.context=context;

}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent,
                                       int viewType) {
    View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.recycler_view_review_item, parent, false);



    MyViewHolder myViewHolder = new MyViewHolder(view);
    return myViewHolder;
}

@Override
public void onBindViewHolder(final MyViewHolder holder, final int listPosition) {

    TextView textViewName = holder.reviewerName;
    TextView textViewDate = holder.reviewDate;
    TextView ratingText = holder.reviewerRating;
    TextView textViewComment = holder.reviewerComment;
    RatingBar ratingBar =holder.rating;
    ImageView imageView = holder.profileImage;

    textViewName.setText(reviewsDataSet.get(listPosition).getName());
    textViewDate.setText(reviewsDataSet.get(listPosition).getDate());

    ratingText.setText(reviewsDataSet.get(listPosition).getRate());
    textViewComment.setText(reviewsDataSet.get(listPosition).getText());
    ratingBar.setRating(Float.valueOf(reviewsDataSet.get(listPosition).getRate()));

    Picasso.with(holder.profileImage.getContext()).load(reviewsDataSet.get(listPosition).getImage()).into(holder.profileImage);



   if(reviewsDataSet.get(listPosition).getUploads().isEmpty()){
        holder.reviewerImages.setVisibility(View.GONE);
    }

    else{
    ReviewMediaRecyclerViewAdapter reviewImagesAdapter = new ReviewMediaRecyclerViewAdapter(reviewsDataSet.get(listPosition).getUploads());
    holder.reviewerImages.setAdapter(reviewImagesAdapter); }
}

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

}

查看上传RecyclerView适配器:

公共类ReviewMediaRecyclerViewAdapter扩展了RecyclerView.Adapter {

public ArrayList<Uploads> getDataSet(){
    return dataSet;
}
private static ArrayList<Uploads> dataSet=new ArrayList<>();

private Context context;
private int originalHeight = 0;
private boolean isViewExpanded = false;

public static class ImageViewHolder extends RecyclerView.ViewHolder {

    ImageView reviewImage;
    ImageView removeImage;

    public ImageViewHolder(final View itemView) {
        super(itemView);

        this.reviewImage = (ImageView) itemView.findViewById(R.id.review_image);

        reviewImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LayoutInflater inflater = LayoutInflater.from(itemView.getContext());
                View subView = inflater.inflate(R.layout.alert_dialog_image, null);
                final ImageView subImageView = (ImageView)subView.findViewById(R.id.dialog_imageview);

                    subImageView.setImageDrawable(reviewImage.getDrawable());


                AlertDialog.Builder builder = new AlertDialog.Builder(itemView.getContext());

                builder.setView(subView);
                AlertDialog alertDialog = builder.create();

                builder.show();
            }
        });



    } }

    public static class VideoViewHolder extends RecyclerView.ViewHolder {

        ImageView reviewVideo;
        ImageView removeImage;


        // ItemClickListener mItemClickListener;
        //CardView cardView;

        public VideoViewHolder(final View itemView) {
            super(itemView);

            this.reviewVideo = (ImageView) itemView.findViewById(R.id.review_video_thumbnail);

        }
    }



public ReviewMediaRecyclerViewAdapter(ArrayList<Uploads> data) {
    this.dataSet = data;

}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view;
    switch (viewType) {
        case 1:
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_item_review_images_v2, parent, false);
            return new ImageViewHolder(view);

        case 2:
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_item_review_type_video, parent, false);
            return new VideoViewHolder(view);

    }
    return null;
}

@Override
public int getItemViewType(int position) {

    switch (dataSet.get(position).getType()) {
        case "1":
            return Uploads.IMAGE_TYPE;
        case "2":
            return Uploads.VIDEO_TYPE;
        default:
            return -1;
    }
}

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int listPosition) {

    Uploads object = dataSet.get(listPosition);
    if (object != null) {
        switch (object.getType()) {
            case "1":

                Picasso.with(((ImageViewHolder) holder).reviewImage.getContext()).load(dataSet.get(listPosition).getUploadName()).into(((ImageViewHolder) holder).reviewImage );



                break;
            case "2":
                //Bitmap bitmap= ThumbnailUtils.createVideoThumbnail((dataSet.get(listPosition).getUploadName()), MediaStore.Video.Thumbnails.FULL_SCREEN_KIND);//filePath is your video file path.


                CreateThumbnails createThumbnails=new CreateThumbnails(dataSet.get(listPosition).getUploadName(),listPosition,((VideoViewHolder)holder).reviewVideo);
                createThumbnails.execute();

                ((VideoViewHolder)holder).reviewVideo.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent i= new Intent(((VideoViewHolder)holder).reviewVideo.getContext(), VideoPlayerActivity.class);
                        i.putExtra("VIDEO_URL",dataSet.get(listPosition).getUploadName());
                        ((VideoViewHolder)holder).reviewVideo.getContext().startActivity(i);
                    }
                });

                break;

        }
    }
}

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

public class CreateThumbnails extends AsyncTask<Void,Void,Bitmap> {

    private ImageView previewImageViewInstance;
    private int mPosition;
    private String imagePath;

    public CreateThumbnails(String imagePath, int mPosition, ImageView previewImageViewInstance) {
        this.imagePath = imagePath;
        this.mPosition = mPosition;
        this.previewImageViewInstance = previewImageViewInstance;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        previewImageViewInstance.setImageBitmap(null);
    }

    @Override
    protected Bitmap doInBackground(Void... params) {
        Bitmap bitmap = null;
        //String videoPath =dataSet.get(listPosition).getUploadName() ;
        MediaMetadataRetriever mediaMetadataRetriever = null;
        try {
            mediaMetadataRetriever = new MediaMetadataRetriever();
            if (Build.VERSION.SDK_INT >= 14)
                // no headers included
                mediaMetadataRetriever.setDataSource(imagePath, new HashMap<String, String>());
            else
                mediaMetadataRetriever.setDataSource(imagePath);
            //   mediaMetadataRetriever.setDataSource(videoPath);
            bitmap = mediaMetadataRetriever.getFrameAtTime(0);
        } catch (Exception e) {
            e.printStackTrace();

        } finally {
            if (mediaMetadataRetriever != null)
                mediaMetadataRetriever.release();
        }
        return bitmap;
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        super.onPostExecute(bitmap);
        if (bitmap != null)
            previewImageViewInstance.setImageBitmap(bitmap);

    }

}



}

1 个答案:

答案 0 :(得分:1)

尝试以下代码

  1. 将您的主Recyclerview持有者传递给内部回收者视图,如
  2.   

    ReviewMediaRecyclerViewAdapter reviewImagesAdapter = new   ReviewMediaRecyclerViewAdapter(reviewsDataSet.get(listPosition).getUploads(),保持件);

    2.改变内部的recyclelerview适配器,如下所示

    ClassReviewsRecyclerViewAdapter.MyViewHolder myViewHolder;
    
    public ReviewMediaRecyclerViewAdapter(ArrayList<Uploads> data,ClassReviewsRecyclerViewAdapter.MyViewHolder myViewHolder) {
        this.dataSet = data;
        this.myViewHolder = myViewHolder;
    
    }
    
    1. 现在您可以通过以下代码获得主要的Recyclerview位置
    2.   

      myViewHolder.getAdapterPosition();

      实施例: -

      ((VideoViewHolder)holder).reviewVideo.setOnClickListener(new View.OnClickListener() {
                          @Override
                          public void onClick(View v) {
      
      Toast.makeText(v.getContext(),myViewHolder.getAdapterPosition()+"", Toast.LENGTH_SHORT).show();
      
                              Intent i= new Intent(((VideoViewHolder)holder).reviewVideo.getContext(), VideoPlayerActivity.class);
                              i.putExtra("VIDEO_URL",dataSet.get(listPosition).getUploadName());
                              ((VideoViewHolder)holder).reviewVideo.getContext().startActivity(i);
                          }
                      });