2

我想将数据从片段传递到 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();
    }
4

1 回答 1

0

是你图中的tablayout片段,所以它是接收你传递给它的任何参数的那个,而不是你的h1h2片段。

有很多事情需要纠正:

  1. 您使用错误-FragmentManagerViewPagerAdapter应该使用getChildFragmentManager()正确地将ViewPager片段嵌套在tablayout片段中。这是他们在轮换时正确保存和恢复状态所必需的:
ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager(), 0);
  1. 你期待和 的争论h1h2但永远不要调用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();
    }
}
于 2020-05-03T17:58:49.827 回答