选项卡布局不显示片段
Posted
技术标签:
【中文标题】选项卡布局不显示片段【英文标题】:Tab Layout not showing fragments 【发布时间】:2021-11-20 19:34:53 【问题描述】:我有一个名为“通知片段”的片段,我想在其中使用选项卡布局显示另一个名为“观看片段”的片段。当我运行应用程序时,选项卡布局栏可见,但片段不可见。
我的 Fragment 将在其中显示其他 Fragment(通知 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>();
我的视图寻呼机适配器
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_
android:layout_
tools:context=".ui.notifications.NotificationsFragment">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_
android:layout_
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.tabs.TabItem
android:layout_
android:layout_
android:text="Watching" />
<com.google.android.material.tabs.TabItem
android:layout_
android:layout_
android:text="Completed" />
<com.google.android.material.tabs.TabItem
android:layout_
android:layout_
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_
android:layout_
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_
android:layout_
android:orientation="vertical"
tools:context=".ui.notifications.FragmentsInsideList.watching.WatchingFragment">
<TextView
android:id="@+id/textView4"
android:layout_
android:layout_
android:text="hello world"
android:textSize="34sp" />
<GridView
android:id="@+id/gridv1"
android:layout_
android:layout_
android:numColumns="2" />
</LinearLayout>
【问题讨论】:
【参考方案1】:为了让 Adapter 知道它有多少页,你需要重写 getCount 方法。您确实覆盖了它,但使用 0 作为页数,因此没有页。
在您的 FragmentAdapter 类中,尝试更改:
@Override
public int getCount()
return 0;
到这里:
@Override
public int getCount()
return tabcount;
【讨论】:
知道了。谢谢。以上是关于选项卡布局不显示片段的主要内容,如果未能解决你的问题,请参考以下文章