选项卡布局不显示片段

Tab Layout not showing fragments

我有一个名为“Notification fragment”的片段,我想在其中使用 Tab 布局显示另一个名为“Watching 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; }

我的第一个片段(观看片段)

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 寻呼机适配器

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;
    }
}

通知片段布局

<?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>

观看片段布局

<?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>

为了让适配器知道它有多少页,您需要覆盖 getCount 方法。您确实覆盖了它,但使用 0 作为页数,因此没有页数。

在您的 FragmentAdapter class 中,尝试更改此设置:

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

为此:

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