简体   繁体   English

按钮单击不起作用,双击工作

[英]Button single click not working, double click works

I'm working with android recyclerview. 我正在使用android recyclerview。 I have a button on each item. 我在每个项目上都有一个按钮。 When I click first time to button, nothing happens but I click double to this button, it works. 当我第一次点击按钮时,没有任何反应,但我单击双按钮,它可以工作。 Bellow, this is my code: 贝娄,这是我的代码:

public class NewsViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

@BindView(R.id.image_news)
public ImageView imageNews;

@BindView(R.id.text_title_news)
public TextView textTitleNews;

@BindView(R.id.text_description_news)
public TextView textDescriptionNews;

@BindView(R.id.text_time)
public TextView textTime;

@BindView(R.id.button_share)
public Button buttonShare;

@BindView(R.id.button_save)
public Button buttonSave;

public NewsViewHolder(View itemView) {
    super(itemView);
    ButterKnife.bind(this, itemView);
    buttonShare.setOnClickListener(this);
    buttonSave.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    if (v.getId() == buttonSave.getId()) {
        Log.e("clicked", "save");
    } else if (v.getId() == buttonShare.getId()) {
        Log.e("clicked", "share");
    }
}

} }

Please help!!! 请帮忙!!!

try out initializing each onclicklistener anonymously as follows: 尝试匿名初始化每个onclicklistener,如下所示:

buttonShare.setOnClickListener(new View.OnClickListener(){
   @Override
   public void onClick(View v) {
       //do something 

   }

});

buttonSave.setOnClickListener(new View.OnClickListener(){
   @Override
   public void onClick(View v) {
       //do something else

   }

});

If you want to avoid this, you can try using a switch case instead of the if statements 如果要避免这种情况,可以尝试使用switch case而不是if语句

Try this code: 试试这段代码:

public class NewsViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

@BindView(R.id.image_news)
public ImageView imageNews;

@BindView(R.id.text_title_news)
public TextView textTitleNews;

@BindView(R.id.text_description_news)
public TextView textDescriptionNews;

@BindView(R.id.text_time)
public TextView textTime;

@BindView(R.id.button_share)
public Button buttonShare;

@BindView(R.id.button_save)
public Button buttonSave;

public NewsViewHolder(View itemView) {
    super(itemView);
    ButterKnife.bind(this, itemView);
    buttonShare.setOnClickListener(this);
    buttonSave.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.button_share:
            Log.e("clicked", "share");
            break;
        case R.id.button_save:
            Log.e("clicked", "saved");
            break;
     }
}

Since you are using ButterKnife I would recommend you to use ButterKnife annotation for performing click actions on button 由于您使用的是ButterKnife,我建议您使用ButterKnife注释来执行按钮上的单击操作

@OnClick({R.id.button_share, R.id.button_save})
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.button_share:
            // Do something
            break;
        case R.id.button_save:
            // Do something
            break;
    }
}

And use switch instead of if, for better readability. 并使用开关而不是if,以获得更好的可读性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM