Minnie
Minnie

Reputation: 67

Disable buttons in recycler view after clicking

I have a recycler view, which consists of edittext and buttons for each view. I would like to have functionality in such a way that if I enter some text or attach an image for a view, then some of the buttons' functionality needs to be disabled in that view as soon as I click the button.

With the below code, I can attach images when ProofClick function is called via the interface, and then I set the button as disabled for the same position, but the button is not disabled, and I can attach images to the same view twice, which I do not want.

Adapter class:

  // Check if the button is clicked and disable
  private boolean clicked = false;

  public InsStepsContentAdapter(List<InspectonScroll> scrollList, 
      ClickOnRecycle clickOnRecycle, String[] options) {
        this.scrollList = scrollList;
        this.clickOnRecycle = clickOnRecycle;
        this.options = options;

    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_recycler_steps, parent, false));
    }


    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.setIsRecyclable(false);
        holder.progressbar.setMax(scrollList.size()); 
        holder.title.setText(scrollList.get(position).getSteps().get(position).getTitle());
      
   
       holder.btnAttach.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(clicked){
                    clicked = true;
                    clickOnRecycle.ProofClick(holder.getAdapterPosition());
                    holder.btnAttach.setEnabled(true);

                } else{
                    holder.btnAttach.setEnabled(false);
                }

            }
        });
}

      

   public int getItemCount() {
        return scrollList.size();
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    public void updateList(List<ScrollList> scrollList){
        this.scrollList = scrollList;
        notifyDataSetChanged();
    }

Model class:

public class InspectonScroll {
 
    private List<Steps> Steps = new ArrayList<>();
    
     public List<Steps> getSteps() {
        return steps;
    }

    public void setSteps(List<Steps> steps) {
        this.steps = steps;
    }


}

Upvotes: 0

Views: 295

Answers (1)

kelvin
kelvin

Reputation: 1538

ButtonAdapter

public class ButtonAdapter extends RecyclerView.Adapter<ButtonAdapter.ViewHolder> {
    Activity activity;
    ArrayList<ButtonModel> arrayList;
    public ButtonAdapter(Activity activity, ArrayList<ButtonModel> arrayList) {
        this.activity = activity;
        this.arrayList = arrayList;
    }

    @NonNull
    @Override
    public ButtonAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(activity.getLayoutInflater().inflate(R.layout.row_item,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull ButtonAdapter.ViewHolder holder, int position) {

        holder.button.setText("item"+position);
        ButtonModel item = arrayList.get(position);
        holder.button.setEnabled(item.isEnabled);
        holder.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(item.isEnabled){
                    item.isEnabled = false;
                    notifyDataSetChanged();
                }

                Toast.makeText(activity, "Clicked", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public int getItemCount() {
        return arrayList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        Button button;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            button = itemView.findViewById(R.id.button);
        }
    }
}

ButtonModel

public class ButtonModel {
    boolean isEnabled = true;
}

one thing can be done here is maintaining the state of the button in the model as shown in the example and by using that state we can enable or disable the button in the onBindViewHolder method .

Upvotes: 1

Related Questions