gpt4 book ai didi

java - 在两个以上的 fragment 之间传递数据

转载 作者:太空狗 更新时间:2023-10-29 14:41:37 24 4
gpt4 key购买 nike

我成功地实现了使用接口(interface)将数据从一个 fragment 传递到另一个 fragment 。

MainActivity.java

MainActivity.java
public class MainActivity extends AppCompatActivity implements InputTab.SendMessage, FollowingTab.SendMessage, FollowerTab.SendMessage, ProfileTab.SendMessage, UserSearchResultTab.SendMessage {

private SectionsPageAdapter pageAdapter;
ViewPager viewPager;
public static String currentUser;

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

FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, new ProfileTab());

fragmentTransaction.commit();

pageAdapter = new SectionsPageAdapter(getSupportFragmentManager());

// Sets up the ViewPager with the sections adapter

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

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

}

// Adds fragments to SectionsPageAdapter and gives names for the corresponding tab

private void setupViewPager(ViewPager viewPager) {
SectionsPageAdapter adapter = new SectionsPageAdapter(getSupportFragmentManager());
adapter.addFragment(new InputTab(), "Search");
adapter.addFragment(new ProfileTab(), "Profile");
adapter.addFragment(new GithubTab(), "Github Repos");
adapter.addFragment(new FollowerTab(), "Followers");
adapter.addFragment(new FollowingTab(), "Followings");
adapter.addFragment(new UserSearchResultTab(), "test");
viewPager.setAdapter(adapter);
}

@Override
public void sendDataToProfile(String message) {
String tag = "android:switcher:" + R.id.container + ":" + 1;
ProfileTab profile = (ProfileTab) getSupportFragmentManager().findFragmentByTag(tag);
profile.displayReceivedData(message);
}

@Override
public void sendDataToFollowers(String message) {
String tag = "android:switcher:" + R.id.container + ":" + 3;
FollowerTab follower = (FollowerTab) getSupportFragmentManager().findFragmentByTag(tag);
follower.displayReceivedData(message);
}

InputTab.java 中的接口(interface) sendMessage

// Interface for passing-in data to a different tab(fragment)
interface SendMessage {
void sendDataToProfile(String message);

//void sendDataToSearchResult(String message);
void sendDataToResult(String message);
//void sendDataToFollowing(String message);
}

ProfileTab.java 中的接收器

// This is the part where it handles the data passed in from another fragment (tab)
protected void displayReceivedData(String message)
{
fetchData(message);
}

这很好用,但问题是它不适用于两个以上的 fragment ,例如从 A -> B、C、D ...

我遵循了这个特定的教程:https://www.journaldev.com/14207/android-passing-data-between-fragments

所以在我的 MainActivity.java 上,我的 sendDataToFollowers 会给我一个错误,而 sendDataToProfile 工作正常。当我调试它时,FollowerTab follower 为空。

让它适用于两个以上 fragment 的可能解决方案是什么?

最佳答案

你可以试试这个,

标签页 0

getParent().getIntent().putExtra("key", "value"); 

标签 1

String value = getParent().getIntent().getStringExtra("key");

另一种方式:

您必须将信息返回到 fragment 的 Activity 。你的 Activity 将信息分派(dispatch)给它的 fragment :

// In fragment A
((ParentActivity)getActivity()).dispatchInformations("test");

// In ParentActivity
public void dispatchInformations(String mesg){
fragmentB.sendMessage(mesg);
}

关于java - 在两个以上的 fragment 之间传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47109280/

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