无法使用 android jetpack 中的安全参数将数据从片段传递到 tablelayout

unable to pass data from fragment to tablelayout using safe Args in android jetpack

我想将数据从片段传递到 tablayout,但它返回空值。 tablayout由2个片段组成,如图this is my tablayout

我试图将数据从 tablayout 传递到片段及其工作,但我不知道为什么它的对面是 not.please 帮助我

this is my nav graph

这是我的第一个片段

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 是您图表中的片段,因此它是接收您传递给它的任何参数的片段,而不是您的 h1h2 片段。

您需要更正一些事项:

  1. 您在 ViewPagerAdapter 中使用了错误的 FragmentManager - 您应该使用 getChildFragmentManager()ViewPager 片段正确嵌套在 tablayout 中分段。这是他们在旋转时正确保存和恢复其状态所必需的:
ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager(), 0);
  1. 您期待 h1h2 上的参数,但永远不要在这些片段上调用 ​​setArguments()。在这里,您有两个选择:将参数从 tablayout 传递到每个片段或获取父片段的参数(从现在开始,h1h2 现在是子片段):
// 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();
    }
}