如何使用弹出窗口中的按钮从recyclerview项复制文本?

zvokhttg  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(351)

我有一个 RecyclerViewTextView 我想要实现的是做一个类似的复制按钮,比如whatsapp,telegram,当你点击一条消息时会弹出一个窗口。我通过使用 PopupMenu 但与 PopupWindow 我不能触发 onClick 弹出窗口中“复制”按钮的功能。
recyclerview和te popupwindow是什么样子的
这是我的适配器viewholder的外观:

public class MessageViewHolder extends RecyclerView.ViewHolder {
    TextView noteView;

    public MessageViewHolder(View itemView) {
        super(itemView);
        noteView = (TextView)itemView.findViewById(R.id.message_body);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.add_image_popup_window, null);
        final TextView copyMessageText = (TextView) view.findViewById(R.id.copy_message);

        noteView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) { // This part works fine.
                PopupWindow popupwindow = lib.popupDisplay(context);
                Drawable background = ContextCompat.getDrawable(activity, R.drawable.add_image_popup_rounded);
                popupwindow.setBackgroundDrawable(background);
                popupwindow.showAsDropDown(noteView, 0, 0);

                copyMessageText.setOnClickListener(new View.OnClickListener()
                {// This part won't get triggered when i click/touch on te "Copy" button inside the popup window.
                    @Override
                    public void onClick(View v){ 
                        ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
                        ClipData clip = ClipData.newPlainText("MessageText", noteView.getText().toString());
                        clipboard.setPrimaryClip(clip);
                        Toast.makeText(activity, "Text copied to clipboard", Toast.LENGTH_SHORT).show();
                    }
                });

            }
        });
    }
}

这是我的弹出窗口布局 add_image_popup_window :

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent"
    >

    <TextView
        android:id="@+id/copy_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:minWidth="60dp"
        android:text="@string/message_copy"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:padding="10dp"
        android:clickable="true"
        android:textColor="@color/white"
        android:background="@drawable/copy_message_selector" />

</androidx.constraintlayout.widget.ConstraintLayout>

如何解决此问题?

iqxoj9l9

iqxoj9l91#

调用noteview.setonclicklistener()有点太早了。你需要把它称为你的recyclerview.adapter类onbindview。

相关问题