如何动态修改回收站视图的布局?

时间:2017-09-15 12:26:00

标签: android android-recyclerview android-adapter layout-gravity

我使用Firebase编写聊天应用程序。

在ChatActivity中,有一个回收站视图可显示聊天信息。以下图片是第一次加载是正确的。

enter image description here

当输入发送给他人的消息时,某些消息的布局会变得不同。

enter image description here

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>

1 个答案:

答案 0 :(得分:2)

我认为您可以为传入和传出邮件设置两种不同的布局。在您更改设计时,它将来对您有用。

您可以根据需要自定义RecyclerView。您可以参考http://www.codexpedia.com/android/android-recyclerview-with-multiple-different-layouts/获取详细答案。

就你的布局而言。

对于收到的消息 -

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp">

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="#000000"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:text="askjjnads dma dm"
        android:layout_gravity="center"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="20dp"
        />

</LinearLayout>

用于传出消息 -

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="end"
    android:padding="10dp">



    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="askjjnads dma dm"
        android:layout_gravity="center"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="10dp"
        />

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="#000000"/>

</LinearLayout>