java - 如何动态添加建议(视图)并能够控制其中的 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;
}
}
在这一点上,我不知道如何继续,因为我无法横向滚动建议列表,也无法控制建议中显示的文本。
解决方案
推荐阅读
- ros - 无法为 ROS 安装 ps3joy 驱动程序
- lstm - CUDNN_STATUS_EXECUTION_FAILED ERROR 通过调用 cudnnRNNForwardTrainingEx 函数
- python - 大熊猫的分组计数,平均值,Q1,Q2,Q3和标准差
- java - 为自定义用户存储 SPI 创建映射器
- java - entitymanager.merge() 会在克隆的实体对象上工作吗
- maximo - 如何根据记录的状态使某一部分下的单行数据可编辑,而同一部分上的其他行不可编辑?
- javascript - 错误:在 React useContext 和 useReducer 中未定义
- meta - 在错误页面上应该使用哪个标点符号?
- leaflet - 传单 OnWheel 活动
- java - 使用 JDBC 在 SQLite 上批量删除非常慢