Search in sources :

Example 1 with CategoryFragmentAdapter

use of com.example.ezmeal.MyRecipes.RecipeAdapters.CategoryFragmentAdapter in project EZMeal by Jake-Sokol2.

the class CategoryFragment method onCreateView.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.activity_find_recipes_specific_recipe, container, false);
    bottomNav = (BottomNavigationView) getActivity().findViewById(R.id.bottomNavigationView);
    txtTitle = (TextView) view.findViewById(R.id.txtmyRecipesTitle);
    String categoryName = null;
    Bundle extras = getArguments();
    if (extras != null) {
        // retrieve category name from the Intent
        categoryName = extras.getString("category");
        txtTitle.setText(categoryName);
    }
    rvGroupList = (RecyclerView) view.findViewById(R.id.rvMyRecipes);
    adapter = new CategoryFragmentAdapter(categoryFragmentModel.getRecipeList(), categoryFragmentModel.getUrlList());
    rvGroupList.setAdapter(adapter);
    StaggeredGridLayoutManager staggeredManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
    // GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 3, GridLayoutManager.VERTICAL, false);
    /*
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup()
        {
            @Override
            public int getSpanSize(int position)
            {
                if (position == 6)
                {
                    return 3;
                }
                else
                {
                    return 1;
                }
            }
        });*/
    rvGroupList.setLayoutManager(staggeredManager);
    EZMealDatabase sqlDb = Room.databaseBuilder(getActivity().getApplicationContext(), EZMealDatabase.class, "user").allowMainThreadQueries().fallbackToDestructiveMigration().build();
    // get recipeId, pathToImage, and title for recipes of a specific category
    List<recipePathTitle> items = sqlDb.testDao().getCategoryRecipes(categoryName);
    recipeId = new ArrayList<String>();
    for (int i = 0; i < items.size(); i++) {
        categoryFragmentModel.addItem(items.get(i).getTitle(), items.get(i).getPathToImage());
        // keep list of recipeId's in memory so we know which recipeId to pass to the next fragment when the user clicks on a recipe
        recipeId.add(items.get(i).getRecipeId());
    }
    adapter.notifyDataSetChanged();
    adapter.setOnItemClickListener(new CategoryFragmentAdapter.MainAdapterListener() {

        @Override
        public void onItemClick(int position, CardView cardView) {
            Bundle bundle = new Bundle();
            // pass recipeId to specific recipe page so that it knows which recipe to use
            bundle.putString("id", recipeId.get(position));
            NavController navController = Navigation.findNavController(view);
            navController.navigate(R.id.action_myRecipesSpecificCategoryFragment_to_myRecipesSpecificRecipeFragment, bundle, new NavOptions.Builder().setEnterAnim(R.anim.slide_in).setExitAnim(R.anim.stall).setPopExitAnim(R.anim.slide_out).build());
        }
    });
    return view;
}
Also used : CategoryFragmentAdapter(com.example.ezmeal.MyRecipes.RecipeAdapters.CategoryFragmentAdapter) EZMealDatabase(com.example.ezmeal.roomDatabase.EZMealDatabase) Bundle(android.os.Bundle) CardView(androidx.cardview.widget.CardView) NavController(androidx.navigation.NavController) StaggeredGridLayoutManager(androidx.recyclerview.widget.StaggeredGridLayoutManager) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) BottomNavigationView(com.google.android.material.bottomnavigation.BottomNavigationView) TextView(android.widget.TextView) View(android.view.View) RecyclerView(androidx.recyclerview.widget.RecyclerView) CardView(androidx.cardview.widget.CardView) com.example.ezmeal.roomDatabase.recipePathTitle(com.example.ezmeal.roomDatabase.recipePathTitle) RecyclerView(androidx.recyclerview.widget.RecyclerView)

Aggregations

Bundle (android.os.Bundle)1 View (android.view.View)1 TextView (android.widget.TextView)1 CardView (androidx.cardview.widget.CardView)1 NavController (androidx.navigation.NavController)1 LinearLayoutManager (androidx.recyclerview.widget.LinearLayoutManager)1 RecyclerView (androidx.recyclerview.widget.RecyclerView)1 StaggeredGridLayoutManager (androidx.recyclerview.widget.StaggeredGridLayoutManager)1 CategoryFragmentAdapter (com.example.ezmeal.MyRecipes.RecipeAdapters.CategoryFragmentAdapter)1 EZMealDatabase (com.example.ezmeal.roomDatabase.EZMealDatabase)1 com.example.ezmeal.roomDatabase.recipePathTitle (com.example.ezmeal.roomDatabase.recipePathTitle)1 BottomNavigationView (com.google.android.material.bottomnavigation.BottomNavigationView)1