java - 片段内的选项卡式布局无法正常工作
问题描述
我是一个初学者,试图在“主要”片段中应用选项卡式布局的想法,该片段也允许我导航到其他“次要片段”,有一个带有按钮的主要活动,单击该按钮时会膨胀选项卡式片段在这个活动里面。但我得到的是:由于某种原因,标签仅在第一个片段上重复。
这是我的代码:MainActiviy.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void fragny(View view) {
FragmentTransaction ft=getSupportFragmentManager().beginTransaction();
ft.replace(R.id.ma,new labRatFrag());
ft.commit();
}
} main_activity.xml
<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:id="@+id/ma"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:onClick="fragny"
/>
FragmentPagerAdapter
public class FragAdapt extends FragmentPagerAdapter {
private static final String[] TAB_TITLES = new String[]{"test1","test2","test3"};
private final Context mContext;
public FragAdapt(Context context ,FragmentManager fm) {
super(fm);
mContext=context;
}
@NonNull
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
return new labRatFrag();
case 1:
return new labRatFrag2();
case 2:
return new labRatFrag3();
default:
return null;
}
}
@Override
public String getPageTitle(int position) {
return TAB_TITLES[position];
}
@Override
public int getCount() {
return 3;
}
}
我的主要片段(labRatFrag.java)
public class labRatFrag extends Fragment {
public labRatFrag() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v= inflater.inflate(R.layout.fragment_lab_rat, container, false);
FragAdapt fa=new FragAdapt(getContext(),getActivity().getSupportFragmentManager());
ViewPager vp=v.findViewById(R.id.pagery);
vp.setAdapter(fa);
TabLayout tabs = v.findViewById(R.id.toto);
tabs.setupWithViewPager(vp);
return v;
}
}
fragment_lab_rat.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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"
>
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay"
>
<com.google.android.material.tabs.TabLayout
android:id="@+id/toto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"/>
</com.google.android.material.appbar.AppBarLayout>
<androidx.viewpager.widget.ViewPager
android:id="@+id/pagery"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
/>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
我想这样做是为了将这个想法与底部导航或导航抽屉应用到我计划的未来应用程序中
解决方案
FragAdapt
是给你的次要片段的,对吧?
如果是,请尝试更换
FragAdapt fa=new FragAdapt(getContext(),getActivity().getSupportFragmentManager());
和
FragAdapt fa=new FragAdapt(getContext(),getChildFragmentManager());
.
它可能会有所帮助。如果没有,将为您准备一个演示
推荐阅读
- php - 为什么在这种情况下使用 __dir__ 时我必须使用双 ../
- python - 在 scikit-learn 中使用带有预计算内核的 GaussianProcessRegressor
- mysql - 如何在 MySQL 数据库中获取会话/线程开始时间
- python - 如何在大数据中进行超参数优化?
- python - 使用 OOP 制作 Tkinter GUI
- swift - 将先前的视图变量附加到滑块视图?
- spring-boot - 选择spring data jpa 2.4.5时的案例
- javascript - 根据屏幕大小重新定位元素
- rxjs - rxjs 定期或定时器到期时调用函数
- javascript - 带有动画的 JS 的上一个和下一个按钮不起作用