How the.setOnClickListener(view -> method() item works)

Asked

Viewed 653 times

0

How does this item.setOnClickListener(view -> method());? I’ve never seen that kind of call, I saw it today and I was curious about that view, where does it come from? How does this type of call work?

@Override
    public void onBindViewHolder(LineHolder holder, int position) {
        holder.title.setText(String.format(Locale.getDefault(), "%s, %d - %s",
                mUsers.get(position).getName(),
                mUsers.get(position).getAge(),
                mUsers.get(position).getCity()
        ));

        holder.moreButton.setOnClickListener(view -> updateItem(position));
        holder.deleteButton.setOnClickListener(view -> removerItem(position));
    }
  • 2

    The name of that expression is lambda. It is widely used with anonymous classes (both in Java 8+, and in Javascript ES6 (Arrow functions)), the advantage of which is to reduce code and make it cleaner. https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html

  • Looking only at this section, the moreButton should run with one click only.

  • Pse, it doesn’t work at all, I made my own recyclerView and tb n worked, just c two clicks

  • On the site where the code is when the guy clicks the function is called, but here it just doesn’t work

  • Ah, forget the double click part, I solved here, it was my mistake, I had put the click event in recyclerView, so his action was ahead, that was the mistake

  • 1

    If the double click part was another unrelated problem then update the question by removing it, to prevent people from trying to solve a problem that does not exist.

Show 1 more comment

2 answers

2


The syntax in question is a lambda, syntax introduced in Java 8, explained in detail in this answer.

Write this in Java 8:

holder.moreButton.setOnClickListener(view -> updateItem(position));

It’s the equivalent of what was written like this in Java 7:

holder.moreButton.setOnClickListener(new View.OnClickListener() {
    @Override public void onClick(View v) {
        updateItem(position);
    }
});

0

Structured Viewholder class

/**
 * A classe ViewHolder internamente guarda a view
 */
public class LineHolder extends RecyclerView.ViewHolder {
    TextView title;
    Button moreButton,deleteButton;

    /**
     * contrutor onde a View é adicionada
     * E inflado os demais elementos da view
     * @param itemView
     */
    public LineHolder(View itemView) {
        super(itemView);

        title =  itemView.findViewById(R.id.title);
        morebutton =  itemView.findViewById(R.id.moreButton);
        deleteButton =  itemView.findViewById(R.id.deleteButton);

    }
}

Where the Holder is created and inserted its view

/**
 * Aqui que a View do Holder é inflada
 */
public LineHolder onCreateViewHolder(ViewGroup parent, int type) {
    return new LineHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_line_holder, parent, false));
}

Then you update the Holder

/**
 * Como os Holders são views reaproveitadas, é necessario fazer sua atualização quando ele pe chamado, e este metodo é para fazer a atualização dos recursos da view
 * @param holder
 * @param position
 */
public void onBindViewHolder(LineHolder holder, int position) {
    holder.title.setText("some text");

    //chamda da função lambda: necessario java 8 para executar ela
    holder.moreButton.setOnClickListener(view -> updateItem(position));

    //chamada padrão
    holder.moreButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            updateItem(position);
        }
    });

    holder.deleteButton.setOnClickListener(view -> removerItem(position));
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.