您好我制作了一个有游戏作弊码的作弊应用程序(gtav),如何为onclicklistener
中的每个button
设置不同的recyclerView
。
例如,我在button
中有recyclerView
名为 copytoclip board ,我希望button
中的每个recyclerView
都能复制不同的文字。
我怎么能这样做?
recycler_view_list
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:clickable="true"
android:background="?android:attr/selectableItemBackground"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:textSize="16dp"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/cheat"
android:layout_below="@id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/description"
android:layout_below="@+id/cheat"
android:layout_gravity="bottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Copy"
android:id="@+id/cpytocp"
android:layout_gravity="top|right" />
</LinearLayout>
列表
public class Cheats {
private String title, cheat, description;
public Cheats(){
}
public Cheats( String title, String cheat, String description){
this.title = title;
this.cheat = cheat;
this.description = description;
}
public String getTitle(){
return title;
}
public void setTitle(String name){
this.title = name;
}
public String getCheat(){
return cheat;
}
public void setCheat(String cheat){
this.cheat = cheat;
}
public String getDescription(){
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
适配器
`public class CheatsAdapter extends` `RecyclerView.Adapter`<CheatsAdapter.MyViewHolder> `{`
private List<Cheats> cheatList;
public class MyViewHolder extends RecyclerView.ViewHolder{
public TextView title, cheat, description;
public MyViewHolder(View view){
super(view);
title = (TextView)view.findViewById(R.id.title);
cheat = (TextView) view.findViewById(R.id.cheat);
description = (TextView) view.findViewById(R.id.description);
}
}
public CheatsAdapter(List<Cheats> cheatList){
this.cheatList = cheatList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.cheat_list, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Cheats cheats = cheatList.get(position);
holder.title.setText(cheats.getTitle());
holder.cheat.setText(cheats.getCheat());
holder.description.setText(cheats.getDescription());
}
@Override
public int getItemCount(){
return cheatList.size();
}
}
答案 0 :(得分:0)
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Cheats cheats = cheatList.get(position);
holder.title.setText(cheats.getTitle());
holder.cheat.setText(cheats.getCheat());
holder.description.setText(cheats.getDescription());
holder.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// TO DO
// if this happens to all the buttons..insert here a switch recognizing which button is pressed through getting an info of the button maybe
}
});
}
答案 1 :(得分:0)
更改为适配器类
public class CheatsAdapter extends` `RecyclerView.Adapter`<CheatsAdapter.MyViewHolder> `{`
private List<Cheats> cheatList;
public class MyViewHolder extends RecyclerView.ViewHolder{
public TextView title, cheat, description;
public Button btnClick;
public MyViewHolder(View view){
super(view);
btnClick= (Button )view.findViewById(R.id.cpytocp);
title = (TextView)view.findViewById(R.id.title);
cheat = (TextView) view.findViewById(R.id.cheat);
description = (TextView) view.findViewById(R.id.description);
}
}
public CheatsAdapter(List<Cheats> cheatList){
this.cheatList = cheatList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.cheat_list, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Cheats cheats = cheatList.get(position);
btnClick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// do that you want to perform on click
}
}); holder.title.setText(cheats.getTitle());
holder.cheat.setText(cheats.getCheat());
holder.description.setText(cheats.getDescription());
}
@Override
public int getItemCount(){
return cheatList.size();
}
}
答案 2 :(得分:0)
在按钮xml android:onClick="doSomething"
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Copy"
android:id="@+id/cpytocp"
android:layout_gravity="top|right"
android:onClick="copyToClipboard" />
然后,您需要在您的activity / fragment中创建引用的方法:
public void copyToClipboard(View v) {
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText(label, text);
clipboard.setPrimaryClip(clip);
}
注意:
switch(v.getId())
以了解所点击的元素copyToClipboard
代码以here