IanTDuncan / MealTime

Project for CSC 480
0 stars 0 forks source link

XML Update - TabLayout and ViewPager Addition #180

Closed BeepDroid closed 2 months ago

BeepDroid commented 3 months ago

XML Update - TabLayout and ViewPager Addition


Description:

Adding some widgets to our Meal Result page layout by the name of TabLayout and ViewPager, ViewPager wraps around TabLayout to allow users to swipe between different options. I'm adding this to make transition from the result page to the corresponding shopping list a more easy process for the user!

Steps to Reproduce:


  1. Add the TabLayout to the appropriate XML page first, ensuring that it shows up and there is enough room on the page for it.
  2. Add the ViewPager widget right after, this will wrap around the TabLayout. You won't be able to see it on the actual page but it allows users to swipe left and right to navigate, as well as give the tabs XML connections.
  3. Create a ViewPager adapter to store your switch case for tabbing between options and set the layouts you want to the correct tabs. This is also where you decide how many tabs you have.
  4. Initialize all these items in your activity like you would any page object.
  5. Make sure the activity is extending AppCompact.

Expected vs. Actual Behavior:


  1. Ability to go to Shopping list and the saved meal plan with just a swipe of a finger.
  2. Having some errors with my AppCompact extension, need to properly adjust the classes I'm using with it and ensure the logic still works.

Code Snippets:


ViewPagerAdapter:


package com.example.mealtime1;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager2.adapter.FragmentStateAdapter;

public class MyPagerAdapter extends FragmentStateAdapter {
    private static final int NUM_PAGES = 2;

    public MyPagerAdapter(@NonNull FragmentActivity fragmentActivity){
        super(fragmentActivity);
    }

    @NonNull
    @Override
    public Fragment createFragment(int position) {
        switch(position) {
            case 0:
                return new Tab1Fragment();
            case 1:
                return new Tab2Fragment();
            default:
                return null;
        }
    }

    @Override
    public int getItemCount() {
        return 0;
    }

    public class Tab1Fragment extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            return inflater.inflate(R.layout.main_menu_activity, container, false);
        }
    }

    public class Tab2Fragment extends Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            return inflater.inflate(R.layout.meal_result_activity, container, false);
        }
    }
}

XML Exerpt:


<com.google.android.material.tabs.TabLayout
            android:id="@+id/tab_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabMode="fixed"
            app:tabGravity="fill"/>
        <androidx.viewpager.widget.ViewPager
            android:id="@+id/view_pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

Environment Details:


Android Stuido, Windows OS.

For Issue Resolution:


Adjusting my class extensions and editing logic where necessary.

Current Status:


In progress

Cade5480 commented 3 months ago

I can help look at it if I need to, I was having issues with AppCompact when I was creating the splash screen, but I tried using it as a ComponentActivity and it worked all the same. If you need me to help though I would be more than happy to.

BeepDroid commented 2 months ago

@Cade5480 I had a breakthrough tonight and I'll happily let you look at it in class tomorrow since it's gonna take a little re-working, but all it is is time consuming rather than difficult