Jadi, saya menggabungkan BottomNavbar dengan Tablayout, awalnya berfungsi dengan baik tetapi ketika saya mengubah menu dari bilah navigasi bawah dari home dan kemudian kembali ke menu lagi, salah satu fragmen tablayout hilang seperti ini : tablayout

fragment_makanan_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ViewMenuHome.MakananMenuFragment">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/black"
        android:layout_gravity="center"
        android:gravity="center"
        android:textSize="30sp"
        android:text="Hello text"/>
    

</FrameLayout>

fragment_jayanan_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ViewMenuHome.JajananMenuFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:textColor="@color/black"
        android:textSize="30sp"
        android:text="Jajanan Menu Fragment" />

</FrameLayout>

fragment_minuman_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ViewMenuHome.MinumanMenuFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:textColor="@color/black"
        android:textSize="30sp"
        android:text="@string/hello_blank_fragment" />

</FrameLayout>

Jawa

MenuFragment.java

package com.example.pesanpalgading20;

import android.content.Context;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

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

import com.google.android.material.tabs.TabLayout;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link MenuFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MenuFragment extends Fragment {

    TabLayout tabLayout;
    ViewPager viewPager;
    Context context;

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public MenuFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment MenuFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static MenuFragment newInstance(String param1, String param2) {
        MenuFragment fragment = new MenuFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View viewRoot = inflater.inflate(R.layout.fragment_menu, container, false);
        tabLayout = (TabLayout)viewRoot.findViewById(R.id.tabLayoutView);
        viewPager = (ViewPager)viewRoot.findViewById(R.id.containerViewPager);

        context = container.getContext();

        tabLayout.addTab(tabLayout.newTab().setText("Makanan"));
        tabLayout.addTab(tabLayout.newTab().setText("Minuman"));
        tabLayout.addTab(tabLayout.newTab().setText("Jajanan"));
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

        final MyAdapter adapter = new MyAdapter(getActivity(),getFragmentManager(),tabLayout.getTabCount());
        viewPager.setAdapter(adapter);

        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        // Inflate the layout for this fragment
        return viewRoot;
    }
}

Adapter Saya.java

package com.example.pesanpalgading20;

import android.content.Context;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import com.example.pesanpalgading20.ViewMenuHome.JajananMenuFragment;
import com.example.pesanpalgading20.ViewMenuHome.MakananMenuFragment;
import com.example.pesanpalgading20.ViewMenuHome.MinumanMenuFragment;

public class MyAdapter extends FragmentPagerAdapter {
    private Context myContext;
    int totalTabs;

    public MyAdapter (Context context, FragmentManager fm, int totalTabs){
        super(fm);
        myContext = context;
        this.totalTabs = totalTabs;
    }

    //fragment per tabs

    @Override
    public Fragment getItem(int position){
        switch(position){
            case 0:
                MakananMenuFragment makananMenuFragment = new MakananMenuFragment();
                return makananMenuFragment;
            case 1:
                MinumanMenuFragment minumanMenuFragment = new MinumanMenuFragment();
                return minumanMenuFragment;
            case 2:
                JajananMenuFragment jajananMenuFragment = new JajananMenuFragment();
                return jajananMenuFragment;
            default: return null;
        }
    }

    //counts total number of tabs
    @Override
    public int getCount() {
        return totalTabs;
    }
}

MakananMenuFragment.java

package com.example.pesanpalgading20.ViewMenuHome;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;

import com.example.pesanpalgading20.R;
import com.example.pesanpalgading20.ToppingAdapter.ToppingAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link MakananMenuFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MakananMenuFragment extends Fragment {

    ExpandableListView expandableListTopping;
    List<String> listGroup;
    HashMap<String,List<String>> listItem;
    ToppingAdapter toppingAdapter;

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public MakananMenuFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment MakananMenuFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static MakananMenuFragment newInstance(String param1, String param2) {
        MakananMenuFragment fragment = new MakananMenuFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    public MakananMenuFragment(String mParam1) {
        this.mParam1 = mParam1;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View viewRoot = inflater.inflate(R.layout.fragment_makanan_menu, container, false);



        // Inflate the layout for this fragment
        return viewRoot;

    }
}

JajananMenuFragment.java

package com.example.pesanpalgading20.ViewMenuHome;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

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

import com.example.pesanpalgading20.R;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link JajananMenuFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class JajananMenuFragment extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    public JajananMenuFragment(String mParam1) {
        this.mParam1 = mParam1;
    }

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public JajananMenuFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment JajananMenuFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static JajananMenuFragment newInstance(String param1, String param2) {
        JajananMenuFragment fragment = new JajananMenuFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

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

MinumanMenuFragment

package com.example.pesanpalgading20.ViewMenuHome;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

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

import com.example.pesanpalgading20.R;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link MinumanMenuFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MinumanMenuFragment extends Fragment {

    public MinumanMenuFragment(String mParam1) {
        this.mParam1 = mParam1;
    }

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public MinumanMenuFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment MinumanMenu.
     */
    // TODO: Rename and change types and number of parameters
    public static MinumanMenuFragment newInstance(String param1, String param2) {
        MinumanMenuFragment fragment = new MinumanMenuFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_minuman_menu, container, false);
    }
}
0
Rafly Andrian 19 Mei 2021, 21:14

1 menjawab

Jawaban Terbaik

Jadi saya menemukan solusinya di Tablayout tidak menampilkan konten

Ternyata masalahnya adalah getFragmentmanager() di MenuFragment.java saya

Ketika saya mengubah ke getChildFragmentManager() tidak ada masalah lagi

0
Rafly Andrian 20 Mei 2021, 14:37