【问题标题】:Android Java Set up recyclerview items that are right alignedAndroid Java 设置右对齐的recyclerview项目
【发布时间】:2021-07-31 12:43:30
【问题描述】:

我正在开发一个 android 聊天应用程序,我希望与您聊天的用户的消息向右对齐,他们的消息向左对齐。这是让你区分消息属于谁的条件,我只能写在bindview holder上:

if(model.getUidSender().equals(fAuth.getCurrentUser().getUid()))

但是如何将项目向右或向左对齐?

我也考虑过膨胀两种不同的布局,但我应该去并在 CreateViewHolder 中操作,其中我不会有 model.getUidSender 值。我该如何解决这个问题?

这是我的完整代码:

@Override
    protected void onStart() {
        super.onStart();


        CollectionReference collectRefMessage = FirebaseFirestore.getInstance().collection("roomChat")
                .document("rooms")
                .collection(chatId).document("message")
                .collection("messages");

        FirestoreRecyclerOptions<getMessage> options
                = new FirestoreRecyclerOptions.Builder<getMessage>()
                .setQuery(collectRefMessage.limit(1000).orderBy("date", Query.Direction.ASCENDING), getMessage.class)
                .build();

        FirestoreRecyclerAdapter<getMessage, getASetMsg> firestoreRecyclerAdapter = new FirestoreRecyclerAdapter<getMessage, getASetMsg>(options) {
            @Override
            protected void onBindViewHolder(@NonNull @NotNull getASetMsg holder, int position, @NonNull @NotNull getMessage model) {
                holder.txtMsgText.setText(model.getText());
                holder.dateATime.setText(model.getDate());

              //  uidCh = model.getUidSender();
                //if(model.getUidSender().equals(fAuth.getCurrentUser().getUid()))


            }

            @NonNull
            @NotNull
            @Override
            public getASetMsg onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {


                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_chat,parent,false);

                getASetMsg viewHolder = new getASetMsg(view);
                return viewHolder;
            }
        };

        rView.setAdapter(firestoreRecyclerAdapter);
        firestoreRecyclerAdapter.startListening();
    }

 public static class getASetMsg extends RecyclerView.ViewHolder {

        TextView txtMsgText, dateATime;

        public getASetMsg(@NonNull View itemView) {
            super(itemView);


            txtMsgText = itemView.findViewById(R.id.txtMsgText);
            dateATime = itemView.findViewById(R.id.dateATime);


        }
    }

【问题讨论】:

    标签: java android android-recyclerview


    【解决方案1】:

    我设法这样解决它:

     if(model.getUidSender().equals(fAuth.getCurrentUser().getUid())) {
                        final FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) holder.messageCard.getLayoutParams();
                        params.gravity = GravityCompat.END; // or GravityCompat.END
                        holder.messageCard.setLayoutParams(params);
                    }
    

    所以,这是聊天卡的布局:

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_alignParentEnd="true"
        app:cardBackgroundColor="@color/textC"
    
        android:layout_marginTop="10dp"
        >
    
        <com.google.android.material.card.MaterialCardView
            android:id="@+id/messageCard"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:cardBackgroundColor="@color/textC"
            app:cardCornerRadius="20dp">
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        <TextView
            android:id="@+id/txtMsgText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hi, this is a message"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="30dp"
            android:textColor="#FFFFFF"
            android:layout_marginTop="25dp"
            android:layout_marginBottom="20dp"
            android:textSize="22dp"/>
    
        <TextView
            android:id="@+id/dateATime"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="30dp"
            android:text="32/07/2021 00:00"
            android:layout_marginTop="5dp"
            android:textColor="@color/white"
            android:layout_marginLeft="30dp"
    
    
            />
    
        </RelativeLayout>
    
    </com.google.android.material.card.MaterialCardView>
    
    </FrameLayout>
    

    【讨论】:

      猜你喜欢
      • 2017-04-11
      • 2021-02-21
      • 1970-01-01
      • 1970-01-01
      • 2021-08-27
      • 2016-11-05
      • 2011-11-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多