微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在适配器的多个TextViews中显示字符串

如何解决在适配器的多个TextViews中显示字符串

“我的应用”当前在单个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,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>

                

Contex Error

App picture after working code

解决方法

尝试以下操作:

在您的布局内,为要添加的TextView添加一个容器:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linear"... >

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

    <TextView ... />

    <LinearLayout android:id="@+id/optionsContainer"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:background="@drawable/bg_bubble_watbot"
                  android:orientation="vertical" />

    <ImageView .../>

</LinearLayout>

然后,更新您的ViewHolder以引用该容器:

public class ViewHolder extends RecyclerView.ViewHolder {
    ...
    LinearLayout optionsContainer;

    public ViewHolder(View view) {
        super(view);
        ...
        optionsContainer = (LinearLayout) itemView.findViewById(R.id.optionsContainer);
    }
}

然后,在您的OPTION情况下:

    TextView tv = ((ViewHolder) holder).message;
    tv.setVisibility(View.GONE));
    LinearLayout optionsContainer = ((ViewHolder) holder).optionsContainer;
    TextView messageTextView = createTextView(message.getMessage(),optionsContainer.getContext());
    for ( DialogNodeOutputOptionsElement r : message.getOptions() ) {
        // you should check if r.getLabel() really returns a HTML string
        // if not,you will have to enclose it with html tags to make it clickable later
        String option = r.getLabel();
        TextView optionTextView = createTextView(option,optionsContainer.getContext());
        // add the created textView to our container
        optionsContainer.addView(optionTextView);
    }

在您的适配器内实现createTextView()功能:

private TextView createTextView(String text,Context context) {
    TextView tv = new TextView(context);
    LinearLayout.LayoutParams params=new LinearLayout.LayoutParams
            ((int) LinearLayout.LayoutParams.WRAP_CONTENT,(int) LinearLayout.LayoutParams.WRAP_CONTENT);
    tv.setLayoutParams(params);
    tv.setTextSize((float) 15);
    tv.setText(text);
    int blueColor = Color.parseColor("#0000ff");
    // make text blue
    tv.setTextColor(blueColor);
    // make text underline
    tv.setPaintFlags(tv.getPaintFlags()| Paint.UNDERLINE_TEXT_FLAG);
    tv.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(context,"Link clicked",Toast.LENGTH_SHORT).show();
            // add here what the click should do
        }
    });
    return tv;
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。