简体   繁体   中英

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.

    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

@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).

Now I think you can pass data to swipable views.

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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