Home > Software design >  Tab Layout not showing fragments
Tab Layout not showing fragments

Time:09-29

I have a fragment named "Notification fragment" in which I want to show another fragment named "Watching Fragment" using Tab layout . When I run the app, the tab layout bar is visible but the fragments are not visible.

My Fragment inside which other fragments are to be shown (Notifications Fragment)

public class NotificationsFragment extends Fragment{


    GridView listv;
    FragmentAdapter fragmentAdapter;

    private NotificationsViewModel notificationsViewModel;
    private FragmentNotificationsBinding binding;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        notificationsViewModel =
                new ViewModelProvider(this).get(NotificationsViewModel.class);

        binding = FragmentNotificationsBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

        TabLayout tabLayout = root.findViewById(R.id.tabLayout);
        ViewPager vp = root.findViewById(R.id.vp2);

        fragmentAdapter = new FragmentAdapter(getChildFragmentManager() , tabLayout.getTabCount());
        vp.setAdapter(fragmentAdapter);

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

                    if(tab.getPosition() == 0 || tab.getPosition() == 1 || tab.getPosition() == 2)
                            fragmentAdapter.notifyDataSetChanged();
                    }

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

                    }

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

                    }
                });

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

    return  root; }

My First Fragment (Watching Fragment)

public class WatchingFragment extends Fragment {

    ArrayList<String> list = new ArrayList<String>();
    GridView gridv1;

    private WatchingViewModel mViewModel;

    public static WatchingFragment newInstance() {
        return new WatchingFragment();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {

        View view =  inflater.inflate(R.layout.watching_fragment, container, false);

        gridv1 = view.findViewById(R.id.gridv1);


        Intent intent = getActivity().getIntent();
        String animename = intent.getStringExtra("nameanime");
        list.add(animename);


        loadData2();
        saveData2();

        ListNewAdapter adapter = new ListNewAdapter(getContext(), R.layout.watch_list, list);
       gridv1.setAdapter(adapter);
        gridv1.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                list.remove(position);
                adapter.notifyDataSetChanged();
                saveData2();
                return true;
            }
        });

    return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mViewModel = new ViewModelProvider(this).get(WatchingViewModel.class);
        // TODO: Use the ViewModel
    }

    private void saveData2() {
        SharedPreferences sp = getActivity().getSharedPreferences("shared preferences", MODE_PRIVATE);
        SharedPreferences.Editor ed = sp.edit();
        Gson gson = new Gson();
        String json = gson.toJson(list);
        ed.putString("anime list", json);
        ed.apply();
    }

    private void loadData2() {
        SharedPreferences sp = getActivity().getSharedPreferences("shared preferences", MODE_PRIVATE);
        Gson gson = new Gson();
        String json = sp.getString("anime list", "");
        Type type = new TypeToken<ArrayList<String>>() {}.getType();
        list = gson.fromJson(json , type);

        if (list == null) {
            list = new ArrayList<String>();
        }

    }

}

My View pager adapter

public class FragmentAdapter extends FragmentPagerAdapter {
int tabcount;
    public FragmentAdapter(@NonNull @NotNull FragmentManager fm, int behavior) {
        super(fm, behavior);
        tabcount = behavior;
    }

    @NonNull
    @NotNull
    @Override
    public Fragment getItem(int position) {
        switch (position){
            case 0: return new WatchingFragment();
            case 1: return new CompletedFragment();
            case 2: return new WantToWatchFragment();
            default: return null;
        }
    }

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

Notifications Fragment Layout

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.notifications.NotificationsFragment">

    <com.google.android.material.tabs.TabLayout
        android:id="@ id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Watching" />

        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Completed" />

        <com.google.android.material.tabs.TabItem
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Want To Watch" />
    </com.google.android.material.tabs.TabLayout>

    <androidx.viewpager.widget.ViewPager
        android:id="@ id/vp2"
        android:name="com.example.animeguide.ui.notifications.NotificationsFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tabLayout" />

</androidx.constraintlayout.widget.ConstraintLayout>

Watching Fragment Layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".ui.notifications.FragmentsInsideList.watching.WatchingFragment">

    <TextView
        android:id="@ id/textView4"
        android:layout_width="184dp"
        android:layout_height="58dp"
        android:text="hello world"
        android:textSize="34sp" />

    <GridView
        android:id="@ id/gridv1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="2" />

</LinearLayout>

CodePudding user response:

In order for the Adapter to know how many pages it has, you need to override getCount method. You did override it, but used 0 as the number of pages, thus having no pages.

In your FragmentAdapter class, try changing this:

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

To this:

@Override
public int getCount() {
    return tabcount;
}
  • Related