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

Android 在具有多视图类型的“回收器”视图中滚动时有很多延迟,android,android-recyclerview,lag,Android,Android Recyclerview,Lag,我在应用程序中使用了具有多个视图的回收器视图。这很好,但我有一个问题,滚动这个回收视图!当我想要上下滚动它时,它有延迟。 这是我的回收器视图适配器: public class chatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { private ArrayList<ChatModel> chatArray; private Context mContext; public

我在应用程序中使用了具有多个视图的回收器视图。这很好,但我有一个问题,滚动这个回收视图!当我想要上下滚动它时,它有延迟。 这是我的回收器视图适配器:

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();
  }
}
公共类chatAdapter扩展了RecyclerView.Adapter{
私有数组列表;
私有上下文;
公共聊天适配器(ArrayList聊天阵列,上下文mContext){
this.chatary=chatary;
this.mContext=mContext;
}
公共静态类ViewHolderLeftText扩展了RecyclerView.ViewHolder{
文本视图TXT问题;
TextView-txtAnswer;
线性布局应答器布局;
公共视图持有者左文本(视图v){
超级(五);
txtQuestion=(TextView)v.findViewById(R.id.txtQuestion);
txtAnswer=(TextView)v.findViewById(R.id.txtAnswer);
应答布局=(线性布局)v.findviewbyd(R.id.answerLayout);
}
}
公共静态类ViewHolderLeftImage扩展了RecyclerView.ViewHolder{
公共视图持有者左视图(视图v){
超级(五);
}
}
公共静态类ViewHolderRightText扩展了RecyclerView.ViewHolder{
TextView RightTXT问题;
TextView rightTxtAnswer;
线性布局右应答器布局;
图像视图状态;
ImageView用户图像;
公共ViewHolderRightText(视图v){
超级(五);
rightTxtQuestion=(TextView)v.findViewById(R.id.right\u txtQuestion);
rightXtAnswer=(TextView)v.findViewById(R.id.right\u txtAnswer);
rightAnswerLayout=(线性布局)v.findViewById(R.id.right\u answerLayout);
状态=(图像视图)v.findviewbyd(R.id.avatar);
userImage=(ImageView)v.findViewById(R.id.img\u User\u Image);
}
}
公共静态类ViewHolderRightImage扩展了RecyclerView.ViewHolder{
图像视图图像;
图像视图状态;
公共ViewHolderRightImage(视图v){
超级(五);
image=(ImageView)v.findviewbyd(R.id.image);
状态=(图像视图)v.findviewbyd(R.id.avatar);
}
}
公共静态类ViewHolderLeftTextReply扩展了RecyclerView.ViewHolder{
TextView leftTextReplyPastQ;
TextView leftTextReplyPastA;
TextView leftTextReplyCurrentQ;
TextView leftTextReplyCurrentA;
公共ViewHolderLeftTextReply(视图v){
超级(五);
leftTextReplyPastQ=(TextView)v.findViewById(R.id.left\u reply\u txt\u pass\u question);
leftTextReplyPastA=(TextView)v.findViewById(R.id.left\u reply\u txt\u pass\u answer);
leftTextReplyCurrentQ=(TextView)v.findViewById(R.id.left\u reply\u txt\u current\u question);
leftTextReplyCurrentA=(TextView)v.findViewById(R.id.left\u reply\u txt\u current\u response);
}
}
公共静态类ViewHolderRightTextReply扩展了RecyclerView.ViewHolder{
TextView rightTextReplyPastQ;
TextView-rightTextReplyPastA;
TextView rightTextReplyCurrentQ;
TextView rightTextReplyCurrentA;
公共ViewHolderRightTextReply(视图v){
超级(五);
rightTextReplyPastQ=(TextView)v.findViewById(R.id.right\u reply\u txt\u pass\u question);
rightTextReplyPastA=(TextView)v.findViewById(R.id.right\u reply\u txt\u pass\u answer);
rightTextReplyCurrentQ=(TextView)v.findViewById(R.id.right\u reply\u txt\u current\u question);
rightTextReplyCurrentA=(TextView)v.findViewById(R.id.right\u reply\u txt\u current\u response);
}
}
@凌驾
public int getItemViewType(int位置){
/******************************处理内联父项**************
* *
*1用于项目\消息\文本\左侧
*2对于项目\信息\图像\左侧
*3对于项目\消息\文本\右侧
*4对于项目\信息\图像\右侧
*5对于项目\信息\左侧\回复
*6对于项目(信息)(右)(回复)
*7对于项目\u消息\u我的\u未发送
*  */
if(chatary.get(position.isleftext()&&(!chatary.get(position.isLeftReply())){
返回1;
}else if(chatary.get(position.isLeftImage()){
返回2;
}else if(chatary.get(position.isRightText()&&(!chatary.get(position.isRightReply())){
返回3;
}else if(chatArray.get(position.isRightImage()){
返回4;
}else if(chatary.get(position.isLeftReply()){
返回5;
}else if(chatArray.get(position.isRightReply()){
返回6;
}else if(chatary.get(position.isMineUnSend()){
返回7;
}else{//当没有消息显示时!
返回-1;
}
}
@凌驾
public RecyclerView.ViewHolder onCreateViewHolder(视图组父级,int-viewType){
//创建新视图
最终视图项\消息\文本\左=布局更平坦。从(mContext)。充气(R.layout.item \消息\文本\左,父,假);
最终视图项\消息\图像\左=布局更平坦。从(mContext)。充气(R.layout.item \消息\图像\左,父,假);
最终视图项\消息\文本\右=布局展开器。从(mContext)。充气(R.layout.item \消息\文本\右,父,假);
最终视图项\消息\图像\右侧=布局展开器。从(mContext)。充气(R.layout.item \消息\图像\右侧,父项,false);
最终视图项目\消息\回复\文本\左=布局更平坦。从(mContext)。充气(R.layout.item \消息\回复\文本\左,父,假);
最终视图项\消息\回复\文本\右=布局更平坦。从(mContext)。充气(R.layout.item \消息\回复\文本\右,父,假);
如果(viewType==1){
返回新的ViewHolderLeftText(item_message_text_left);//对于item message text left
}else if(viewType==2){
返回新的ViewHolderLeftImage(item_message_image_left);//对于item message image left
}else if(viewType==3){
返回新的ViewHolderRightText(item_message_text_right);//对于item message text right
}else if(viewType==4){
返回新的ViewHolderRightImage(item_message_image_right);//对于item message image right
}else if(viewType==5){
返回新的ViewHolderLeftTextReply(item_message_reply_text_left);//对于item message left text reply
}若否(vi)
 https://github.com/bumptech/glide