当我想在 recyclerview 中获取文本并保存在剪贴板中时,这是我的代码和一些错误。我的错误:
holder.btnCopy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("simple text", containerTextView.getText());
clipboard.setPrimaryClip(clip);
Toast.makeText(getApplicationContext(), "some",
Toast.LENGTH_LONG).show();
}
});
这是适配器代码:
public class LinearCardListAdapter extends RecyclerView.Adapter<LinearCardListAdapter.MyViewHolder>{
private Context context;
private List<TravelCard> travelCards;
private RecyclerView recyclerView;
public class MyViewHolder extends RecyclerView.ViewHolder {
public ImageView containerImageView;
public TextView containerTextView;
public LinearLayout textLayout;
public int myHeight;
public Button deleteItem;
public TextViewEx subtitleView;
public Button btnCopy;
public MyViewHolder(View itemView) {
super(itemView);
textLayout = (LinearLayout) itemView.findViewById(R.id.text_layout);
containerImageView = (ImageView) itemView.findViewById(R.id.image_container);
btnCopy = (Button) itemView.findViewById(R.id.btn_copy);
containerTextView = (TextView) itemView.findViewById(R.id.text_container);
subtitleView = (TextViewEx) itemView.findViewById(R.id.subtitle_view);
deleteItem = (Button) itemView.findViewById(R.id.btn_delete);
myHeight = containerImageView.getHeight();
}
}
public LinearCardListAdapter(Context context ,List<TravelCard> travelCards , RecyclerView recyclerView){
this.context = context;
this.recyclerView = recyclerView;
this.travelCards = travelCards;
}
@Override
public LinearCardListAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View myView = LayoutInflater.from(parent.getContext()).inflate(R.layout.linear_list_card_item, parent, false);
MyViewHolder myViewHolder= new MyViewHolder(myView);
return myViewHolder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
final int newPos = position;
final MyViewHolder myViewHolder = holder;
Glide.with(context).load(context.getResources().getIdentifier(context.getPackageName() + ":drawable/" + travelCards.get(position).getImagePath(),null,null)).into(holder.containerImageView);
holder.subtitleView.setText(travelCards.get(position).getSubtitle(), true);
holder.containerTextView.setText(travelCards.get(position).getTitle());
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
Rect scrollBounds = new Rect();
recyclerView.getHitRect(scrollBounds);
if (myViewHolder.containerImageView.getLocalVisibleRect(scrollBounds)) {
Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics();
display.getMetrics(outMetrics);
float density = context.getResources().getDisplayMetrics().density;
float dpHeight = outMetrics.heightPixels / density;
int screen_height_pixels = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpHeight, context.getResources().getDisplayMetrics());
int half_screen_height = screen_height_pixels / 2;
int container_height_pixels = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200, context.getResources().getDisplayMetrics());
int center = half_screen_height - (container_height_pixels / 2);
int[] loc_screen = {0, 0};
myViewHolder.containerImageView.getLocationOnScreen(loc_screen);
int final_loc = ((loc_screen[1] - center) * 100) / half_screen_height;
myViewHolder.containerImageView.setTranslationY(-final_loc * 0.6f * density - 160);
}
}
});
holder.btnCopy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("simple text", containerTextView.getText());
clipboard.setPrimaryClip(clip);
Toast.makeText(getApplicationContext(), "متن در حافظه موقت دستگاه ذخیره شد",
Toast.LENGTH_LONG).show();
}
});
holder.deleteItem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
travelCards.remove(newPos);
notifyItemRemoved(newPos);
notifyItemRangeChanged(newPos, travelCards.size());
}
});
}
@Override
public int getItemCount() {
return travelCards.size();
}
}