我正在开发一个应用程序,我们需要在水平 Recyclerview 中使用垂直 Recyclerview。并且垂直 Recyclerview 有多种视图类型。所以问题是,当我向下滚动几个项目并再次返回时,只有嵌套的 recyclerview 填充了错误的数据。否则所有其他数据都是正确的。
就像在这张图片中,您可以看到 recyclerview 消失的正确数据
在这张图片中,您可以看到 recyclerview 填充了一些其他对象数据并在我向上滚动时显示图像
现在我已经尝试了一切 setHasStableIds(true) ,使用相同的 viewpool 使情况恶化,getItemId(); 但似乎没有任何效果。
外部回收器视图适配器
private ArrayList<BasePostData> basePostDataArrayList;
private Context mContext;
private RecyclerView.RecycledViewPool viewPool;
public PostFragmentListAdapter(ArrayList<BasePostData> basePostDataArrayList, Context mContext) {
this.basePostDataArrayList = basePostDataArrayList;
this.mContext = mContext;
setHasStableIds(true);
}
public static class MyviewHolder extends RecyclerView.ViewHolder{
ImageView profileImage,more;
TextView name,user_category,post_category,follow,like,comment,share,description,post_type,highlight;
RecyclerView recyclerView;
HorizontalPostRecyclerview horizontalAdapter;
public MyviewHolder(@NonNull View itemView) {
super(itemView);
Context context = itemView.getContext();
this.comment = itemView.findViewById(R.id.post_comment_textview);
this.name = itemView.findViewById(R.id.post_user_name);
this.user_category = itemView.findViewById(R.id.post_category);
this.post_category = itemView.findViewById(R.id.posts_multiple_categories);
this.follow = itemView.findViewById(R.id.post_follow_button);
this.like = itemView.findViewById(R.id.post_like_textview);
this.share = itemView.findViewById(R.id.post_whatsapp_tv);
this.profileImage = itemView.findViewById(R.id.post_profile_pic);
this.more = itemView.findViewById(R.id.post_more_tv);
this.description = itemView.findViewById(R.id.post_description_textview);
this.recyclerView =itemView.findViewById(R.id.post_content_recyclerview);
this.post_type= itemView.findViewById(R.id.post_type);
this.highlight= itemView.findViewById(R.id.post_highlighted);
// recyclerView = (RecyclerView) itemView.findViewById(R.id.horizontal_list);
}
}
@NonNull
@Override
public MyviewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.post_layout,viewGroup,false);
MyviewHolder myviewHolder = new MyviewHolder(view);
return myviewHolder;
}
@Override
public void onBindViewHolder(@NonNull MyviewHolder viewHolder, int position)
{
StringBuilder post_cat= new StringBuilder();
BasePostData basePostData = basePostDataArrayList.get(position);
viewHolder.description.setText(basePostData.getDiscription());
viewHolder.name.setText(basePostData.getUser());
viewHolder.post_type.setText(basePostData.getPost_type());
viewHolder.itemView.setTag(position);
if (basePostData.getUser_category()!=null){
viewHolder.user_category.setText("("+basePostData.getUser_parent_category()+") "+basePostData.getUser_category());
}else {
viewHolder.user_category.setText(basePostData.getUser_parent_category());
}
viewHolder.like.setText(String.valueOf(basePostData.getLike())+" Likes");
viewHolder.comment.setText(String.valueOf(basePostData.getTotal_comments())+" Comments");
for (int i = 0;i<basePostData.getPost_tag().size();i++)
{
post_cat.append(basePostData.getPost_tag().get(i).getTag()).append(" | ");
}
viewHolder.post_category.setText(post_cat.toString());
Picasso.get()
.load(basePostData.getUser_profile_pic())
.placeholder(R.drawable.profile_ic)
.error(R.drawable.profile_ic)
.into(viewHolder.profileImage);
//--------------------------------highlight answer--------------
if (basePostData.getHighlight_comment()!=null)
{
viewHolder.highlight.setVisibility(View.VISIBLE);
viewHolder.highlight.setText(basePostData.getHighlight_comment().getComment());
}
//---------------------------recyclerview for files-------------------------------
if (basePostData.getFile_type()!=null) {
viewHolder.recyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
viewHolder.horizontalAdapter = new HorizontalPostRecyclerview();
viewHolder. recyclerView.setAdapter(viewHolder.horizontalAdapter);
viewHolder.horizontalAdapter.setData(basePostData.getPost_file()); // List of Strings
viewHolder.horizontalAdapter.setRowIndex(position);
viewHolder.recyclerView.setVisibility(View.VISIBLE);
/*
Log.e("item ",basePostData.getFile_type());
viewHolder. recyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
*//* switch (basePostData.getFile_type()){
case "Image":
viewHolder. horizontalAdapter = new HorizontalPostRecyclerview(basePostData.getPost_file() ,mContext,ConstantsOSH.TYPE_IMAGE);
viewHolder. recyclerView.setAdapter(viewHolder.horizontalAdapter);
break;
case "Video":
viewHolder. horizontalAdapter = new HorizontalPostRecyclerview(basePostData.getPost_file() ,mContext,ConstantsOSH.TYPE_VIDEO);
viewHolder. recyclerView.setAdapter(viewHolder.horizontalAdapter);
break;
case "Document":
viewHolder. horizontalAdapter = new HorizontalPostRecyclerview(basePostData.getPost_file() ,mContext,ConstantsOSH.TYPE_DOC);
viewHolder. recyclerView.setAdapter(viewHolder.horizontalAdapter);
viewHolder.horizontalAdapter.setRowIndex(position);
break;
}*//*
if (basePostData.getFile_type().equalsIgnoreCase("image")){
viewHolder. horizontalAdapter = new HorizontalPostRecyclerview(basePostData.getPost_file() ,mContext,ConstantsOSH.TYPE_IMAGE);
}else if (basePostData.getFile_type().equalsIgnoreCase("video")){
viewHolder. horizontalAdapter = new HorizontalPostRecyclerview(basePostData.getPost_file() ,mContext,ConstantsOSH.TYPE_VIDEO);
}else if (basePostData.getFile_type().equalsIgnoreCase("document"))
viewHolder. horizontalAdapter = new HorizontalPostRecyclerview(basePostData.getPost_file() ,mContext,ConstantsOSH.TYPE_DOC);
viewHolder. recyclerView.setAdapter(viewHolder.horizontalAdapter);
viewHolder.horizontalAdapter.setRowIndex(position);
*/ }
}
@Override
public int getItemCount() {
return basePostDataArrayList.size();
}
public void add(ArrayList<BasePostData> basePostDatalist) {
basePostDataArrayList.addAll(basePostDatalist);
notifyDataSetChanged();
}
public void clear() {
basePostDataArrayList.clear();
notifyDataSetChanged();
}
@Override
public long getItemId(int position) {
BasePostData product = basePostDataArrayList.get(position);
return product.hashCode();
}
}
垂直嵌套的 RecyclerView 适配器
public class HorizontalPostRecyclerview extends
RecyclerView.Adapter<RecyclerView.ViewHolder> {
private ArrayList<PostFileModel> dataset;
private Context mContext;
private int mRowIndex = -1;
private int viewType;
public HorizontalPostRecyclerview() {
setHasStableIds(true);
}
public HorizontalPostRecyclerview(ArrayList<PostFileModel> dataset, Context mContext) {
this.dataset = dataset;
this.mContext = mContext;
}
public HorizontalPostRecyclerview(ArrayList<PostFileModel> dataset, Context mContext,int viewType) {
this.dataset = dataset;
this.mContext = mContext;
this.viewType = viewType;
}
public void setRowIndex(int index) {
mRowIndex = index;
}
public void setData(ArrayList<PostFileModel> data) {
if (dataset != data) {
dataset = data;
notifyDataSetChanged();
}
}
private class ImageViewHolder extends RecyclerView.ViewHolder {
private ImageView imageView,play_btn;
private TextView name;
public ImageViewHolder(View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.horizontal_image_layout_imageview);
// name = itemView.findViewById(R.id.post_image_gallery_name_text);
// play_btn = itemView.findViewById(R.id.post_image_gallery_play_btn);
}
}
private class VideoViewHolder extends RecyclerView.ViewHolder {
private ImageView imageView,play_btn;
private TextView name;
public VideoViewHolder(View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.horizontal_video_layout_imageview);
// name = itemView.findViewById(R.id.post_image_gallery_name_text);
play_btn = itemView.findViewById(R.id.horizontal_video_layout_play_btn);
}
}
private class DocViewHolder extends RecyclerView.ViewHolder {
private ImageView imageView,play_btn;
private TextView name;
public DocViewHolder(View itemView) {
super(itemView);
//imageView = itemView.findViewById(R.id.post_image_gallery_imageview);
name = itemView.findViewById(R.id.horizontal_doc_layout_textview);
play_btn = itemView.findViewById(R.id.horizontal_doc_layout_playbtn);
}
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View itemView;
// ImageViewHolder holder = new ImageViewHolder(itemView);
switch (i)
{
case ConstantsOSH.TYPE_IMAGE:
itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.horizontal_post_image_layout,viewGroup, false);
return new ImageViewHolder(itemView);
// break;
case ConstantsOSH.TYPE_DOC:
itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.horizontal_post_doc_layout,viewGroup, false);
return new DocViewHolder(itemView);
// break;
case ConstantsOSH.TYPE_VIDEO:
itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.horizontal_post_video_layout,viewGroup, false);
return new VideoViewHolder(itemView);
// break;
default:
itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.horizontal_post_image_layout,viewGroup, false);
return new ImageViewHolder(itemView);
}
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
// ImageViewHolder holder = (ImageViewHolder) viewHolder;
switch (dataset.get(i).getFile_type()){
case "Image":
ImageViewHolder holder = (ImageViewHolder) viewHolder;
Picasso.get().load(dataset.get(i).getFile()).error(R.drawable.camera_ic).placeholder(R.drawable.flag_india).into(holder.imageView);
holder.itemView.setTag(i);
/* Glide.with(holder.imageView.getContext()) // Bind it with the context of the actual view used
.load(dataset.get(i).getFile()) // Load the image
.thumbnail(0.2f) // make use of the thumbnail which can display a down-sized version of the image
.into(holder.imageView);
*/
break;
case "document":
DocViewHolder holder2 = (DocViewHolder) viewHolder;
holder2.play_btn.setImageResource(R.drawable.doc_ic_new);
holder2.play_btn.setVisibility(View.VISIBLE);
String[] split = dataset.get(i).getFile().split("post_file/");
holder2.name.setText(split[1]);
holder2.name.setVisibility(View.VISIBLE);
holder2.itemView.setTag(i);
break;
case "video":
VideoViewHolder holder3 = (VideoViewHolder) viewHolder;
long interval = 2000;
RequestOptions options = new RequestOptions().frame(interval).placeholder(R.drawable.camera_ic);
Glide.with(holder3.imageView.getContext()).asBitmap()
.load(dataset.get(i).getFile())
.apply(options)
.into(holder3.imageView);
holder3.play_btn.setVisibility(View.VISIBLE);
holder3.itemView.setTag(i);
break;
}
}
@Override
public int getItemCount() {
return dataset.size();
}
@Override
public long getItemId(int position) {
PostFileModel postFileModel = dataset.get(position);
return postFileModel.hashCode();
}
@Override
public int getItemViewType(int position) {
switch (dataset.get(position).getFile_type()) {
case "Image":
return ConstantsOSH.TYPE_IMAGE;
case "video":
return ConstantsOSH.TYPE_VIDEO;
case "document":
return TYPE_DOC;
default:
return -1;
}
}
}