【发布时间】:2020-08-20 11:18:49
【问题描述】:
我的应用程序当前在单个 TextView 中显示 4 个选项(我想让它们可点击)。目前,它看起来像这样(即最后一条消息气泡):app picture
我希望将这些选项设置为 4 个单独的 TextView,而不是上面的,例如 in this example。我研究了多种解决方案,但没有一个对我有用,因为我使用的是RecyclerView.Adapter。以下是相关部分:
case OPTION:
String option = "";
option = message.getMessage();
for ( DialogNodeOutputOptionsElement r : message.getOptions() ){
option += r.getLabel()+"<br/>";
((ViewHolder) holder).message.setText(Html.fromHtml(option));
// new TextView for next Option
整个 ChatAdapter 如下所示:
public class ChatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
protected Activity activity;
private int SELF = 100;
private ArrayList<Message> messageArrayList;
public ChatAdapter(ArrayList<Message> messageArrayList) {
this.messageArrayList = messageArrayList;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView;
// view type is to identify where to render the chat message
// left or right
if (viewType == SELF) {
// self message
itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.chat_item_self, parent, false);
} else {
// WatBot message
itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.chat_item_watson, parent, false);
}
return new ViewHolder(itemView);
}
@Override
public int getItemViewType(int position) {
Message message = messageArrayList.get(position);
if (message.getId() != null && message.getId().equals("1")) {
return SELF;
}
return position;
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
Message message = messageArrayList.get(position);
switch (message.type) {
case TEXT:
((ViewHolder) holder).message.setText(Html.fromHtml(message.getMessage()+"<br/>"));
break;
case IMAGE:
((ViewHolder) holder).message.setVisibility(View.GONE);
ImageView iv = ((ViewHolder) holder).image;
Glide
.with(iv.getContext())
.load(message.getUrl())
.into(iv);
break;
case OPTION:
String option = "";
option = message.getMessage();
for ( DialogNodeOutputOptionsElement r : message.getOptions() ){
option += r.getLabel()+"<br/>";
((ViewHolder) holder).message.setText(Html.fromHtml(option));
// new TextView for next Option
}
break;
case PAUSE:break;
}
}
@Override
public int getItemCount() {
return messageArrayList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView message;
ImageView image;
public ViewHolder(View view) {
super(view);
message = (TextView) itemView.findViewById(R.id.message);
image = (ImageView) itemView.findViewById(R.id.image);
//TODO: Uncomment this if you want to use a custom Font
}
}
}
XML 看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<android.support.v7.widget.AppCompatImageView
android:layout_width="54dp"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:src="@mipmap/new_face" />
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="10dp"
android:autoLink="web"
android:background="@drawable/bg_bubble_watbot"
android:fontFamily="sans-serif"
android:textColor="@android:color/black"
android:textIsSelectable="true"
android:textSize="14sp" />
<ImageView
android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
【问题讨论】:
-
能否请您添加与您的
ViewHolder对应的项目的xml布局?理想情况下,添加Adapter和ViewHolder的代码。 -
@DatPhamTat 添加了它:)
标签: java android android-studio watson-assistant