【问题标题】:Setting different `onclick listener` to each `button` in `recyclerView`为`recyclerView`中的每个`button`设置不同的`onclick listener`
【发布时间】:2016-08-26 07:16:20
【问题描述】:

您好,我正在制作一个包含游戏 (gtav) 作弊码的作弊应用程序,我如何为 recyclerView 中的每个 button 设置不同的 onclicklistener。 例如,我在recyclerView 中有一个名为copytoclip boardbutton,我希望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


    【解决方案1】:
    @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
    
                    }
                });
    
    }
    

    【讨论】:

      【解决方案2】:

      更改为您的适配器类

        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();
          }
          }
      

      【讨论】:

        【解决方案3】:

        在按钮xml中添加onclick事件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 为例

        【讨论】:

        • @TarekZoubi 如注释中所述,侦听器中的代码只是一个示例,您的问题是关于添加侦听器,如果您收到此错误,侦听器正在工作,因此您的原始问题已解决... . 现在如果你现在想解决剪贴板的问题,请检查我链接的答案开始一个新问题并在此处与我分享链接(不要用新问题修改问题,Don't be a chameleon, don't be a vandal跨度>
        • @TarekZoubi from answer I linked 确保您已导入 android.content.ClipboardManagerNOT android.text.ClipboardManager。后者已弃用。
        猜你喜欢
        • 2018-06-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多