如何在recyclerview中添加打字点

时间:2018-09-06 15:28:01

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

嗨,我正在尝试与Socket.IO建立1对1聊天,但是我发出声音后,另一部分正在向我输入内容,但是我的问题是我不知道如何在此github上添加加载点。 :

https://github.com/EyalBira/loading-dots/blob/master/example/src/main/java/com/eyalbira/example/TestActivity.java

我正在尝试让它看起来像Facebook Messenger,然后有人在这里输入您喜欢的图片,如下所示:

enter image description here

我的activity_chat.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#EEEEEE"
tools:context=".ui.activity.UserChatActivity">

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/tools"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/fitnessm8_green"
    android:fitsSystemWindows="true">

    <ImageView android:layout_gravity="center_vertical" android:id="@id/button_back" android:onClick="BackButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/back_arrow" android:layout_marginRight="30dp" />

    <FrameLayout android:id="@+id/profile_photo_thumbnail_frame" android:padding="3.0dip" android:layout_width="?actionBarSize" android:layout_height="?actionBarSize">

        <de.hdodenhof.circleimageview.CircleImageView
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/avater_image"
            android:src="@drawable/ic_no_user"
            android:layout_width="50dp"
            android:layout_height="50dp"
            app:civ_border_width="2dp"
            app:civ_border_color="#FFFFFF"/>
    </FrameLayout>

    <TextView android:textAppearance="@style/TextAppearance.AppCompat.Widget.ActionBar.Title" android:textColor="@android:color/white" android:id="@+id/chat_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="15dp" android:drawablePadding="5.0dip" android:layout_toRightOf="@+id/avater_image" android:layout_centerVertical="true" />

</android.support.v7.widget.Toolbar>

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view_messages"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginTop="0dp"
    app:layout_constraintBottom_toTopOf="@id/relativeLayout_message"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/toolbar" />

<RelativeLayout
    android:id="@+id/relativeLayout_message"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="1.0">

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_send_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:tint="?attr/colorPrimaryDark"
        app:fabSize="mini"
        app:srcCompat="@drawable/ic_image_black_24dp" />

    <android.support.design.widget.TextInputEditText
        android:id="@+id/editText_message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_toEndOf="@id/fab_send_image"
        android:background="@drawable/rect_oval_white"
        android:elevation="2dp"
        android:hint="Fire your message"
        android:padding="8dp" />

    <ImageView
        android:id="@+id/imageView_send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_centerVertical="true"
        android:layout_marginEnd="24dp"
        android:elevation="2dp"
        app:srcCompat="@drawable/ic_send_black_24dp" />
</RelativeLayout>

这里是我的适配器:

public class ChatRoomThreadAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

private String userId;
private int SELF = 100;
private static String today;

private Context mContext;
private ArrayList<ChatMessages> messageArrayList;

public class ViewHolder extends RecyclerView.ViewHolder {
    TextView message, timestamp, readedstatus;
    ImageView profileimage;

    public ViewHolder(View view) {
        super(view);
        message = (TextView) itemView.findViewById(R.id.message);
        timestamp = (TextView) itemView.findViewById(R.id.timestamp);
        readedstatus = (TextView) itemView.findViewById(R.id.readedstatus);
        profileimage = (ImageView) itemView.findViewById(R.id.avater_image);
    }
}

public ChatRoomThreadAdapter(Context mContext, ArrayList<ChatMessages> messageArrayList, String userId) {
    this.mContext = mContext;
    this.messageArrayList = messageArrayList;
    this.userId = userId;

    Calendar calendar = Calendar.getInstance();
    today = String.valueOf(calendar.get(Calendar.DAY_OF_MONTH));
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView;
    if (viewType == SELF) {
        itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.list_item_message_right, parent, false);
    } else {
        itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.list_item_message_left, parent, false);
    }
    return new ViewHolder(itemView);
}

@Override
public int getItemViewType(int position) {
    ChatMessages message = messageArrayList.get(position);
    if (message.getUser().getUserId().equals(userId)) {
        return SELF;
    }
    return position;
}

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
    ChatMessages message = messageArrayList.get(position);

    String timestamp = TextUtils.getDateTimeStampWithTime(message.getCreatedAt());
    String fimage = message.getUser().getUserprofilepicture();
    String readedat = TextUtils.getDateTimeStampWithTime(message.getReadedAt());

    if (message.getUser().getUserId().equals(userId)) {
        // Self
        ((ViewHolder) holder).message.setText(message.getMessage());

        if (message.getUser().getUsername() != null)
            ((ViewHolder) holder).timestamp.setText(timestamp);

        ((ViewHolder) holder).readedstatus.setVisibility(View.GONE);
        ((ViewHolder) holder).readedstatus.setVisibility(View.GONE);


    } else {
        // Other
        ((ViewHolder) holder).message.setText(message.getMessage());

        if (message.getUser().getUsername() != null)
            ((ViewHolder) holder).timestamp.setText(timestamp);

        if (fimage.isEmpty()) {
            Picasso.with(mContext).load(R.drawable.ic_no_user)
                    .resize(80, 80)
                    .into(((ViewHolder) holder).profileimage);
        } else {
            Picasso.with(mContext).load(fimage)
                    .resize(80, 80)
                    .into(((ViewHolder) holder).profileimage);
        }
        if (message.getReadedAt().toString().equals("0")){
            ((ViewHolder) holder).readedstatus.setText(mContext.getResources().getString(R.string.message_delivered));
        } else {
            ((ViewHolder) holder).readedstatus.setText(mContext.getResources().getString(R.string.message_readed_at) + " " + readedat);
        }
    }
}

@Override
public int getItemCount() {
    return messageArrayList.size();
}
}

这是我的代码,在这里我使用recyclerview获取所有消息:

                            ArrayList androidList = (ArrayList) response.body().getMessages();

                        messageArrayList = new ArrayList<>(androidList);

                        mAdapter = new ChatRoomThreadAdapter(getApplicationContext(), messageArrayList, selfUserId);

                        LinearLayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
                        recyclerView.setLayoutManager(layoutManager);
                        recyclerView.setItemAnimator(new DefaultItemAnimator());
                        recyclerView.setAdapter(mAdapter);

                        mAdapter.notifyDataSetChanged();
                        if (mAdapter.getItemCount() > 1) {
                            recyclerView.getLayoutManager().smoothScrollToPosition(recyclerView, null, mAdapter.getItemCount() - 1);
                        }

0 个答案:

没有答案