当前位置: 首页 > 知识库问答 >
问题:

在具有多视图类型的回收器视图中滚动有很多延迟

居京
2023-03-14

我在我的应用程序中使用了具有多个视图的回收器视图。这工作正常,但我有一个问题,滚动这个回收视图!当我想上下滚动时,它有滞后现象。这是我的回收器视图适配器:

public class chatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

  private ArrayList<ChatModel> chatArray;
  private Context mContext;


  public chatAdapter(ArrayList<ChatModel> chatArray, Context mContext) {
    this.chatArray = chatArray;
    this.mContext = mContext;
  }

  public static class ViewHolderLeftText extends RecyclerView.ViewHolder {

    TextView txtQuestion;
    TextView txtAnswer;
    LinearLayout answerLayout;

    public ViewHolderLeftText(View v) {
      super(v);

      txtQuestion = (TextView) v.findViewById(R.id.txtQuestion);
      txtAnswer = (TextView) v.findViewById(R.id.txtAnswer);
      answerLayout = (LinearLayout) v.findViewById(R.id.answerLayout);

    }
  }

  public static class ViewHolderLeftImage extends RecyclerView.ViewHolder {

    public ViewHolderLeftImage(View v) {
      super(v);

    }
  }

  public static class ViewHolderRightText extends RecyclerView.ViewHolder {

    TextView rightTxtQuestion;
    TextView rightTxtAnswer;
    LinearLayout rightAnswerLayout;
    ImageView state;
    ImageView userImage;

    public ViewHolderRightText(View v) {
      super(v);

      rightTxtQuestion = (TextView) v.findViewById(R.id.right_txtQuestion);
      rightTxtAnswer = (TextView) v.findViewById(R.id.right_txtAnswer);
      rightAnswerLayout = (LinearLayout) v.findViewById(R.id.right_answerLayout);
      state = (ImageView) v.findViewById(R.id.avatar);
      userImage = (ImageView) v.findViewById(R.id.img_User_Image);
    }
  }

  public static class ViewHolderRightImage extends RecyclerView.ViewHolder {

    ImageView image;
    ImageView state;

    public ViewHolderRightImage(View v) {
      super(v);

      image = (ImageView) v.findViewById(R.id.image);
      state = (ImageView) v.findViewById(R.id.avatar);

    }
  }


  public static class ViewHolderLeftTextReply extends RecyclerView.ViewHolder {

    TextView leftTextReplyPastQ;
    TextView leftTextReplyPastA;
    TextView leftTextReplyCurrentQ;
    TextView leftTextReplyCurrentA;

    public ViewHolderLeftTextReply(View v) {
      super(v);

      leftTextReplyPastQ = (TextView) v.findViewById(R.id.left_reply_txt_past_question);
      leftTextReplyPastA = (TextView) v.findViewById(R.id.left_reply_txt_past_answer);
      leftTextReplyCurrentQ = (TextView) v.findViewById(R.id.left_reply_txt_current_question);
      leftTextReplyCurrentA = (TextView) v.findViewById(R.id.left_reply_txt_current_answer);

    }
  }

  public static class ViewHolderRightTextReply extends RecyclerView.ViewHolder {

    TextView rightTextReplyPastQ;
    TextView rightTextReplyPastA;
    TextView rightTextReplyCurrentQ;
    TextView rightTextReplyCurrentA;

    public ViewHolderRightTextReply(View v) {
      super(v);

      rightTextReplyPastQ = (TextView) v.findViewById(R.id.right_reply_txt_past_question);
      rightTextReplyPastA = (TextView) v.findViewById(R.id.right_reply_txt_past_answer);
      rightTextReplyCurrentQ = (TextView) v.findViewById(R.id.right_reply_txt_current_question);
      rightTextReplyCurrentA = (TextView) v.findViewById(R.id.right_reply_txt_current_answer);

    }
  }


  @Override
  public int getItemViewType(int position) {


    /******************************Handling Inline Parents**************
     * *
     * 1 for item_message_text_left
     * 2 for item_message_image_left
     * 3 for item_message_text_right
     * 4 for item_message_image_right
     * 5 for item_message_left_reply
     * 6 for item_message_right_reply
     * 7 for item_message_mine_unSend
     *  */
    if (chatArray.get(position).isLeftText() && (!chatArray.get(position).isLeftReply())) {
      return 1;
    } else if (chatArray.get(position).isLeftImage()) {
      return 2;
    } else if (chatArray.get(position).isRightText() && (!chatArray.get(position).isRightReply())) {
      return 3;
    } else if (chatArray.get(position).isRightImage()) {
      return 4;
    } else if (chatArray.get(position).isLeftReply()) {
      return 5;
    } else if (chatArray.get(position).isRightReply()) {
      return 6;
    } else if (chatArray.get(position).isMineUnSend()) {
      return 7;
    } else {//When there is no message to show!
      return -1;
    }
  }

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

    // Create a new View
    final View item_message_text_left = LayoutInflater.from(mContext).inflate(R.layout.item_message_text_left, parent, false);
    final View item_message_image_left = LayoutInflater.from(mContext).inflate(R.layout.item_message_image_left, parent, false);
    final View item_message_text_right = LayoutInflater.from(mContext).inflate(R.layout.item_message_text_right, parent, false);
    final View item_message_image_right = LayoutInflater.from(mContext).inflate(R.layout.item_message_image_right, parent, false);
    final View item_message_reply_text_left = LayoutInflater.from(mContext).inflate(R.layout.item_message_reply_text_left, parent, false);
    final View item_message_reply_text_right = LayoutInflater.from(mContext).inflate(R.layout.item_message_reply_text_right, parent, false);


    if (viewType == 1) {
      return new ViewHolderLeftText(item_message_text_left);  //For item message text left
    } else if (viewType == 2) {
      return new ViewHolderLeftImage(item_message_image_left); //For item message image left
    } else if (viewType == 3) {
      return new ViewHolderRightText(item_message_text_right); //For item message text right
    } else if (viewType == 4) {
      return new ViewHolderRightImage(item_message_image_right); //For item message image right
    } else if (viewType == 5) {
      return new ViewHolderLeftTextReply(item_message_reply_text_left); //For item message left text reply
    } else if (viewType == 6) {
      return new ViewHolderRightTextReply(item_message_reply_text_right); //For item message right text reply
    } else if (viewType == 7) {
      return new ViewHolderRightText(item_message_text_right); //For item message right text unSend
    } else {
      return new ViewHolderLeftText(item_message_text_left);  //When there is nothing to show!
    }
  }


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


    switch (holder.getItemViewType()) {

      case 1: //For item message text left
        ViewHolderLeftText vLText = (ViewHolderLeftText) holder;
        vLText.txtQuestion.setText(chatArray.get(position).getCourseForumModel().getCourseForumQuestion());
        if (!chatArray.get(position).getCourseForumModel().getCourseForumAnswer().equals("")) {
          if (chatArray.get(position).getCourseForumModel().getCourseForumAUser().equals(G.userId)) {
            //This Answer is prepared by this user
            vLText.answerLayout.setBackgroundColor(mContext.getResources().getColor(R.color.my_message_background_color));
          }
          vLText.txtAnswer.setText(chatArray.get(position).getCourseForumModel().getCourseForumAnswer());
        }
        break;

      case 2: //For item message image left
        ViewHolderLeftImage vLImage = (ViewHolderLeftImage) holder;
        break;

      case 3: //For item message Text Right
        ViewHolderRightText vRText = (ViewHolderRightText) holder;
        vRText.rightTxtQuestion.setText(chatArray.get(position).getCourseForumModel().getCourseForumQuestion());
        if (!chatArray.get(position).getCourseForumModel().getCourseForumAnswer().equals("")) {
          vRText.rightTxtAnswer.setText(chatArray.get(position).getCourseForumModel().getCourseForumAnswer());
        }
        vRText.state.setImageResource(R.drawable.send_icon);

        break;

      case 4: //For item message image Right

        final ViewHolderRightImage vRImage = (ViewHolderRightImage) holder;
        int userFileId = chatArray.get(position).getCourseForumModel().getCourseForumQFile();
        FileModel fileModel = InternetService.getSingleFile(userFileId);
        Bitmap userImage = BitmapFactory.decodeFile(G.DIR_APP + fileModel.getFileName() + "." + fileModel.getFileExtension());
        vRImage.image.setImageBitmap(userImage);
        vRImage.state.setImageResource(R.drawable.send_icon);


        break;

      case 5: //For item message left reply

        ViewHolderLeftTextReply vLTReply = (ViewHolderLeftTextReply) holder;
        vLTReply.leftTextReplyPastQ.setText(chatArray.get(position).getReplayedMessage().getCourseForumQuestion());
        vLTReply.leftTextReplyPastA.setText(chatArray.get(position).getReplayedMessage().getCourseForumAnswer());
        vLTReply.leftTextReplyCurrentQ.setText(chatArray.get(position).getCourseForumModel().getCourseForumQuestion());
        vLTReply.leftTextReplyCurrentA.setText(chatArray.get(position).getCourseForumModel().getCourseForumAnswer());


        break;

      case 6: //For item message right reply

        ViewHolderRightTextReply vRTReply = (ViewHolderRightTextReply) holder;
        vRTReply.rightTextReplyPastQ.setText(chatArray.get(position).getReplayedMessage().getCourseForumQuestion());
        vRTReply.rightTextReplyPastA.setText(chatArray.get(position).getReplayedMessage().getCourseForumAnswer());
        vRTReply.rightTextReplyCurrentQ.setText(chatArray.get(position).getCourseForumModel().getCourseForumQuestion());
        vRTReply.rightTextReplyCurrentA.setText(chatArray.get(position).getCourseForumModel().getCourseForumAnswer());

        break;

      case 7: //For item message That not send till now

        ViewHolderRightText vRUnSendText = (ViewHolderRightText) holder;

        String question = chatArray.get(position).getUnSendChats().getCourseForum().getCourseForumQuestion();
        int hasFile = chatArray.get(position).getUnSendChats().getHasFile();

        vRUnSendText.rightTxtQuestion.setText(question);
        vRUnSendText.state.setImageResource(R.drawable.un_send_icon);

        if (hasFile == 1) {//This UnSend Message has a file
          String filePath = chatArray.get(position).getUnSendChats().getFilePath();
          Bitmap unSendImage = BitmapFactory.decodeFile(filePath);
          vRUnSendText.userImage.setImageBitmap(unSendImage);
        }
        break;

      case -1: //When there is no message to show
        ViewHolderLeftText vLTextForNoMessage = (ViewHolderLeftText) holder;
        break;

    }
  }


  public void update(ArrayList<ChatModel> updatedArray) {
    chatArray.clear();
    chatArray.addAll(updatedArray);
    notifyDataSetChanged();
  }

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

这个回收器视图有6种以上的视图类型,我必须显示,但是当回收器视图中的项目增加时,我在上下滚动时会有很大的延迟。我不知道为什么我在滚动上有延迟!我真的很感谢你的帮助。

共有2个答案

濮金鑫
2023-03-14

你可以使用滑动库非常快地加载图像,滚动也非常快,你可以找到这个滑动库

 https://github.com/bumptech/glide

你可以在应用程序模块buil.gradle文件中添加Glide库作为android Studio中的依赖项,打开ProjectStructure,然后单击应用程序,然后单击对话框右侧的图标,然后单击库依赖项并搜索glide,然后您可以添加您喜欢的所有滑动库,这样您就可以获得您想要的。

鞠嘉志
2023-03-14

我认为问题不是多重布局,而是你在不同布局中做的图像处理。

如果将大位图设置为较小的imageView,则会占用太多内存。因此,最好将图像任务放在不同的线程上,因为您可以使用一些好的图像库,如“毕加索”或“通用图像加载器”。

有关回收器视图的更多优化提示,请单击此处

 类似资料:
  • 在其他回收器视图中有一个回收器视图。两者都需要垂直滚动。外部回收器视图滚动正常,但内部回收器视图滚动不正常。 这是代码: ViewAdapter如下: 我尝试了以下两种回收商观点,但都无法解决问题 也尝试了这个:

  • 我试图制作一个可在滚动条上生成项目的RecyclerView。方法 我试图实现的是使用循环生成一个无限的 示例:[示例_屏幕截图][1] 编辑:忘记在适配器构造函数中添加一些代码了!添加日志 类ViewHolder2扩展了RecyclerView。ViewHolder实现了视图。OnClickListener{

  • 我在recyclerView中遇到错误// 数据类类别( )这是模型///// 对象类别模型{ ////适配器/// 类CategoriesAdapter(private val itemList:ArrayList):RecyclerView。适配器 .充气(R.layout.memmals,parent,false)返回ViewHolder(视图) }/////主要活动/// 类MainAct

  • 我正在使用RecyclerView,并有一些动画,我可以在ViewHolder项目中放大一个相对布局。 注意:它不是添加/删除/插入动画。当用户在 ViewHolder 项中交互时,它将启动。因此,我在这里没有使用ItemAnimator。 动画工作正常,但它会在某个随机视图项中重新出现(最终状态)。我知道这是由于物品的重复使用,我也在清除动画,但这没有帮助。 我在节目里做这个 在onViewDe

  • 从使用RecycerView创建动态列表: 当我们创建时,我们必须指定将与适配器绑定的。 是否可以创建具有多种视图类型的?

  • 我正在使用recyclerView,我成功地扩展了两个视图,但每个视图的内容都来自不同的json数据类型。我尝试在适配器中传递这两个数据类型,但它们没有正确绑定 > 源代码 公共类SimpleStringRecycleWebAdapter:RecycleWebView。适配器{ } 公共类SimpleViewHolder:RecyclerView。ViewHolder{public string