在RecyclerView中为Every CardView添加复制按钮

时间:2018-02-14 15:55:47

标签: android firebase firebase-realtime-database

如何实施Coppy按钮 我想要一个RecyclerView中每张卡的复制按钮。单击该按钮时,将复制TextView,但我对将OnClick或如何获取字符串放在哪里感到困惑。

import android.content.ClipData;
import android.content.ClipboardManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.FirebaseApp;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class SadStatus extends AppCompatActivity {
    private View frag1View;

    private RecyclerView recyclerView;
    private DatabaseReference myref;

    private ClipboardManager clipboardManager;
    private ClipData clipData;

    String text;

    ImageButton imageButton, imageButton2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_attitude);


        FirebaseApp.initializeApp(this);

        recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
        recyclerView.setHasFixedSize(true);

        myref = FirebaseDatabase.getInstance().getReference().child("SadStatus");

        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        clipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);

        imageButton = (ImageButton) findViewById(R.id.shareButton);
        imageButton2 = (ImageButton) findViewById(R.id.coppyButton);
    }


    @Override
    public void onStart() {
        super.onStart();
        FirebaseRecyclerAdapter<Blog, AttitudeActivity.BlogViewHolder> recyclerAdapter = new FirebaseRecyclerAdapter<Blog, AttitudeActivity.BlogViewHolder>(
                Blog.class,
                R.layout.individual_row,
                AttitudeActivity.BlogViewHolder.class,
                myref
        ) {
            @Override

            //populateViewHolder metod sets all Element which Our Main Componets Contain.

            protected void populateViewHolder(AttitudeActivity.BlogViewHolder viewHolder, Blog model, int position) {

                //it gets tittle frm model class and setting it to the viewHolder, same as below
                viewHolder.setTitle(model.getTitle());




            }
        };

        //here Adapter is Setting the recyclerView
        recyclerView.setAdapter(recyclerAdapter);
    }

    //this Is the BlogViewHolder
    public static class BlogViewHolder extends RecyclerView.ViewHolder {

        View mView;
        //here Reference to all the Elements it Contain

        String text;

        private ClipboardManager clipboardManager;
        private ClipData clipData;

        TextView textView_title;
        ImageButton imageButton, imageButton2;

        public BlogViewHolder(final View itemView) {

            super(itemView);

            mView = itemView;

            textView_title = (TextView) itemView.findViewById(R.id.title);
            imageButton = (ImageButton) itemView.findViewById(R.id.shareButton);
            imageButton2 = (ImageButton) itemView.findViewById(R.id.coppyButton);

            textView_title.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    String text = textView_title.getText().toString().trim();

                    clipData = ClipData.newPlainText("text",text);
                    clipboardManager.setPrimaryClip(clipData);
                }
            });


        }


        public void setTitle(String title) {
            textView_title.setText(title);


        }
    }
}

1 个答案:

答案 0 :(得分:0)

您可以在populateViewHolder方法内为TextView添加onClick侦听器。

protected void populateViewHolder(//your arguments) {

   viewHolder.textView_title.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
         //your code
           }
        });
   }