我使用Firebase編寫聊天應用程序。如何動態修改回收站視圖的佈局?
在ChatActivity中,有一個回收站視圖來顯示聊天信息。以下圖片是第一次加載是正確的。
當輸入郵件發送給其他人,一些消息的佈局變得不同。
的代碼MessageAdapter
public MessageAdapter(List<YeMessage> mMessageList){
this.mMsgList = mMessageList;
}
@Override
public MessageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_message, parent, false);
mContext = parent.getContext();
ref = FirebaseDatabase.getInstance().getReference();
return new MessageViewHolder(view);
}
public class MessageViewHolder extends RecyclerView.ViewHolder{
@BindView(R.id.msg_message)
TextView messageText;
@BindView(R.id.msg_imageView)
ImageView image;
public MessageViewHolder(View view){
super(view);
ButterKnife.bind(this, view);
}
}
@Override
public void onBindViewHolder(MessageViewHolder holder, int position) {
mAuth = FirebaseAuth.getInstance();
current_user_id = mAuth.getCurrentUser().getUid();
YeMessage c = mMsgList.get(position);
String from_user = c.getFrom();
ref.child("Users").child(from_user).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
RelativeLayout.LayoutParams rp_msg = (RelativeLayout.LayoutParams)holder.messageText.getLayoutParams();
RelativeLayout.LayoutParams rp_img = (RelativeLayout.LayoutParams)holder.image.getLayoutParams();
image = dataSnapshot.child("imageUrl").getValue().toString();
if(from_user.equals(current_user_id)){
//lp.gravity = Gravity.END;
//end
rp_img.addRule(RelativeLayout.ALIGN_PARENT_END);
//rp_img.addRule(RelativeLayout.ALIGN_PARENT_TOP);
holder.image.setLayoutParams(rp_img);
//rp_msg.addRule(RelativeLayout.ALIGN_BOTTOM, R.id.msg_imageView);
rp_msg.addRule(RelativeLayout.START_OF, R.id.msg_imageView);
holder.messageText.setLayoutParams(rp_msg);
Glide.with(mContext)
.load(image)
.into(holder.image);
}else {
//lp.gravity = Gravity.START;
rp_img.addRule(RelativeLayout.ALIGN_PARENT_START);
rp_img.addRule(RelativeLayout.ALIGN_PARENT_TOP);
holder.image.setLayoutParams(rp_img);
rp_msg.addRule(RelativeLayout.ALIGN_PARENT_TOP);
rp_msg.addRule(RelativeLayout.END_OF, R.id.msg_imageView);
holder.messageText.setLayoutParams(rp_msg);
Glide.with(mContext)
.load(image)
.into(holder.image);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
YeMessage msg = mMsgList.get(position);
holder.messageText.setText(msg.getMessage());
}
@Override
public int getItemCount() {
return mMsgList.size();
}
我想原因是我對回收視圖的生命週期知識太少。
我發現如果我想改變onBindViewHolder()中的引力,它每次都不會成功。因爲它會在調用onCreateViewHolder()時正確佈局。
如何確保每次調用onCreateViewHolder(),以完成編程創建佈局?
添加item_message.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/message_single_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation = "horizontal"
android:padding="15dp">
<android.support.v7.widget.AppCompatImageView
android:id="@+id/msg_imageView"
android:layout_width="45sp"
android:layout_height="45sp" />
<TextView
android:id="@+id/msg_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/message_background"
android:padding="10dp"
android:textColor="#ffffff"
/>
</RelativeLayout>
顯示你的XML還 – Ankita
相反,在每個項目將重心可以使用兩種不同的佈局與一個左對齊,另一一個與右對齊。 – Kunu
@Kunu有一個很好的觀點。看看這個更多的信息:https://stackoverflow.com/questions/26245139/how-to-create-recyclerview-with-multiple-view-type – Kuffs