【问题标题】:How to pass data from Fragment to Tablayout fragment如何将数据从 Fragment 传递到 Tablayout 片段
【发布时间】:2019-12-23 00:29:26
【问题描述】:

这将在使用 viewpageAdapter 的片段中运行片段。我想将数据从片段传递到 tablayout 片段。我尝试了任何方法,但仍然崩溃。我只想将一次数据传递给 tablayout 中的片段。

第一个片段。这条线我想传递给 tablayout 片段:-

specItem = getArguments().getString("spec_item");

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    imageUrl = getArguments().getString("image_url");
    priceItem = getArguments().getString("price_item");
    specItem = getArguments().getString("spec_item");

}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_view_details_item, container, false);

    textPrice = (TextView) rootView.findViewById(R.id.textPrice);
    image_Details = (ImageView) rootView.findViewById(R.id.image_Detail);
    tabLayout = (TabLayout) rootView.findViewById(R.id.tablayout_id);
    viewPager = (ViewPager) rootView.findViewById(R.id.viewpage_id);

    textPrice.setText(priceItem);

    Picasso.get().load(imageUrl).into(image_Details);

    ViewPageAdapter adapter = new ViewPageAdapter(getActivity().getSupportFragmentManager());
    adapter.AddFragment(new FragmentSpecifications(), "Specification");
    adapter.AddFragment(new FragmentReview(), "Review");

    viewPager.setAdapter(adapter);
    tabLayout.setupWithViewPager(viewPager);


    return rootView;
}

tablayout 中的第二个片段,我不知道如何接收:-

View rootView;

TextView textSpec;
String specItem;
public FragmentSpecifications() {
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.fragment_specification, container, false);

    textSpec = (TextView) rootView.findViewById(R.id.textviewspec);

    textSpec.setText();

    return rootView;
}

【问题讨论】:

    标签: java android-studio


    【解决方案1】:

    在您的FragmentSpecifications 中创建Fragment 的实例以向其发送数据,如下所示:

    public static FragmentSpecifications newInstance(String priceItem) {
    
        Bundle args = new Bundle();
        args.putString("priceItem", priceItem);
        FragmentSpecifications fragment = new FragmentSpecifications();
        fragment.setArguments(args);
        return fragment;
    }
    

    之后在同一 FragmentSpecifications 覆盖 onCreate 方法并获取值:

    private String priceItem;
    
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null)
           priceItem = getArguments().getString("priceItem");
    }
    

    最后,当您添加 FragmentSpecifications 时,适配器会传递值:

    adapter.AddFragment(FragmentSpecifications.newInstance(priceItem), "Specification");
    

    【讨论】:

    • 这个答案可以正常工作,但是当我返回并再次进入内部时,数据将丢失。为什么以及如何解决它?
    • 返回页面重新输入会丢失数据
    • 我不知道您的应用程序的工作流程,您始终可以创建一个变量 static 以保持整个应用程序的生命周期,但请记住,您需要正确处理 static 变量否则您会在应用程序中遇到一些错误。
    • 我可以显示之前和之后吗? 4小时后,我尝试搜索任何方法,仍然一样。
    • 默认情况下tablayout会清空文本。所以原文无法说明原因?
    猜你喜欢
    • 2020-12-23
    • 1970-01-01
    • 2021-07-12
    • 2017-01-18
    • 1970-01-01
    • 1970-01-01
    • 2014-04-17
    • 1970-01-01
    • 2013-07-11
    相关资源
    最近更新 更多