簡體   English   中英

在適配器的多個 TextView 中顯示字符串

[英]Show a string in multiple TextViews in adapter

我的應用程序當前在單個 TextView 中顯示 4 個選項(我想使其可點擊)。 目前,它看起來像這樣(即最后一條消息氣泡):應用圖片

而不是上面的,我希望將這些選項作為 4 個單獨的 TextViews,就像在這個例子中一樣。 我研究了多種解決方案,但沒有一個對我有用,因為我使用的是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>

                

上下文錯誤

工作代碼后的應用圖片

嘗試以下操作:

在您的布局中,為您要添加的TextView s 添加一個容器:

<?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() function:

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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM