如何将变量从活动传递到片段不能解决以前的所有问题

问题描述 投票:0回答:2

如何将一个变量从ViewSyllabusActivity传递给片段尝试了很多例子,很多次我想将数据ViewSyllabusActivity.java传递给Fragment.java但我无法发送数据帮助我提前感谢

view syllabus activity.Java

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

        viewPager = (ViewPager) findViewById(R.id.viewpager);
        setupViewPager(viewPager);

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

        Intent intent = getIntent();
        int sem = intent.getIntExtra(SubListActivity.EXTRA_NUMBER, 0);
        String sid = intent.getStringExtra(SubListActivity.EXTRA_TEXT);

    }
    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());

        adapter.addFrag(new ObjFragment(), "Objectives");
        adapter.addFrag(new ContActivity(), "Contents");
        adapter.addFrag(new LaboActivity(), "Laboratory");
        adapter.addFrag(new TBActivity(), "Text Books");
        adapter.addFrag(new LaboActivity(), "References");
        viewPager.setAdapter(adapter);
    }

    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);
        }

    }

fragment.Java

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_obj, container, false);
        TextView textView = view.findViewById(R.id.obj);
    return view;
    }
java android android-layout android-fragments
2个回答
0
投票

你在ViewPagerAdapter中发送了一些值,如下所示: -

@Override
public Fragment getItem(int position) {
    Bundle bundle = new Bundle();
    if(position == 0){
        bundle.putInt("position", position);
        bundle.putString("survayType", "your value");
        mFragmentList.get(position).setArguments(bundle);
    }else if(position == 1){
        bundle.putInt("position", position);
        bundle.putString("survayType", "your value");
        mFragmentList.get(position).setArguments(bundle);
    }else if(position == 2){
        bundle.putInt("position", position);
        bundle.putString("survayType", "your value");
        mFragmentList.get(position).setArguments(bundle);
    }else if(position == 3){
        bundle.putInt("position", position);
        bundle.putString("survayType", "your value");
        mFragmentList.get(position).setArguments(bundle);
    }else if(position == 4){
        bundle.putInt("position", position);
        bundle.putString("survayType", "your value");
        mFragmentList.get(position).setArguments(bundle);
    }

    return mFragmentList.get(position);
}

并获取片段中的值,如下所示: -

String survayType = getArguments().getString("survayType");

我这样做,它对我有用。


2
投票

使用EventBus。这真的很容易也很好。

1 - 创建活动:

public class SendDataToFragmentEvent{
private YourDataType yourData;

 public SendDataToFragmentEvent(YourDataType yourData){ 
  this.yourData = yourData;
 }

 public void getYourData(){return yourData;}

}

而在您的活动中,在您想要将数据发送到片段的那一刻:

EventBus.getDefault.post(new SendDataToFragmentEvent(yourDataHere));

而且你必须等待片段中的这些数据:

@Subscribe(threadMode = ThreadMode.MAIN)  
public void onSendDataToFragmentEvent(SendDataToFragmentEventevent 
 event) {
/* Do something with your data here*/
};

不要忘记EventBus必须知道何时开始收听或停止:

 @Override
 public void onStart() {
    super.onStart();
    EventBus.getDefault().register(this);
 }

 @Override
 public void onStop() {
   super.onStop();
   EventBus.getDefault().unregister(this);
 }

这是因为Eventbus永远留在这里,直到你的片段/活动被破坏。

注意:片段必须打开才能收听您的活动。

Here是该库的链接,我发现它非常好。

© www.soinside.com 2019 - 2024. All rights reserved.