Amay Diam
Amay Diam

Reputation: 2591

Handling button click in RecyclerView adapter?

I have a problem with my button in adapter RecyclerView. When I click on it, sometimes the action is not directly executed. I must scroll a bit of RecyclerView, so that the action is directly executed. This my adapter:

public class ViewHolder extends RecyclerView.ViewHolder {

    public Button btn_action;

    public ViewHolder(View vi) {
        super(vi);
        btn_action = (Button) vi.findViewById(R.id.btn_action);           
    }

    @Override
    public NewsRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.pin_item_news_recyclerview, parent, false);
        ViewHolder vh = new ViewHolder(v);
        return vh;
    }

    public void onBindViewHolder(final ViewHolder holder, int position) {

        holder.btn_action.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activity.TakePhoto();
            }
        });
    }
}

And this is the action of TakePhoto() in the activity.

public void TakePhoto() {
    if (isIntentAvailable(getActivity(), MediaStore.ACTION_IMAGE_CAPTURE)) {
        dispatchTakePictureIntent(ACTION_TAKE_PICTURE_BIG);
    } else {
        messageRespone("Maaf, tidak bisa memanggil Camera.");
    }
}

So how do I fix it? Thanks.

Upvotes: 2

Views: 12076

Answers (3)

AdamMc331
AdamMc331

Reputation: 16691

You should make your ViewHolder itself implement the on click interface, and then set the listener to the button like this:

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

   public ViewHolder(View view) {
      super(view);
      button = (Button) view.findViewById(R.id.button);
      button.setOnClickListener(this);
   }

   @Override
   public void onClick(View view) {
      Toast.makeText(context, "Clicked button at position: " + getAdapterPosition(), Toast.LENGTH_SHORT).show();
   }
}

Upvotes: 4

Samuel Moshie
Samuel Moshie

Reputation: 570

I solve with this and it works fine just in case u need to handle multiple buttons

public class ViewHolder extends RecyclerView.ViewHolder {

   public ViewHolder(View view){
      super(view);
      button = (Button) view.findViewById(R.id.button);
      button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(view.getContext(), "Clicked button at position: " + getAdapterPosition(), Toast.LENGTH_SHORT).show();
                }
            });
   }

}

Upvotes: 0

challenger
challenger

Reputation: 2214

To solve your problem.. set the OnClickListner in the oncreateViewHolder Method..

Use this in the bind method just to avoid creating big number of listener objects while u dont need them btw..U don'T EVEN NEED IT any more in the bind method!

OnBindViewHoler(){
   if(holder.btn_action.getOnClickListener()==null)
      holder.btn_action.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            activity.TakePhoto();
        }
    });}

Upvotes: 0

Related Questions