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

时间:2017-07-23 05:44:09

标签: android android-recyclerview recycler-adapter lag

我在我的应用程序中使用具有多个视图的回收器视图。这工作正常但我滚动此循环视图有问题!当我想要向上和向下滚动它时它会滞后。 这是我的Recycler视图适配器:

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 个答案:

答案 0 :(得分:2)

我认为问题不是多个布局,而是您在不同布局中进行的图像处理。

如果将大位图设置为较小的imageView,则会占用太多内存。因此最好将图像任务放在不同的线程上,因为你可以使用一些好的图像库,如“Picasso”或“Universal Image Loader”。

有关回收商视图的更多优化提示,Click here

答案 1 :(得分:0)

您可以使用Glide库非常快速地加载图像,并且滚动速度非常快 你可以找到这个Glide图书馆

 https://github.com/bumptech/glide

您可以在app模块buil.gradle文件中添加Glide库作为依赖项 在android Studio中打开ProjectStructure并单击应用程序,然后单击对话框右侧的+图标,然后单击库依赖项并搜索滑动,然后您可以添加所有您想要的滑动库 通过这种方式,你可以得到你想要的东西。