Adding Different Layouts For Every View In Recyclerview
I am making an android chat app. I have a recyclerview to show messages. i have two different layouts for messages, one for sent and one for received. my recyclerview doesnt throw
Solution 1:
I've done something similar, you should focus mostly on getItemViewType
Here is how I do (also I suggest you to use RecyclerView
instead of ListView
)
@OverridepublicintgetItemViewType(int position){
if (/*your condition based on message received or sent*/) {
return VIEW_MESSAGE_RECEIVED;
}
else {
return VIEW_MESSAGE_SENT;
}
}
After, you just check what getItemViewType
has returned
@Overridepublic MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == VIEW_MESSAGE_RECEIVED) {
Viewview= LayoutInflater.from(parent.getContext())
.inflate(R.layout.chat_message_item_received, parent, false);
returnnewChatAdapter.MyViewHolder(view);
}else {
VieweventDataView= LayoutInflater.from(parent.getContext())
.inflate(R.layout.chat_message_item_sent, parent, false);
returnnewChatAdapter.MyViewHolder(view);
}
}
Solution 2:
Try this code.. remove listitem null check because once value is assign after that it not getting null.
add also add one more things if one layout is show than other is gone at time two layout not show to good..
if (currentChat.getType() == 1) {
listItem1 = LayoutInflater.from(context).inflate(R.layout.chat_item_user, parent, false);
listItem1.setVisibility(View.VISIBLE);
listItem2.setVisibility(View.GONE);
} else {
listItem2 = LayoutInflater.from(context).inflate(R.layout.chat_item_contact, parent, false);
listItem2.setVisibility(View.VISIBLE);
listItem1.setVisibility(View.GONE);
}
Post a Comment for "Adding Different Layouts For Every View In Recyclerview"