【发布时间】:2016-08-26 07:16:20
【问题描述】:
您好,我正在制作一个包含游戏 (gtav) 作弊码的作弊应用程序,我如何为 recyclerView 中的每个 button 设置不同的 onclicklistener。
例如,我在recyclerView 中有一个名为copytoclip board 的button,我希望recyclerView 中的每个button 复制不同的文本。
我该怎么做?
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();
}
}
【问题讨论】:
标签: java android android-recyclerview onclicklistener copy-paste