gpt4 book ai didi

javascript - Android 事件总线将数据从 Activity 传递到选项卡 fragment

转载 作者:行者123 更新时间:2023-11-28 05:00:42 25 4
gpt4 key购买 nike

我的程序中有 5 个选项卡 fragment ,我想将数据从工具栏按钮传递到 5 个选项卡 fragment ,但问题是当我单击工具栏按钮时,它只传递到 3 个 fragment ,任何人都知道解决这个问题

主要 Activity

这是我在 MainActivity 中的 onclick 函数

    public void editpage( View view) {

ToggleButton etMessage = (ToggleButton) findViewById(R.id.toggleButton);
Events.ActivityFragmentMessage activityFragmentMessageEvent = new Events.ActivityFragmentMessage(String.valueOf(etMessage.getText()));
Events.ActivityFragmentMessage2 activityFragmentMessageEvent2 = new Events.ActivityFragmentMessage2(String.valueOf(etMessage.getText()));
Events.ActivityFragmentMessage3 activityFragmentMessageEvent3 = new Events.ActivityFragmentMessage3(String.valueOf(etMessage.getText()));
Events.ActivityFragmentMessage4 activityFragmentMessageEvent4 = new Events.ActivityFragmentMessage4(String.valueOf(etMessage.getText()));
Events.ActivityFragmentMessage5 activityFragmentMessageEvent5 = new Events.ActivityFragmentMessage5(String.valueOf(etMessage.getText()));
GlobalBus.getBus().post(activityFragmentMessageEvent);
GlobalBus.getBus().post(activityFragmentMessageEvent2);
GlobalBus.getBus().post(activityFragmentMessageEvent3);
GlobalBus.getBus().post(activityFragmentMessageEvent4);
GlobalBus.getBus().post(activityFragmentMessageEvent5);

}

list_fragment2

这就是单击 MainActivity 上的切换按钮后我接收数据的方式

@Override
public void onStart() {
super.onStart();
GlobalBus.getBus().register(this);
}
@Subscribe
public void getMessage(Events.ActivityFragmentMessage activityFragmentMessage) {

messageView2 = (TextView) getView().findViewById(R.id.sum2);
messageView2.setText(String.valueOf(activityFragmentMessage.getMessage()));
messageView2.setText(activityFragmentMessage.getMessage());
i = messageView2.getText().toString();
result = Integer.parseInt(i);
if (result == 1) {
Nsum[0]++;
editor.putInt(PREFS_KEY,Nsum[0]);
editor.commit();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(list_fragment2.this).attach(list_fragment2.this).commit();

}else if (result ==0){
editor.remove(PREFS_KEY);
editor.commit();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(list_fragment2.this).attach(list_fragment2.this).commit();
}

}
@Override
public void onDestroyView() {
super.onDestroyView();
GlobalBus.getBus().unregister(this);
}

orderlist_fragment

 @Override
public void onStart() {
super.onStart();
GlobalBus.getBus().register(this);
}
@Subscribe
public void getMessage3(Events.ActivityFragmentMessage3 activityFragmentMessage3) {

messageView3 = (TextView) getView().findViewById(R.id.sum3);
messageView3.setText(String.valueOf(activityFragmentMessage3.getMessage3()));
messageView3.setText(activityFragmentMessage3.getMessage3());
i = messageView3.getText().toString();
result = Integer.parseInt(i);
if (result == 1) {
Nsum[0]++;
editor.putInt(PREFS_KEY,Nsum[0]);
editor.commit();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(orderlist_fragment.this).attach(orderlist_fragment.this).commit();

}else if (result ==0){
editor.remove(PREFS_KEY);
editor.commit();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(orderlist_fragment.this).attach(orderlist_fragment.this).commit();
}

}
@Override
public void onDestroyView() {
super.onDestroyView();
GlobalBus.getBus().unregister(this);
}

Tab4_fragment

 @Override
public void onStart() {
super.onStart();
GlobalBus.getBus().register(this);
}
@Subscribe
public void getMessage(Events.ActivityFragmentMessage4 activityFragmentMessage4) {

messageView4 = (TextView) getView().findViewById(R.id.sum4);
messageView4.setText(String.valueOf(activityFragmentMessage4.getMessage4()));
messageView4.setText(activityFragmentMessage4.getMessage4());
i = messageView4.getText().toString();

result = Integer.parseInt(i);
if (result == 1) {
Nsum[0]++;
editor.putInt(PREFS_KEY,Nsum[0]);
editor.commit();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(Tab4_fragment.this).attach(Tab4_fragment.this).commit();

}else if (result ==0){
editor.remove(PREFS_KEY);
editor.commit();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(Tab4_fragment.this).attach(Tab4_fragment.this).commit();
}

}
@Override
public void onDestroyView() {
super.onDestroyView();
GlobalBus.getBus().unregister(this);
}

最佳答案

这是因为 fragment 4和5还没有创建。将 setOffscreenPageLimit(5) 添加到 viewPager 实例中。它将把所有 5 个 fragment 保留到内存中,而不是使用延迟加载机制。

官方文档链接setOffscreenPageLimit

Set the number of pages that should be retained to either side of the current page in the view hierarchy in an idle state. Pages beyond this limit will be recreated from the adapter when needed.

关于javascript - Android 事件总线将数据从 Activity 传递到选项卡 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42151055/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com