简体   繁体   English

如何使用 ViewPager2 将数据从 Activity 发送到 Fragment?

[英]How to send data from Activity to Fragment with ViewPager2?

I'm trying to send data from MainActivity to one of the fragments which was created using ViewPager2.我正在尝试将数据从 MainActivity 发送到使用 ViewPager2 创建的片段之一。

MainActivity:主要活动:

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);

    ViewPager2 viewPager = findViewById(R.id.viewPager);
    MainPagerAdapter pagerAdapter = new MainPagerAdapter(this);
    viewPager.setAdapter(pagerAdapter);
}

MainPagerAdapter:主页面适配器:

public class MainPagerAdapter extends FragmentStateAdapter
{
    public MainPagerAdapter(@NonNull FragmentActivity fragmentActivity)
    {
        super(fragmentActivity);
    }

    @NonNull
    @Override
    public Fragment createFragment(int position)
    {
        switch (position)
        {
            case 0:
                return new Fragment1();
            case 1:
                return new Fragment2();
            default:
                return new Fragment3();
        }
    }

    @Override
    public int getItemCount()
    {
        return 3;
    }
}

I send data in MainActivity:我在 MainActivity 中发送数据:

Fragment1 fragment1 = new Fragment1();

Bundle bundle = new Bundle();
bundle.putInt("1", 1);
fragment1.setArguments(bundle);

And try to get it in Fragment1:并尝试在 Fragment1 中获取它:

Bundle bundle = this.getArguments();
if (bundle != null)
{
    int i = bundle.getInt("1", 0);
    Log.d(Constants.LOG_TAG, "" + i);
}

But it always comes with an empty bundle because the fragment is created when the viewpager scrolls through it.但它总是带有一个空包,因为片段是在 viewpager 滚动浏览它时创建的。

How can I send data to Fragment when MainActivity is created?如何在创建 MainActivity 时向 Fragment 发送数据?

My solution:我的解决方案:

MainActivity:主要活动:

pagerAdapter = new MainPagerAdapter(this);
pagerAdapter.setData(fragment1Container);

MainPagerAdapter:主页面适配器:

public class MainPagerAdapter extends FragmentStateAdapter
{
    private Fragment1Container fragment1Container;
    private Fragment2Container fragment2Container;

    public void setData(Fragment1Container container)
    {
        fragment1Container = container;
    }

    public void setData(Fragment2Container container)
    {
        fragment2Container = container;
    }

    @NonNull
    @Override
    public Fragment createFragment(int position)
    {
        switch (position)
        {
            case 0:
                Fragment1 fragment1 = new Fragment1();

                Bundle bundle = new Bundle();
                Gson gson = new Gson();

                bundle.putString("fragment1Container", gson.toJson(fragment1Container));
                fragment1.setArguments(bundle);

                return fragment1;
            case 1:...

Fragment1:片段1:

private Fragment1Container fragment1Container;

if (bundle != null)
{
    Gson gson = new Gson();
    String jsonString = bundle.getString("fragment1Container");
    Type entityType = new TypeToken<Fragment1Container>(){}.getType();
    fragment1Container = gson.fromJson(jsonString , entityType );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM