Fragment
Frafment1中我还写了个Fragment切换,如果不需要可以删除,写成和Fragment2一样即可。
public class Fragment1 extends Fragment {
public Fragment1() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_1, container, false);
Button btn_random = (Button) view.findViewById(R.id.btn_random);
getActivity().getSupportFragmentManager().beginTransaction()
// 在指定的容器中添加新的Fragment
.add(R.id.fragment_container,new FirstFragment())
// 提交事务
.commit();
btn_random.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
// 创建数组
Class<?>[] fragmentClasses = new Class<?>[]{
FirstFragment.class,SecondFragment.class,ThirdFragment.class};
// 生成一个随机索引
int randomIndex = new Random().nextInt(fragmentClasses.length);
// 根据随机索引创建对应的Fragment实例
Fragment randomFragment = null;
try {
randomFragment = (Fragment) fragmentClasses[randomIndex].newInstance();
} catch (InstantiationException | IllegalAccessException | java.lang.InstantiationException e) {
e.printStackTrace();
}
// 替换为随机选择的Fragment
if (randomFragment != null) {
fragmentTransaction.replace(R.id.fragment_container, randomFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
}
});
return view;
}
}
public class Fragment2 extends Fragment {
public Fragment2() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_2, container, false);
}
}
public class Fragment3 extends Fragment {
public Fragment3() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_3, container, false);
}
}
public class Fragment4 extends Fragment {
public Fragment4() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_4, container, false);
}
}
对于Fragment中的页面内容随便写个TextView就行。
FragmentAdapter
public class FragmentAdapter extends FragmentStateAdapter {
List<Fragment> fragmentList = new ArrayList<>();
public FragmentAdapter(@NonNull FragmentActivity fragmentActivity,List<Fragment> fragmentList) {
super(fragmentActivity);
this.fragmentList = fragmentList;
}
@NonNull
@Override
public Fragment createFragment(int position) {
return fragmentList.get(position);
}
@Override
public int getItemCount() {
return fragmentList.size();
}
}
MainActivity
public class MainActivity extends AppCompatActivity {
private ViewPager2 view_pager2;
private TabLayout tab_layout;
private List<Fragment> fragmentList;
private static final String[] NAMES = {
"Fragment1", "Fragment2", "Fragment3", "Fragment4"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//绑定空间
view_pager2 = findViewById(R.id.view_pager2);
tab_layout = findViewById(R.id.tab_layout);
initFragmentList();
FragmentAdapter adapter = new FragmentAdapter(MainActivity.this, fragmentList);
view_pager2.setAdapter(adapter);
view_pager2.setCurrentItem(0);
//不支持viewpager2
//tab_layout.setupWithViewPager(view_pager2);
TabLayoutMediator tabLayoutMediator = new TabLayoutMediator(tab_layout, view_pager2, false, true, (tab, position) -> tab.setText(NAMES[position]));
tabLayoutMediator.attach();
}
public void initFragmentList() {
fragmentList = new ArrayList<>();
fragmentList.add(new Fragment1());
fragmentList.add(new Fragment2());
fragmentList.add(new Fragment3());
fragmentList.add(new Fragment4());
}
}
文章评论