recyclerView whit多视图类型在滚动时变得混乱

时间:2018-07-25 16:15:05

标签: android android-recyclerview

我有一个recyclerView,其中包含三种视图;在视图类型2中,我正在检查文件是否在内部存储器上退出,因此我从存储器中加载了文件,以及  我不是在视图中心显示一个小的图像视图来下载它。当用户单击下载图像视图时,图像从下载图标更改为暂停图标,但是其他一些行会发生这种情况...

该问题的主要公认答案是

  1. holder.setIsRecyclable(false);
    但这毁了recyclerView的目的

2。覆盖此方法

@Override public long getItemId(int position) {
        return position; } 

@Override  public int getItemViewType(int position) {
   return position; }

和我的代码

  @Override
public int getItemViewType(int position) {
   int type = messages.get(position).getType();
    if (type==1) {
        return 1;
    }else if (type==2) {
        return 2 ;
    }else if (type==3) {
        return 3;
    }
    else {
        return -1;
    }

}

@Override
public long getItemId(int position) {
    return position;
}
  1. setHasStableIds(true);
    不起作用

4。在onBindViewHolder中编写if和else条件

  switch (holder.getItemViewType()) {
           case 1 :
               SimpleHolder  simpleHolder = (SimpleHolder)holder;
               simpleHolder.tv_text.setText(message.getMessage());
               simpleHolder.tv_time.setText(time);
               if (message.getLiked()==0) {
                   simpleHolder.iv_like.setImageDrawable(ContextCompat.getDrawable(context,R.drawable.unlike));
               }else {
                   simpleHolder.iv_like.setImageDrawable(ContextCompat.getDrawable(context,R.drawable.like));
               }

               break;
           case 2 :


               ImageHolder imageHolder = (ImageHolder)holder;
               //in method esme folder va esme aks ro migire o check mikone bebine mojode ya na
                if (isFileExists(Config.Folders.IMAGES ,message.getUrl())) {
                    imageHolder.circularProgressBar.setVisibility(View.GONE);
                    imageHolder.iv_download.setVisibility(View.GONE);
                    Glide.with(context).load(getFile(Config.Folders.IMAGES,message.getUrl())).apply(new RequestOptions()
                            .diskCacheStrategy(DiskCacheStrategy.ALL)

                    ) .into(new SimpleTarget<Drawable>() {
                        @Override
                        public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) {
                            ((ImageHolder) holder).iv_picture.setImageDrawable(resource);
                        }
                    });
                    imageHolder.iv_picture.setImageURI(Uri.fromFile(getFile(Config.Folders.IMAGES,message.getUrl())));
                }else {
                    imageHolder.iv_download.setVisibility(View.VISIBLE);
                    imageHolder.circularProgressBar.setVisibility(View.VISIBLE);
                    Glide.with(context).load(Config.MESSAGE_THUMB_ADDRESS+message.getThumb()).apply(new RequestOptions()
                            .diskCacheStrategy(DiskCacheStrategy.ALL)
                            .placeholder(ContextCompat.getDrawable(context,R.drawable.white))
                    ).into(imageHolder.iv_picture);
                }

                imageHolder.tv_time.setText(time);
               if (message.getMessage()!=null) {
                   imageHolder.tv_text.setVisibility(View.VISIBLE);
                   imageHolder.tv_text.setText(message.getMessage());
               }else {
                   imageHolder.tv_text.setVisibility(View.GONE);
               }
               if (message.getLiked()==0) {
                   imageHolder.iv_like.setImageDrawable(ContextCompat.getDrawable(context,R.drawable.unlike));
               }else {
                   imageHolder.iv_like.setImageDrawable(ContextCompat.getDrawable(context,R.drawable.like));
               }

               break;

但它们都不起作用
 我的适配器类的整个代码是

public class Message_Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
Context context;
ArrayList<Message> messages;
ClickListener clickListener;

public Message_Adapter(Context context, ArrayList<Message> messages) {
    this.context = context;
    this.messages = messages;
    setHasStableIds(true);
}

public interface ClickListener {
    public void picture_imageClicked(int position, View view, CircularProgressBar circularProgressBar, ImageView iv_download);

    public void picture_likeClicked(int position, View view);

    public void picture_commentClicked(int position, View view);

    public void simple_likeClicked(int position, View view);

    public void simple_commentClicked(int position, View view);

    public void video_imageClicked(int position, View view, CircularProgressBar circularProgressBar, ImageView iv_download);

    public void video_likeClicked(int position, View view);

    public void video_commentClicked(int position, View view);

}

public void setCliclListener(ClickListener cliclListener) {
    this.clickListener = cliclListener;
}


@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    if (viewType == 1) {
        View view = LayoutInflater.from(context).inflate(R.layout.custom_simple, parent, false);
        return new SimpleHolder(view);
    } else if (viewType == 2) {
        View view = LayoutInflater.from(context).inflate(R.layout.custom_picture, parent, false);
        CircularProgressBar circularProgressBar = (CircularProgressBar) view.findViewById(R.id.cp_picturee);
        circularProgressBar.setColor(ContextCompat.getColor(context, android.R.color.holo_blue_dark));
        circularProgressBar.setBackgroundColor(ContextCompat.getColor(context, android.R.color.holo_blue_dark));
        circularProgressBar.setProgressBarWidth(10);
        circularProgressBar.setBackgroundProgressBarWidth(5);
        int animationDuration = 2500; // 2500ms = 2,5s
        // circularProgressBar.setProgressWithAnimation(65, animationDuration);
        return new ImageHolder(view);
    } else if (viewType == 3) {
        View view = LayoutInflater.from(context).inflate(R.layout.custom_video, parent, false);
        CircularProgressBar circularProgressBar = (CircularProgressBar) view.findViewById(R.id.cp_video);
        circularProgressBar.setColor(ContextCompat.getColor(context, android.R.color.holo_blue_dark));
        circularProgressBar.setBackgroundColor(ContextCompat.getColor(context, android.R.color.holo_blue_dark));
        circularProgressBar.setProgressBarWidth(10);
        circularProgressBar.setBackgroundProgressBarWidth(5);
        return new VideoHolder(view);
    } else {
        throw new RuntimeException("The type has to be ONE or TWO");
    }

}

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
    Message message = messages.get(position);
    String time = "00:00";
    Date date = null;
    try {
        date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(message.getUpdated_at());
        time = new SimpleDateFormat("H:mm").format(date);
    } catch (ParseException e) {
        e.printStackTrace();
    }


    switch (holder.getItemViewType()) {
        case 1:
            SimpleHolder simpleHolder = (SimpleHolder) holder;
            simpleHolder.tv_text.setText(message.getMessage());
            simpleHolder.tv_time.setText(time);
            if (message.getLiked() == 0) {
                simpleHolder.iv_like.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.unlike));
            } else {
                simpleHolder.iv_like.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.like));
            }

            break;
        case 2:


            ImageHolder imageHolder = (ImageHolder) holder;
            //in method esme folder va esme aks ro migire o check mikone bebine mojode ya na
            if (isFileExists(Config.Folders.IMAGES, message.getUrl())) {
                imageHolder.circularProgressBar.setVisibility(View.GONE);
                imageHolder.iv_download.setVisibility(View.GONE);
                Glide.with(context).load(getFile(Config.Folders.IMAGES, message.getUrl())).apply(new RequestOptions()
                        .diskCacheStrategy(DiskCacheStrategy.ALL)

                ).into(new SimpleTarget<Drawable>() {
                    @Override
                    public void onResourceReady(Drawable resource, Transition<? super Drawable> transition) {
                        ((ImageHolder) holder).iv_picture.setImageDrawable(resource);
                    }
                });
                imageHolder.iv_picture.setImageURI(Uri.fromFile(getFile(Config.Folders.IMAGES, message.getUrl())));
            } else {
                imageHolder.iv_download.setVisibility(View.VISIBLE);
                imageHolder.circularProgressBar.setVisibility(View.VISIBLE);
                Glide.with(context).load(Config.MESSAGE_THUMB_ADDRESS + message.getThumb()).apply(new RequestOptions()
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .placeholder(ContextCompat.getDrawable(context, R.drawable.white))
                ).into(imageHolder.iv_picture);
            }

            imageHolder.tv_time.setText(time);
            if (message.getMessage() != null) {
                imageHolder.tv_text.setVisibility(View.VISIBLE);
                imageHolder.tv_text.setText(message.getMessage());
            } else {
                imageHolder.tv_text.setVisibility(View.GONE);
            }
            if (message.getLiked() == 0) {
                imageHolder.iv_like.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.unlike));
            } else {
                imageHolder.iv_like.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.like));
            }

            break;
        case 3:
            VideoHolder videoHolder = (VideoHolder) holder;
            videoHolder.tv_time.setText(time);
            if (message.getMessage() != null) {
                videoHolder.tv_text.setVisibility(View.VISIBLE);
                videoHolder.tv_text.setText(message.getMessage());
            } else {
                videoHolder.tv_text.setVisibility(View.GONE);
            }
            if (message.getLiked() == 0) {
                videoHolder.iv_like.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.unlike));
            } else {
                videoHolder.iv_like.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.like));
            }

            Glide.with(context).load(Config.VIDEO_PIC_ADDRES + message.getThumb()).apply(new RequestOptions()
                    .diskCacheStrategy(DiskCacheStrategy.ALL)

            ).into(videoHolder.iv_picture);


            if (isFileExists(Config.Folders.VIDEOS, message.getUrl())) {
                videoHolder.circularProgressBar.setVisibility(View.INVISIBLE);
                videoHolder.iv_download.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.play));
            } else {
                videoHolder.iv_download.setVisibility(View.VISIBLE);
                videoHolder.circularProgressBar.setVisibility(View.VISIBLE);
                videoHolder.iv_download.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.download));
            }

            String lenth = message.getLenth() < 1024 ? message.getLenth() + " kb" : message.getLenth() / 1024 + " mb";
            videoHolder.tv_video_time.setText(message.getTime() + " | " + lenth);


            break;


        default:
            break;


    }
}

public boolean isFileExists(String folderName, String filename) {
    if (filename == null) {
        filename = "null";
    }
    File file = new File(Environment.getExternalStorageDirectory() + "/NoorAlSalehin/" + folderName, filename);
    return file.exists();
}

public File getFile(String folderName, String filename) {
    File file = new File(Environment.getExternalStorageDirectory() + "/NoorAlSalehin/" + folderName, filename);
    return file;
}


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

@Override
public int getItemViewType(int position) {
    int type = messages.get(position).getType();
    if (type == 1) {
        return 1;
    } else if (type == 2) {
        return 2;
    } else if (type == 3) {
        return 3;
    } else {
        return -1;
    }

}

@Override
public long getItemId(int position) {
    return position;
}

class SimpleHolder extends RecyclerView.ViewHolder {
    TextView tv_text, tv_time;
    ImageView iv_like, iv_comment;

    public SimpleHolder(View itemView) {
        super(itemView);
        tv_text = (TextView) itemView.findViewById(R.id.tv_custom_simple_text);
        iv_like = (ImageView) itemView.findViewById(R.id.iv_simple_like);
        iv_comment = (ImageView) itemView.findViewById(R.id.iv_simple_comment);
        tv_time = (TextView) itemView.findViewById(R.id.tv_simple_time);
        iv_like.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                clickListener.simple_likeClicked(getAdapterPosition(), view);
            }
        });
        iv_comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                clickListener.simple_commentClicked(getAdapterPosition(), view);
            }
        });
    }
}

class ImageHolder extends RecyclerView.ViewHolder {
    ImageView iv_picture, iv_download, iv_like, iv_comment;
    TextView tv_text, tv_time;

    CircularProgressBar circularProgressBar;

    public ImageHolder(View itemView) {
        super(itemView);
        iv_download = (ImageView) itemView.findViewById(R.id.iv_picture_download);
        iv_picture = (ImageView) itemView.findViewById(R.id.iv_picture_image);
        circularProgressBar = (CircularProgressBar) itemView.findViewById(R.id.cp_picturee);
        tv_text = (TextView) itemView.findViewById(R.id.tv_picture_text);
        iv_comment = (ImageView) itemView.findViewById(R.id.iv_picture_comment);
        tv_time = (TextView) itemView.findViewById(R.id.tv_picture_time);
        iv_picture.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                clickListener.picture_imageClicked(getAdapterPosition(), view, circularProgressBar, iv_download);
            }
        });
        iv_like = (ImageView) itemView.findViewById(R.id.iv_picture_like);
        iv_like.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                clickListener.picture_likeClicked(getAdapterPosition(), view);
            }
        });
        iv_comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                clickListener.picture_commentClicked(getAdapterPosition(), view);
            }
        });
    }
}

class VideoHolder extends RecyclerView.ViewHolder {
    //  ImageView iv_picture   , iv_like , iv_comment;
    ImageView iv_picture, iv_download, iv_like, iv_comment;
    TextView tv_text, tv_time, tv_video_time;
    //  CircleProgressView circleProgressView ;
    CircularProgressBar circularProgressBar;

    public VideoHolder(View itemView) {
        super(itemView);
        iv_download = (ImageView) itemView.findViewById(R.id.iv_video_download_playy);
        iv_picture = (ImageView) itemView.findViewById(R.id.iv_video_image);
        //    circleProgressView=(CircleProgressView)itemView.findViewById(R.id.cp_video_download);
        circularProgressBar = (CircularProgressBar) itemView.findViewById(R.id.cp_video);
        tv_text = (TextView) itemView.findViewById(R.id.tv_video_text);
        iv_comment = (ImageView) itemView.findViewById(R.id.iv_video_comment);
        tv_time = (TextView) itemView.findViewById(R.id.tv_video_time);
        iv_picture.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                clickListener.video_imageClicked(getAdapterPosition(), view, circularProgressBar, iv_download);
            }
        });
        tv_video_time = (TextView) itemView.findViewById(R.id.tv_video_video_time);
        iv_like = (ImageView) itemView.findViewById(R.id.iv_video_like);
        iv_like.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                clickListener.video_likeClicked(getAdapterPosition(), view);
            }
        });
        iv_comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                clickListener.video_commentClicked(getAdapterPosition(), view);
            }
        });

    }
}

}

0 个答案:

没有答案