我创建了一个FragmentAcitivity和一个ShareFragment。我想将变量(图像url)从FragmentAcitivity传递给ShareFragment。我尝试使用bundle来获取ShareFragment中的额外内容,但它不起作用。我把它放在FragmentActivity
中 private ShareFragment mShareFragment;
FragmentManager fm = getSupportFragmentManager();
mShareFragment = (ShareFragment) fm.findFragmentById(R.id.sharefragment);
Sharefragment.java:
public class ShareFragment extends Fragment {
private CallbackManager mCallbackManager;
private OnShareContentChangedListener mShareContentChangedListener;
private ViewPager mViewPage;
// The resource to url mapping
private static final ArrayList<Pair<Integer, String>> IMAGE_IDS =
new ArrayList<Pair<Integer, String>>() {{
add(new Pair<>(R.drawable.goofy, "http://shareitexampleapp.parseapp.com/goofy/"));
add(new Pair<>(R.drawable.liking, "http://shareitexampleapp.parseapp.com/liking/"));
add(new Pair<>(R.drawable.viking, "http://shareitexampleapp.parseapp.com/viking/"));
}};
private String _url;
@Override
public View onCreateView(
LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
mCallbackManager = CallbackManager.Factory.create();
View view = inflater.inflate(share_it_view, container, false);
LoginButton loginButton = (LoginButton) view.findViewById(R.id.connectWithFbButton);
loginButton.setFragment(this);
loginButton.setReadPermissions("public_profile");
setupViewPage(view);
LikeView pageLike = (LikeView) view.findViewById(R.id.like_page);
pageLike.setFragment(this);
return view;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mCallbackManager.onActivityResult(requestCode, resultCode, data);
System.out.println(".......ONACTIVITYRESULT......." );
}
public void setOnShareContentChangeListener(OnShareContentChangedListener listener) {
mShareContentChangedListener = listener;
}
public String getCurrentShareContent() {
return IMAGE_IDS.get(mViewPage.getCurrentItem()).second;
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
ShareImageFragment imageFragment = new ShareImageFragment();
imageFragment.setImage(IMAGE_IDS.get(position).first);
return imageFragment;
}
@Override
public int getCount() {
return IMAGE_IDS.size();
}
}
private void setupViewPage(View view) {
mViewPage = (ViewPager) view.findViewById(R.id.pager);
PagerAdapter adapter = new ScreenSlidePagerAdapter(getFragmentManager());
mViewPage.setAdapter(adapter);
final PageSelector pageSelector = (PageSelector) view.findViewById(R.id.page_selector);
pageSelector.setImageCount(IMAGE_IDS.size());
final LikeView photoLike = (LikeView) view.findViewById(R.id.like_photo);
photoLike.setFragment(this);
mViewPage.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(
int position,
float positionOffset,
int positionOffsetPixels) {
pageSelector.setPosition(position);
String shareContent = IMAGE_IDS.get(position).second;
photoLike.setObjectIdAndType(
shareContent,
LikeView.ObjectType.OPEN_GRAPH);
mShareContentChangedListener.onShareContentChanged(shareContent);
}
@Override
public void onPageSelected(int position) {
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
public interface OnShareContentChangedListener {
void onShareContentChanged(String content);
}
}
答案 0 :(得分:0)
试试这个,希望有所帮助
Bundle bundle = new Bundle();
bundle.putString("name", name);
bundle.putString("address", address);
FragmentA fragmentA = new FragmentA();
fragmentA.setArguments(bundle);
然后在片段
String name = getArguments().getString("name");
String address = getArguments().getString("address");
其他方式
获取片段中的值:
String name = ((C)getActivity()).getValue();
Toast.makeText(getActivity(), name, Toast.LENGTH_SHORT);
C班:
public String getValue()
{
return trainerStrDescription;
}