简体   繁体   English

如何将数据从活动传递到可滑动片段?

[英]how to pass data from activity to swipable fragments?

i have data in activity in which some data i want to show in activity itself and some data i want to show in three swipable fragments. 我有活动数据,其中一些数据我想在活动本身中显示,一些数据我想在三个可滑动片段中显示。 so how do i pass data from activity to that fragments. 所以我如何将数据从活动传递到该片段。 i am gonna put image of what i want to do. 我要把我想做的事的形象。 在此图像中,我有一些数据可以显示活动的上半部分,而另一些则要显示在片段中,因此这就是为什么要将这些数据传递给可滑动片段的原因。

Please give solution about this.Thanks. 请对此提供解决方案。谢谢。

I think your main question is how to pass data from activity to fragment?Right? 我认为您的主要问题是如何将数据从活动传递到片段?

You can pass data from activity to fragment by creating a Bundle and put some data to it and then pass it to the setArguments method. 您可以通过创建Bundle并将数据放入活动中,然后再将其传递给setArguments方法,从而将数据从活动传递至片段。

    YourFragment yourFragment = new YourFragment();
    Bundle bundle = new Bundle();
    bundle.putInt(KEY_RECIPE_INDEX, index);//you can use bundle.putString or others to pass different types of data
    yourFragment.setArguments(bundle);

Now you can retrive the data in your fragment by using getArguments 现在,您可以使用getArguments检索片段中的数据

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    int index = getArguments().getInt(ViewPagerFragment.KEY_RECIPE_INDEX);
    //........

You also ask in your question that you have some data in your activity and some of the data you want to show on activity and some of them you want to show in swipable fragments.Now I am going to show you how to do that by using an example. 您还问一个问题,您的活动中有一些数据,要在活动中显示的一些数据,以及想以可滑动片段显示的一些数据。现在,我将向您展示如何使用一个例子。

Suppose you have two swipable tab and each tab has a fragment 假设您有两个可滑动的标签,每个标签都有一个片段

final IngredientsFragment ingredientsFragment = new IngredientsFragment();
    Bundle bundle = new Bundle();
    bundle.putInt(KEY_RECIPE_INDEX, index);
    ingredientsFragment.setArguments(bundle);

    final DirectionsFragment directionsFragment = new DirectionsFragment();
    bundle = new Bundle();
    bundle.putInt(KEY_RECIPE_INDEX, index);
    directionsFragment.setArguments(bundle);


    ViewPager viewPager = (ViewPager) view.findViewById(R.id.viewPager);
    viewPager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
        @Override
        public android.support.v4.app.Fragment getItem(int position) {
            return position == 0 ? ingredientsFragment : directionsFragment;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return position == 0 ? "Ingredients" : "Directions";
        }

        @Override
        public int getCount() {
            return 2;
        }
    });

    TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tabLayout);
    tabLayout.setupWithViewPager(viewPager);

I think there is no problem of the data that you want to show in your activity. 我认为您想要在活动中显示的数据没有问题。

Qick recap: You pass the data to fragment by creating a bundle and put some data and use yourfragment.setArguments(yourBundle). 快速回顾:您通过创建捆绑包将数据传递给片段,并放置一些数据并使用yourfragment.setArguments(yourBundle)。

Now I think you can pass data to swipable views. 现在,我认为您可以将数据传递到可滑动视图。

Hope this helps! 希望这可以帮助!

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

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