首页 > 解决方案 > 如何动态添加建议(视图)并能够控制其中的 textView

问题描述

我的应用程序中有一个片段,它是一个基本的聊天机器人。对于那个聊天机器人,我希望能够提供用户可以回复的回复建议。

在此处输入图像描述

我在 xml 文件中有我想要的回复布局。

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">

<com.google.android.material.card.MaterialCardView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="4dp"
    android:layout_marginEnd="12dp"
    android:layout_marginStart="12dp"
    android:layout_gravity="end"
    android:padding="4dp"
    app:strokeColor="@color/suggestion_border"
    app:strokeWidth="2dp"
    app:cardElevation="0dp"
    app:cardCornerRadius="8dp">

    <TextView
        android:id="@+id/chat_suggestion"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:textSize="18dp"
        android:textColor="@color/chat_text_color"
        android:text="Suggestion"/>
</com.google.android.material.card.MaterialCardView>

虽然产生这样的东西

在此处输入图像描述

我有一个incoming_msg.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<TextView
    android:id="@+id/chat_message"
    // irrelevent
    android:textSize="18sp" />

<TextView
    android:id="@+id/chat_time"
    // irrelevent
    android:text="12:90 PM"
    android:textColor="#b4b2b2"
    android:textSize="12sp" />

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/chat_time">
    <LinearLayout
        android:id="@+id/suggestion_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

    </LinearLayout>
</ScrollView>

并且因为对话片段是一个回收者视图。我有一个onBindViewHolder

public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        DateFormat df = DateFormat.getTimeInstance(DateFormat.SHORT);

        switch (holder.getItemViewType()) {
            case 1:
                IncomingMsg iM = (IncomingMsg) holder;
                iM.msg.setText(msg.get(position).getMessage());
                iM.dt.setText(df.format(msg.get(position).getTime()));


                if (!msg.get(position).getSuggest().isEmpty()) {
                    if (iM.s.getChildCount() != msg.get(position).getSuggest().size()) {
                        for (Suggestion tempSuggestion: msg.get(position).getSuggest()) {
                            if (tempSuggestion.isWithImage()) {
                                // TODO
                            } else {
                                iM.s.addView(getLayoutInflater().inflate(R.layout.outgoing_suggestion, iM.p));
                            }
                        }
                    }
                }
                break;
        }
    }

在这一点上,我不知道如何继续,因为我无法横向滚动建议列表,也无法控制建议中显示的文本。

标签: javaandroidandroid-recyclerview

解决方案


推荐阅读