Tablayout with reyclerview on android

Asked

Viewed 81 times

0

I have a problem regarding Tablayout and Recyclerview. I have some tabs that are filled in according to the return of a json file. These tabs are the categories of products and according to the user’s click is loaded within the view an Adapter Recycler with the products of that category. In each item of the Recycler view I have a product and each product has an add or remove symbol and I have a textview that according to the click on more or less is incremented or decremented. I need that when selecting products on a tab, when you click on another tab and then go back to the tab where there were already selected products, the same products already selected are there. I’ll put the code I’ve tried so far.

public class Carrinho {

    private String id_produto;
    private String quantidade_produto;
    private String nome_produto;

    public String getNome_produto() {
        return nome_produto;
    }

    public void setNome_produto(String nome_produto) {
        this.nome_produto = nome_produto;
    }

    public String getId_produto() {
        return id_produto;
    }

    public void setId_produto(String id_produto) {
        this.id_produto = id_produto;
    }

    public String getQuantidade_produto() {
        return quantidade_produto;
    }

    public void setQuantidade_produto(String quantidade_produto) {
         this.quantidade_produto = quantidade_produto;
    }


}

public class CarrinhoController {

    private List<Carrinho> carrinhoList = new ArrayList<>();

    public List<Carrinho> getCarrinhoList() {
        return carrinhoList;
    }

    public void setCarrinhoList(List<Carrinho> carrinhoList) {
        this.carrinhoList = carrinhoList;
    }
}

I tried to create this structure within a list and then check within it if there are products that also exist within the list of products of the current tab, and if yes, would put the value of the text view of that product with the amount that comes from the list where the products are added.

  • Hello William, is there any way to post the code for Tablayout and Recyclerview? From what I understand, the problem is that whenever the user clicks on a tab, you will set the recyclerview Adapter again, and this will uncheck all that were already marked. You have to create a mechanism.

  • Hey, Max, thanks for the tip. However, I was able to solve it here using Java 8 files. I do it as follows, I make a comparison between my cart and the products that are in that Adapter. If the product id is equal, then it means that there is inside the cart and then Seto the value of the text view with the quantity that is already inside the cart. I’ll post the code where I do it

  • Stream.of(Cartcontroller.getCarrinhoList()). filter(p -> p.getId_product(). equals(view.getIdProduct()). foreach(c -> products.setQtd_product(String.valueOf(c.getQuantidade_product())));

No answers

Browser other questions tagged

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