-1

Estou aprendendo sobre android agora, e colocando em pratica o que vou aprendendo, porém agora estou com um problema que não consigo resolver, nem encontrar exatamente o que quero

A minha ideia era adicionar um recyclerView na Vertical e dentro dele outro recycler na Horizontal (Ou seja pra cada item na vertical recebe uma nova recyclerview na horizontal)

Desse jeito :

inserir a descrição da imagem aqui

Consegui listar, porém os dois primeiros itens do recyclerView VERTICAL, ficam sem o recyclerView HORIZONTAL, contando apenas apartir do 3º (suponho que seja por que o metodo está sendo chamado apenas quando se cria um novo item, porém não estou conseguindo resolver esse problema)

RecyclerView onde tem e onde não tem

AdapterCatalog

A ideia era chamar o metodo loadSubCategory dentro do OnBindViewHolder pra ele criar dentro cada item do recyclerview Vertical, mas pelo visto não está dando certo

public class AdapterCatalog extends RecyclerView.Adapter<AdapterCatalog.MyViewHolder>{

private Context context;
private List<Category> catalogList;
private AdapterSubCategory adapterSubCategory;
private List<Category> subCategoryList = new ArrayList<>();
private ValueEventListener valueEventListenerSubCategory;
private DatabaseReference subCategoryRef = FirebaseDatabase.getInstance().getReference().child("produtos");

public AdapterCatalog(List<Category> listCatalog, Context c) {
    this.catalogList = listCatalog;
    this.context = c;
}

@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_catalog, parent, false);
    return new MyViewHolder(view);

}

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
    loadSubCategory();
    Category category = catalogList.get( position );
    holder.textCategory.setText(category.getCategory());
    holder.textSubCategory.setText(category.getSubCategory());
    holder.textDescription.setText(category.getDescription());
    Uri uri = Uri.parse (category.getImageCategory());
    Glide.with(context).load(uri).into(holder.imageCategory);


    //Configurar Adapter
    adapterSubCategory = new AdapterSubCategory(subCategoryList, context);

    //Define Layout Categoria
    LinearLayoutManager horizontalLayoutManagaer = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
    holder.recyclerSubCategory.setLayoutManager(horizontalLayoutManagaer);
    holder.recyclerSubCategory.setHasFixedSize(true);
    holder.recyclerSubCategory.setAdapter(adapterSubCategory);
}

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

public class MyViewHolder extends RecyclerView.ViewHolder{

    private TextView textCategory, textSubCategory, textDescription;
    private ImageView imageCategory;
    private RecyclerView recyclerSubCategory;


    public MyViewHolder(@NonNull View itemView) {
        super(itemView);
        textCategory        = itemView.findViewById(R.id.textCategory);
        textSubCategory     = itemView.findViewById(R.id.textSubCategory);
        textDescription     = itemView.findViewById(R.id.textItemDescription);
        imageCategory       = itemView.findViewById(R.id.imageCategory);
        recyclerSubCategory = itemView.findViewById(R.id.recyclerSubCategory);
    }
}

public void loadSubCategory() {
    subCategoryList.clear();
    valueEventListenerSubCategory = subCategoryRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            for (DataSnapshot subCategoryDate : dataSnapshot.getChildren()) {

                Category subCategory = subCategoryDate.getValue(Category.class);
                subCategoryList.add(subCategory);
            }

            adapterSubCategory.notifyDataSetChanged();

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
}

}

AdapterSubCategory

public class AdapterSubCategory extends RecyclerView.Adapter<AdapterSubCategory.MyViewHolder> {

private List<Category> subCategoryList;
private Context context;

public AdapterSubCategory (List<Category> listSubCategory, Context c) {
    this.subCategoryList = listSubCategory;
    this.context = c;

}

@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

    View listSubCategory = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_subcategory, parent, false);
    return new MyViewHolder(listSubCategory);

}

@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {

    Category subCategory = subCategoryList.get( position );
    holder.category = subCategory.getCategory();
    holder.subCategory = subCategory.getSubCategory();
    Uri uri = Uri.parse (subCategory.getImageCategory());
    Glide.with(context).load(uri).into(holder.imageSubCategory);


}

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

public class MyViewHolder extends RecyclerView.ViewHolder{

    private String category, subCategory;
    private ImageView imageSubCategory;


    public MyViewHolder(@NonNull View itemView) {
        super(itemView);
        imageSubCategory = itemView.findViewById(R.id.imageSubCategory);
    }
}

}

CatalogFragment

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_catalog, container, false);

    recyclerCatalog = view.findViewById(R.id.recyclerCatalog);
    recyclerSubCategory = view.findViewById(R.id.recyclerSubCategory);

    //Configurar Adapter
    adapterCatalog = new AdapterCatalog(catalogList, getActivity());

    //Define Layout Categoria
    RecyclerView.LayoutManager layoutManagerMain = new LinearLayoutManager(getActivity());
    recyclerCatalog.setLayoutManager(layoutManagerMain);
    recyclerCatalog.setHasFixedSize(true);
    recyclerCatalog.setAdapter(adapterCatalog);

    //Referencia do DB
    catalogRef = FirebaseDatabase.getInstance().getReference().child("catalogo");

    return view;
}

@Override
public void onStart() {
    super.onStart();
    loadCatalog();
}

@Override
public void onStop() {
    super.onStop();
    catalogRef.removeEventListener(valueEventListenerCatalog);

}

public void loadCatalog() {
    catalogList.clear();
    valueEventListenerCatalog = catalogRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for (DataSnapshot catalogDate : dataSnapshot.getChildren()) {
                Category catalog = catalogDate.getValue(Category.class);
                catalogList.add(catalog);
            }

            adapterCatalog.notifyDataSetChanged();

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

}

}

1 Resposta 1

0

Resolvi meu problema fazendo as seguintes alterações

Obs: Não sei dizer se é a melhor forma de fazer isso, porém depois de tanto sofrer consegui fazer funcionar dessa forma, caso tenham alguma dica sobre agradeço.

no CatalogFragment adicionei a variavel static

public static DatabaseReference subRef = FirebaseDatabase.getInstance().getReference().child("produtos");

e dentro do metodo loadCatalog() chamei o metodo loadRecyclerSub()

 public void loadCatalog() {
    adapterCatalog.loadRecyclerSub();
    valueEventListenerCatalog = catalogRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for (DataSnapshot catalogDate : dataSnapshot.getChildren()) {
                Category catalog = catalogDate.getValue(Category.class);

                catalogList.add(catalog);
            }

            adapterCatalog.notifyDataSetChanged();

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

e dentro de AdapterCatalog mudei a referencia do firebase para o static do Fragment

public void loadRecyclerSub() {
    subList.clear();
    valueEventListenerCatalog = CatalogFragment.subRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for (DataSnapshot subCategoryDate : dataSnapshot.getChildren()) {
                Category subCategory = subCategoryDate.getValue(Category.class);
                subList.add(subCategory);
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

}

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .