Creating Fragments with RecyclerView: Change with button click?

25 Views Asked by At

I'm creating a recipe application where on one activity, the user should be able to click either a "breakfast", "lunch", or "dinner" button, and a different fragment will be displayed with recipes with the keyword clicked. I hope that makes sense. I already created the RecyclerView to show random recipes (from spoonacular API) on the home screen, and it's working well. However, I can't find a fragment example online that I feel fits my situation where I can pull ideas from. Fragments are new to me, and I've read all the documentation on Android, but I'm at a complete standstill. Can someone explain to me in simple English how to format this fragment in Java? Are all the initially generated methods required in each fragment's java code?

For example, I want to just copy the code from my main activity since it's working, but I can't call the fragment by adding "fragment.this" as a parameter for anything to get context for the fragment..

BTW, I'm not having trouble switching between (empty)fragments, just populating the recyclerview inside of each fragment.

The Github Link, because it's entirely too much to directly post. https://github.com/toriliford/Liford_RecipeApp/tree/master

MainActivity:

    package com.example.liford_recipeapp;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.example.liford_recipeapp.Adapters.RandomRecipeAdapter;
import com.example.liford_recipeapp.Listeners.RandomRecipeResponseListener;
import com.example.liford_recipeapp.Models.RandomRecipeApiGenerator;

public class MainActivity extends AppCompatActivity {

    Button categoryBtn;
    Button keywordBtn;
    Button savedRecipeBtn;

    ProgressDialog dialog;
    RequestManager manager;
    RandomRecipeAdapter randomRecipeAdapter;
    RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        categoryBtn = findViewById(R.id.categoryBtn);
        keywordBtn = findViewById(R.id.keywordBtn);
        savedRecipeBtn = findViewById(R.id.savedRecipesBtn);

        //create intents for each button
        Intent categoryIntent = new Intent(this, CategoryChoice.class);
        Intent keywordIntent = new Intent(this, KeywordChoice.class);
        Intent savedRecipeIntent = new Intent(this, SavedRecipeChoice.class);

        //on-click listener for category choice
        categoryBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(categoryIntent);
            }
        });

        //on-click listener for keyword choice
        keywordBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(keywordIntent);
            }
        });

        //on-click listener for saved recipes choice
        savedRecipeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(savedRecipeIntent);
            }
        });


        dialog = new ProgressDialog(this);
        dialog.setTitle("Loading...");

        manager = new RequestManager(this);
        manager.getRandomRecipes(randomRecipeResponseListener);
        dialog.show();
    }

    private final RandomRecipeResponseListener randomRecipeResponseListener = new RandomRecipeResponseListener() {
        @Override
        public void didFetch(RandomRecipeApiGenerator response, String message) {
            //dismiss dialog when fetched
            dialog.dismiss();
            recyclerView = findViewById(R.id.homeRecyclerView);
            recyclerView.setHasFixedSize(true);
            recyclerView.setLayoutManager(new GridLayoutManager(MainActivity.this, 1));
            randomRecipeAdapter = new RandomRecipeAdapter(MainActivity.this, response.recipes);
            recyclerView.setAdapter(randomRecipeAdapter);
        }

        @Override
        public void didError(String message) {
            Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT);
        }
    };
}
  
0

There are 0 best solutions below