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));
}
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– Valdeir Psr
Looking only at this section, the moreButton should run with one click only.
– Valdeir Psr
Pse, it doesn’t work at all, I made my own recyclerView and tb n worked, just c two clicks
– Woton Sampaio
On the site where the code is when the guy clicks the function is called, but here it just doesn’t work
– Woton Sampaio
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
– Woton Sampaio
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.
– Isac