1

I am using a recycler view with multiple views in my application. this works fine but I have a problem with scrolling this recycle view! it has lag when I want to scroll it up and down. This is my recycler view adapter:

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();
  }
}

This recycler view has more than 6 view type that I have to show but when items in recycler view will be increased I have a lot of lag in scrolling it up and down. I don't know why I have lag in scrolling! I really appreciate your help.

Ehsan
  • 2,676
  • 6
  • 29
  • 56
  • It would be better if you can show the layout xml. Is there any NestedScrollVIew in your layout wrapping the RecyclerView ? If yes then set nested scrolling in recycler view to false, recyclerView.setNestedScrollingEnabled(false) and obviously there seems some image processing/decoding which may be affecting the rendering and smooth scrolling. – Prashant Jul 23 '17 at 16:40
  • How did you solved It ? – ankalagba May 24 '21 at 09:52

2 Answers2

2

I think the problem is not multiple layouts, but the image processing you are doing in the different layouts.

If you set large bitmap to smaller imageView it would take too much memory. So it is better to put image tasks on different thread, for that you can use some good image library like "Picasso" or "Universal Image Loader".

For More optimization tips about recycler view, Click here

Shivam
  • 492
  • 5
  • 18
0

You can use Glide library to load images very fast and also the scrolling is very fast you can find this Glide library

 https://github.com/bumptech/glide

you can add Glide library in app module buil.gradle file as a dependency in android Studio open ProjectStructure and click on app andthen click on + icon on rightside of dialog then click library dependency and search for glide then you can add glide library thats all you want in this way you can get what you want .

Hemanth Kumar
  • 257
  • 3
  • 12