-1
I’m learning about android now, and putting into practice what I’m learning, but now I have a problem that I can not solve, or find exactly what I want
My idea was to add a recyclerView in Vertical and inside it another Horizontal Recycler (That is, for each vertical item you receive a new horizontal recyclerview)
That way :
I managed to list, but the first two items of recyclerView VERTICAL, are without recyclerView HORIZONTAL, counting only from the 3rd (I suppose that is why the method is being called only when creating a new item, but I am not able to solve this problem)
Adaptercatalog
The idea was to call the loadSubCategory method within Onbindviewholder to create within each item of recyclerview Vertical, but apparently it is not working
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) {
}
});
}
}