将Activity中的数据与ViewPager传递给Fragments

时间:2016-03-29 13:10:00

标签: android android-fragments android-viewpager

我研究了很多,但没有找到解决方案。

这是我的活动:

public class ProfileActivity extends AppCompatActivity {



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



        setContentView(R.layout.activity_profile);



        //region FindViews
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        tabLayout = (PagerSlidingTabStrip) findViewById(R.id.tabs);
        //endregion

        //region ViewPager
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        setupViewPager(viewPager);
        viewPager.setOffscreenPageLimit(5);
//        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setViewPager(viewPager);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        changeColor(ContextCompat.getColor(getApplicationContext(), R.color.theme_red_primary));
                        break;
                    case 1:
                        changeColor(ContextCompat.getColor(getApplicationContext(), R.color.theme_yellow_primary));
                        break;
                    case 2:
                        changeColor(ContextCompat.getColor(getApplicationContext(), R.color.theme_green_primary));
                        break;
                    case 3:
                        changeColor(ContextCompat.getColor(getApplicationContext(), R.color.theme_blue_primary));
                        break;
                    case 4:
                        changeColor(ContextCompat.getColor(getApplicationContext(), R.color.theme_black_primary));
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        //endregion

        extrasForFragment = new Bundle();
        extrasForFragment.putInt("name", 3);

        //region Get Extras from Intent
        extras = getIntent().getExtras();
        if (extras != null) {
            userID = extras.getInt(StaticVariables.INTENT_USERID, -1);
            Log.e("ISER", "dd " + userID);
            OverViewFragment.newInstance().setArguments(extrasForFragment);
        }
        //endregion

        setTitle("");
    }

class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        public void addFrag(Fragment fragment, String title) {
            mFragmentList.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }
    }





private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFrag(OverViewFragment.newInstance(), getString(R.string.overview));
        adapter.addFrag(new Fragment2(), getString(R.string.text));
        adapter.addFrag(new Fragment3(), getString(R.string.text));
        adapter.addFrag(new Fragment4(), getString(R.string.text));
        adapter.addFrag(new Fragment5(), getString(R.string.text));
        viewPager.setAdapter(adapter);
    }
}

但它不起作用..这是我的片段:

public class OverViewFragment extends Fragment {
 public static OverViewFragment newInstance() {
        return new OverViewFragment();
    }
 @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Bundle b = getArguments();
        if (b != null) {
            Log.e("EXTRAS", "sind: " + b.toString() + " und: " + b.getInt("name", 0));
        } else {
            Log.e("FAIL", "...");
        }
        return inflater.inflate(R.layout.fragment_recyclerview, container, false);
    }
}

但它没有用。我试过这个: Send data from activity to fragment in android 还有这个: Passing value between activity to fragment on android 但没有什么想工作.. 我希望有解决方案。谢谢你的帮助!

1 个答案:

答案 0 :(得分:6)

您拨打setArguments的唯一地方位于OverViewFragment.newInstance().setArguments(extrasForFragment);ViewPagersetupViewPager。您想在Fragment overviewFrag = OverViewFragment.newInstance(); overviewFrag.setArguments(<args_bundle>); adapter.addFrag(overviewFrag, getString(R.string.overview)); 方法中设置参数,如下所示:

title 'have1';
proc means data=have1 min p25 mean median p75 max;
run;
title 'have2';
proc means data=have2 min p25 mean median p75 max;
run;