如何将变量从活动传递到片段不能解决以前的所有问题
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何将变量从活动传递到片段不能解决以前的所有问题相关的知识,希望对你有一定的参考价值。
如何将一个变量从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;
}
答案
你在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");
我这样做,它对我有用。
另一答案
使用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是该库的链接,我发现它非常好。
以上是关于如何将变量从活动传递到片段不能解决以前的所有问题的主要内容,如果未能解决你的问题,请参考以下文章
将数据从片段列表视图项传递到活动字符串变量 onitemclicklistener
如何将变量从 Activity 传递到 Fragment,并将其传回?
如果几何着色器处于活动状态,如何将信息从顶点着色器传递到片段着色器?