RecyclerView 没有在 cardview 上正确显示数据 (Android)?

RecyclerView not displaying data on cardview properly (Android)?

我正在制作一个文件管理器,我正在尝试使用 RecyclerView。问题是 recyclerview 没有在 cardview 上正确显示数据。它在不同的卡片视图上显示随机数据,有时显示相同的数据。有些卡片视图是空的,没有显示任何数据。我也没有保存 recyclerview 的状态。数据随机重复。我在片段中使用 recyclerview。有没有办法来解决这个问题?我是 android.

的新手开发者
public class FileInternalStorageRecyclerAdapter extends RecyclerView.Adapter<FileInternalStorageRecyclerAdapter.ViewHolder>{
Context context;
ArrayList<InternalStorageModel> fileList;
InternalStorageFragment.InternalStorageListener listener;
public FileInternalStorageRecyclerAdapter(Context context,ArrayList<InternalStorageModel> fileList, InternalStorageFragment.InternalStorageListener listener){
    this.fileList=fileList;
    this.listener=listener;
    this.context=context;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    CardView cardView=(CardView) LayoutInflater.from(context).inflate(R.layout.file_internal_storage_recycler_adapter,parent,false);
    return new ViewHolder(cardView);
}

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    final CardView cv=holder.cardView;
    final InternalStorageModel model=fileList.get(position);
    if (model.fileIsDirectory){
        if (model.fileName.startsWith(".")){
            holder.imgLogo.setImageResource(R.drawable.ic_hidden_folder_36dp);
            holder.txtName.setText(model.fileSimpleName+" ("+model.fileCountInDirectory+" )");
            return;
        }
        holder.imgLogo.setImageResource(R.drawable.ic_folder_36dp);
        holder.txtName.setText(model.fileSimpleName+" ("+model.fileCountInDirectory+" )");

    }else if (model.fileIsFile){
        if (model.fileName.startsWith(".")){
            holder.imgLogo.setImageResource(R.drawable.ic_hidden_file_36dp);
            holder.txtName.setText(model.fileSimpleName);
            return;
        }
        holder.imgLogo.setImageResource(R.drawable.ic_file_36dp);
        holder.txtName.setText(model.fileSimpleName);

    }
    cv.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            try{
                if (model.fileIsDirectory){
                    listener.navigationThroughFragment(model.fileAbsolutePath);

                }else if (model.fileIsFile){
                    Intent intent=new Intent(Intent.ACTION_VIEW);
                    intent.setDataAndType(Uri.fromFile(model.fileAbsoluteFile),"*/*");
                    context.startActivity(Intent.createChooser(intent,"Complete action using..."));
                }
            }catch(Exception e){
                e.printStackTrace();
            }
        }
    });

}

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

class ViewHolder extends RecyclerView.ViewHolder{
    private ImageView imgLogo;
    private TextView txtName;
    private CardView cardView;
    private ViewHolder(CardView itemView) {
        super(itemView);
        cardView=itemView;
        imgLogo=(ImageView)itemView.findViewById(R.id.imgLogo);
        txtName=(TextView)itemView.findViewById(R.id.txtName);
    }
}

}

我认为一定存在模型项不是文件也不是目录(符号链接?)的情况,这就是您遇到麻烦的地方。

适配器必须为每个 getView() 调用重新绑定数据,否则您将看到不相关的先前数据。尝试在模型不是文件而不是目录的地方添加另一个 else 块。

    // your code as it was
    // ...
} else if (model.fileIsFile) {
    // ...
    // your code as it was
    // ...
} else {
    holder.imgLogo.setImageResource(R.drawable.something);
    holder.txtName.setText(model.fileSimpleName);
}