我想把数据从片段传到tablayout,但它返回的是null值,tablayout由两个片段组成,如图所示。这是我的tablayout
我已经尝试将数据从tablayout传递到fragment,它的工作,但我不知道为什么相反的是它没有。
这是我的第一个片段
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);
}
});
}
}
这是我的tablayout
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();
}
该 tablayout
是你的图中的Fragment,所以它是接受你传递给它的任何参数的那个,而不是你的 h1
和 h2
碎片。
有一些事情你需要纠正。
FragmentManager
与你 ViewPagerAdapter
- 你应该使用 getChildFragmentManager()
适当地嵌套你的 ViewPager
碎片 tablayout
碎片。这是它们在旋转时正确保存和恢复状态所必需的。ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager(), 0);
h1
和 h2
但千万不要打电话 setArguments()
在这些片段上。这里,你有两个选择:将参数从 tablayout
到每个片段中,或者获取父片段的参数(因为现在。h1
和 h2
现在是子片段)。)// Set the arguments from the tablayout onto h1 and h2
h1 f1=new h1();
f1.setArguments(getArguments());
h2 f2=new h2();
f2.setArguments(getArguments());
// OR get the arguments from the parent fragment
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Fragment parentFragment = getParentFragment();
Bundle parentArguments = parentFragment.getArguments();
if (parentArguments!=null)
{
tablayoutArgs args=tablayoutArgs.fromBundle(parentArguments);
String name=args.getName();
Log.i("name",""+name);
}
else {
Toast.makeText(getContext(), "null", Toast.LENGTH_SHORT).show();
}
}