0

嗨,我開發了使用RecyclerView的twilio聊天。之後,我通過改變視圖類型來實現RecyclerView中的窗體視圖和調用屏幕作爲聊天消息項目。在RecyclerView聊天窗口內實現多個佈局

是這種有效的方法嗎?任何其他方式請建議我,謝謝。

這是我的代碼:

if (viewType == SELF) { 

itemView = LayoutInflater.from(parent.getContext()) 
    .inflate(R.layout.chat_item_user, parent, false); 

} else if (viewType == formView) { 

itemView = LayoutInflater.from(parent.getContext()) 
    .inflate(R.layout.chat_form_item, parent, false); 

} else { 

if (calling == true) { 

    itemView = LayoutInflater.from(parent.getContext()) 
    .inflate(R.layout.chat_calling_screen, parent, false); 

} else { 

    itemView = LayoutInflater.from(parent.getContext()) 
    .inflate(R.layout.chat_item_other, parent, false); 
} 

} 

適配器全碼:

class MessagesAdapter extends RecyclerView.Adapter <MessagesAdapter.ViewHolder> { 

class ViewHolder extends RecyclerView.ViewHolder { 
    public TextView messagedata, msgTime; 
    public ImageView profileImg; 
    public Button answer; 
    public Button decline; 
    public RelativeLayout formLayout; 

    public ViewHolder(View view) { 
    super(view); 
    messagedata = (TextView) view.findViewById(R.id.messagechat); 
    msgTime = (TextView) view.findViewById(R.id.msg_time); 
    profileImg = (ImageView) view.findViewById(R.id.chat_profile_img); 
    answer = (Button) view.findViewById(R.id.call_answer); 
    decline = (Button) view.findViewById(R.id.call_decline); 
    formLayout = (RelativeLayout) view.findViewById(R.id.form_container); 

    if (buttonAvaliable) { 

    answer.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     calling = false; 
     Intent answer = new Intent(MainActivity.this, VideoActivity.class); 
     finish(); 
     startActivity(answer); 
    } 
    }); 

    decline.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 

     calling = false; 
     int number = Integer.parseInt(String.valueOf(getAdapterPosition())); 
     finish(); 
     startActivity(getIntent()); 

    } 
    }); 

    } 

    buttonAvaliable = false; 

    if (formAvaliable) { 

    formLayout.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     Intent formActivity = new Intent(MainActivity.this, UserEvaluationChat.class); 
     startActivity(formActivity); 
    } 
    }); 

    } 

    formAvaliable = false; 

    } 
} 

public MessagesAdapter() { 

} 

@Override 
public MessagesAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
    View itemView; 

    if (viewType == SELF) { 

    itemView = LayoutInflater.from(parent.getContext()) 
    .inflate(R.layout.chat_item_user, parent, false); 

    } else if (viewType == formView) { 

    itemView = LayoutInflater.from(parent.getContext()) 
    .inflate(R.layout.chat_form_item, parent, false); 

    formAvaliable = true; 

    } else { 

    if (calling == true) { 

    itemView = LayoutInflater.from(parent.getContext()) 
    .inflate(R.layout.chat_calling_screen, parent, false); 

    buttonAvaliable = true; 
    calling = false; 

    } else { 

    itemView = LayoutInflater.from(parent.getContext()) 
    .inflate(R.layout.chat_item_other, parent, false); 
    } 

    calling = false; 

    } 
    return new ViewHolder(itemView); 
} 

@Override 
public int getItemViewType(int position) { 
    Message message = mMessages.get(position); 

    String userId = String.valueOf(getSharedPref("userId") + "_user").toLowerCase().trim(); 
    String author = String.valueOf(message.getAuthor()).toLowerCase().trim(); 

    if (message.getAuthor().equals(userId)) { 
    return SELF; 
    } 

    if (!message.getAuthor().equals(userId) && message.getMessageBody().equals("patientevaluation__form")) { 
    return formView; 
    } 

    return position; 
} 

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

    Message message = mMessages.get(position); 

    String messageText = String.format("%s", message.getMessageBody()); 

    if (holder.getItemViewType() == formView) { 

    formAvaliable = true; 

    } else { 

    formAvaliable = false; 
    calling = false; 

    holder.messagedata.setTextSize(18); 
    holder.messagedata.setText(messageText); 

    } 

} 

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

這就是我想要的UI。 enter image description here

+0

請從'Adapter'發佈更多的代碼,這樣我們就可以告訴你這是否是正確的做法。 –

+0

@BartekLipinski我添加了適配器完整代碼 – yathavan

回答

0

假設你使用ViewHolders,你的實現似乎是正確的。但是,一種改進是覆蓋getItemViewType(int position)方法以返回每個元素視圖類型(由其位置和calling變量給出)。這樣,您可以用單個switch句子替換代碼中的if句子。