Гравитация не установлена ​​должным образом с LayoutParams в onBindViewHolder - PullRequest
0 голосов
/ 04 ноября 2018

У меня есть чат activity, который организует сообщения на основе имени пользователя отправителя. Все действия, описанные в onBindViewHolder, работают должным образом, за исключением Gravity контейнера (все сообщения выравнивают конец, когда они должны выравнивать начало и конец для партнера и пользователя, вошедшего в систему, соответственно). Что я сделал не так при реализации LayoutParams?

Вот мой код:

@Override
public void onBindViewHolder(@NonNull DMViewHolder holder, int position) {

    // This method fills fields with data for each list item


    Message message = messageList.get(position);

    String signedInUserID = FirebaseAuth.getInstance().getCurrentUser().getUid();

    boolean isSignedInUser = message.getAuthorUID().equals(signedInUserID);

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("hh:mm a");

    holder.message.setText(message.getMessage());
    holder.date.setText(simpleDateFormat.format(message.getTimestamp()));


    if (isSignedInUser) {
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.ALIGN_END);
        holder.singleDMContainer.setLayoutParams(params);

        holder.textContainer.setBackgroundResource(R.drawable.user_message_text_background);
    } else {
        RelativeLayout.LayoutParams params2= new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        params2.addRule(RelativeLayout.ALIGN_START);
        holder.singleDMContainer.setLayoutParams(params2);

        holder.textContainer.setBackgroundResource(R.drawable.partner_message_text_background);
    }


}

и xml-файл для сообщений:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/singleDMContainer"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:orientation="horizontal">


    <RelativeLayout
        android:id="@+id/text_container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true"
        android:layout_marginStart="10dp"
        android:layout_marginEnd="10dp"
        android:layout_marginBottom="5dp"
        android:background="@drawable/user_message_text_background">

        <TextView
            android:id="@+id/dm_message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:fontFamily="@font/nunito_extralight"
            android:gravity="start"
            android:padding="5dp"
            android:text="@string/sample_message"
            android:textColor="@android:color/black"
            android:textSize="18sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/dm_date"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_toEndOf="@+id/dm_message"
            android:fontFamily="@font/nunito_extralight"
            android:padding="5dp"
            android:text="@string/sample_time"
            android:textColor="@android:color/black"
            android:textSize="12sp"
            android:textStyle="normal" />

    </RelativeLayout>


</RelativeLayout>

1 Ответ

0 голосов
/ 04 ноября 2018

Примените правило к дочернему представлению, а не к контейнеру: замените

holder.singleDMContainer.setLayoutParams(params2);

с

holder.textContainer.setLayoutParams(params2);

ОБНОВЛЕНИЕ:

Также используйте RelativeLayout.ALIGN_PARENT_START вместо RelativeLayout.ALIGN_START и RelativeLayout.ALIGN_PARENT_END вместо RelativeLayout.ALIGN_END.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...