我正在使用firebase开发一个Android聊天,但我在分配消息的严重性方面遇到了问题。发件人的邮件在左边,收件人的邮件在右边。
这是我的代码:
@Override
public void onStart() {
super.onStart();
mFirebaseAdapter1 = new FirebaseRecyclerAdapter<ChatModel, ChatMessageViewHolder>(ChatModel.class,
R.layout.textview,
ChatMessageViewHolder.class,
ref_chatchildnode1) {
@Override
protected void populateViewHolder(ChatMessageViewHolder chatMessageViewHolder, ChatModel m, int i) {
if (m.getSender().equals(LoggedInUser)) {
chatMessageViewHolder.msg.setText(m.getMessage());
chatMessageViewHolder.msg.setBackground(ContextCompat.getDrawable(getActivity(), R.color.user));
} else {
chatMessageViewHolder.msg.setText(m.getMessage());
chatMessageViewHolder.msg.setBackground(ContextCompat.getDrawable(getActivity(),R.color.ae));
}
}
};
mRecyclerView.setAdapter(mFirebaseAdapter1);
//mRecyclerView.setAdapter(mFirebaseAdapter2);
mFirebaseAdapter1.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
@Override
public void onItemRangeInserted(int positionStart, int itemCount) {
super.onItemRangeInserted(positionStart, itemCount);
int friendlyMessageCount = mFirebaseAdapter1.getItemCount();
int lastVisiblePosition = linearLayoutManager.findLastCompletelyVisibleItemPosition();
// If the recycler view is initially being loaded or the
// user is at the bottom of the list, scroll to the bottom
// of the list to show the newly added message.
if (lastVisiblePosition == -1 ||
(positionStart >= (friendlyMessageCount - 1) &&
lastVisiblePosition == (positionStart - 1))) {
mRecyclerView.scrollToPosition(positionStart);
}
}
});
}
我尝试以编程方式执行LinearLayout但没有任何反应。这是我做的:
if (m.getSender().equals(LoggedInUser)) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.weight = 1.0f;
params.gravity = Gravity.RIGHT;
chatMessageViewHolder.msg.setText(m.getMessage());
chatMessageViewHolder.msg.setLayoutParams(params);
chatMessageViewHolder.msg.setBackground(ContextCompat.getDrawable(getActivity(), R.color.user));
} else {
chatMessageViewHolder.msg.setText(m.getMessage());
chatMessageViewHolder.msg.setGravity(Gravity.LEFT);
chatMessageViewHolder.msg.setBackground(ContextCompat.getDrawable(getActivity(),R.color.ae));
}
请帮我构建我的代码。
答案 0 :(得分:0)
现在我可以看到2个可能的解决方案... 第一个尝试使用两个带有firebaseUI的ViewHolders(我不知道是否可以这样做)和第二个,最简单,每行使用相对布局并放入气泡聊天视图,然后使用对齐父右或左父。
答案 1 :(得分:0)
我解决了我的问题。
这就是我所做的。
if (m.getSender().equals(LoggedInUser)) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.weight = 1.0f;
params.gravity = Gravity.RIGHT;
chatMessageViewHolder.msg.setText(m.getMessage());
chatMessageViewHolder.msg.setLayoutParams(params);
chatMessageViewHolder.msg.setBackground(ContextCompat.getDrawable(getActivity(), R.color.user));
} else {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.weight = 1.0f;
params.gravity = Gravity.LEFT;
chatMessageViewHolder.msg.setText(m.getMessage());
chatMessageViewHolder.msg.setLayoutParams(params);
chatMessageViewHolder.msg.setBackground(ContextCompat.getDrawable(getActivity(),R.color.ae));
}