我想将数据从片段传递到 tablayout,但它返回 null 值。tablayout 由 2 个片段组成,如图所示这是我的 tablayout
我试图将数据从 tablayout 传递到片段及其工作,但我不知道为什么相反的不是。请帮助我
这是我的第一个片段
public class first_Frag extends Fragment {
NavController navController;
public first_Frag() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_first_, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
navController= Navigation.findNavController(view);
Button b=view.findViewById(R.id.btn);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
first_FragDirections.ActionFirstFrag2ToTablayout action=first_FragDirections.actionFirstFrag2ToTablayout();
action.setName("yoo");
navController.navigate(action);
}
});
}
}
这是我的表格布局
public class tablayout extends Fragment {
Toolbar toolbar;
TabLayout tabLayout;
ViewPager viewPager;
public tablayout() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_tablayout, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
final NavController navController = Navigation.findNavController(view);
//tablayoutDirections. action=tablayoutDirections.actionTablayout2ToB42();
// toolbar = view.findViewById(R.id.toolbar);
tabLayout = view.findViewById(R.id.tab_layout);
viewPager = view.findViewById(R.id.view_pager);
h1 f1=new h1();
h2 f2=new h2();
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
tabLayout.setupWithViewPager(viewPager);
ViewPagerAdapter adapter = new ViewPagerAdapter(getActivity().getSupportFragmentManager(), 0);
adapter.addtabs(f1,"first");
adapter.addtabs(f2,"second");
viewPager.setAdapter(adapter);
}
private class ViewPagerAdapter extends FragmentPagerAdapter {
List<Fragment> fragments = new ArrayList<>();
List<String> titles = new ArrayList<>();
public ViewPagerAdapter(@NonNull FragmentManager fm, int behavior) {
super(fm, behavior);
}
public void addtabs(Fragment fragment, String title) {
fragments.add(fragment);
titles.add(title);
}
@NonNull
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
@Override
public int getCount() {
return fragments.size();
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return titles.get(position);
}
}
这是我需要获取数据的 tablayout item-one(FIRST) 片段
public class h1 extends Fragment {
public h1() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_h1, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
final NavController navController= Navigation.findNavController(view);
if (getArguments()!=null)
{
tablayoutArgs args=tablayoutArgs.fromBundle(getArguments());
String name=args.getName();
Log.i("name",""+name);
}
else {
Toast.makeText(getContext(), "null", Toast.LENGTH_SHORT).show();
}