Firebaserecyclerview Gravity

时间:2017-01-22 14:10:24

标签: android firebase android-recyclerview firebase-realtime-database

我正在尝试创建聊天应用。它工作得非常好。但现在我想重整这些信息。我不知道如何将发送的消息的重力设置到右侧。 viewHolder.mView.setForegroundGravity();不起作用。

public void populateViewHolder(final BlogViewHolder viewHolder, final Blog model, final int position) {
    DatabaseReference getname = FirebaseDatabase.getInstance().getReference().child("Users").child(userID).child("Username");
    getname.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            //Grün und weiß setzen
            String myname = dataSnapshot.getValue(String.class);
            if(myname.equals(model.getUsername())){
                LinearLayout linear = (LinearLayout)findViewById(R.id.singleMessageContainer);
                linear.setGravity(Gravity.RIGHT);
                viewHolder.setNachricht(model.getNachricht());
            }else{
                viewHolder.setNachricht(model.getNachricht());
            }
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
}
public static class BlogViewHolder extends  RecyclerView.ViewHolder{
    View mView;
    public BlogViewHolder(View itemView) {
        super(itemView);
        mView = itemView;
    }
    public void setNachricht(String Nachricht){
        TextView post_Nachricht = (TextView)mView.findViewById(R.id.textViewMessage);
        post_Nachricht.setText(Nachricht);
    }
    public void setUsername(String Username){
        TextView post_Nachricht = (TextView)mView.findViewById(R.id.kommentareimg);
        post_Nachricht.setText(Html.fromHtml("<b>" + Username + ":</b>"));
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/singleMessageContainer"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="6dp">

    <LinearLayout
        android:id="@+id/warpper"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/textViewMessage"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_marginBottom="2dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="2dp"
            android:text="Message"
            android:textSize="14sp"
            tools:ignore="HardcodedText" />
    </LinearLayout>
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

在您的情况下,您可能会考虑在列表项布局中使用两个TextView。进行布局,使一个布局与右侧对齐,另一个布局与左侧对齐。现在,您只需将左侧或右侧布局的可见性设置为GONEVISIBLE即可。

因此,您需要在ViewHolder课程中使用其他一些功能。

public static class BlogViewHolder extends  RecyclerView.ViewHolder{
    View mView;
    public BlogViewHolder(View itemView) {
        super(itemView);
        mView = itemView;
    }

    public void setNachrichtRight(String Nachricht) {
        TextView post_Nachricht_Right = (TextView)mView.findViewById(R.id.textViewMessageRight);
        TextView post_Nachricht_Left = (TextView)mView.findViewById(R.id.textViewMessageLeft);
        post_NachrichtRight.setText(Nachricht);

        // Now set the visibility
        post_Nachricht_Right.setVisibility(View.VISIBLE);
        post_Nachricht_Left.setVisibility(View.GONE);
    }

    public void setNachrichtLeft(String Nachricht) {
        TextView post_NachrichtLeft = (TextView)mView.findViewById(R.id.textViewMessageLeft);
        TextView post_NachrichtRight = (TextView)mView.findViewById(R.id.textViewMessageRight);
        post_NachrichtLeft.setText(Nachricht);

        // Now set the visibility
        post_Nachricht_Right.setVisibility(View.VISIBLE);
        post_Nachricht_Left.setVisibility(View.GONE);
    }

    public void setUsername(String Username){
        TextView post_Nachricht = (TextView)mView.findViewById(R.id.kommentareimg);
        post_Nachricht.setText(Html.fromHtml("<b>" + Username + ":</b>"));
    }
}

所以在onDataChange函数中你可以考虑做这样的事情。

if(myname.equals(model.getUsername())){
    viewHolder.setNachrichtRight(model.getNachricht());
} else {
    viewHolder.setNachrichtLeft(model.getNachricht());
}

最后,您的布局可能看起来像这样

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/singleMessageContainer"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="6dp">

    <TextView
        android:id="@+id/textViewMessageRight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="2dp"
        android:alignParentRight="true"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="2dp"
        android:text="Message"
        android:textSize="14sp"
        tools:ignore="HardcodedText" />

    <TextView
        android:id="@+id/textViewMessageLeft"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:alignParentLeft="true"
        android:layout_marginBottom="2dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="2dp"
        android:text="Message"
        android:textSize="14sp"
        tools:ignore="HardcodedText" />

</RelativeLayout>