0

私はチャットアプリケーションを作成しようとしています。本当にうまくいっています。しかし今、私はメッセージを重力にしたいと思います。私は、送信されたメッセージの重力をどのように右側に置くことができるのか分かりません。 viewHolder.mView.setForegroundGravity();は機能しません。あなたのケースではFirebaserecyclerview Gravity

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> 
+0

のようなものに見えるかもしれませんので...あなたはあなたの問題を解決しましたか? –

答えて

0

あなたのリスト項目のレイアウトで2 TextViewを持っ考えるかもしれません。 1つのレイアウトが右側に揃えられ、もう一方のレイアウトが左側に揃うようにレイアウトを作成します。これで、左または右のレイアウトの表示をGONEまたはVISIBLEに設定するだけです。

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> 
関連する問題