Android 从片段对象访问FragmentActivity中的元素
我有一个带有FragmentPagerAdapter的FragmentActivity,它保存了许多碎片对象,我可以通过水平滑动来滑动 我想知道是否可以从片段对象中访问FragmentActivity中的元素(“elementToBeChanged”)?我想将一个字符串从Fragment类传递到FragmentActivity对象中的TextView元素Android 从片段对象访问FragmentActivity中的元素,android,android-fragments,swipe,fragmentpageradapter,Android,Android Fragments,Swipe,Fragmentpageradapter,我有一个带有FragmentPagerAdapter的FragmentActivity,它保存了许多碎片对象,我可以通过水平滑动来滑动 我想知道是否可以从片段对象中访问FragmentActivity中的元素(“elementToBeChanged”)?我想将一个字符串从Fragment类传递到FragmentActivity对象中的TextView元素 public class GalleryPagerActivity extends FragmentActivity implements O
public class GalleryPagerActivity extends FragmentActivity implements OnClickListener {
private TextView elementToBeChanged;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.gallery_pager);
elementToBeChanged = (TextView) findViewById(R.id.tvTop);
elementToBeChanged.setOnClickListener(this);
}
}
我该怎么做?每次看到片段对象时(通过滑动),都必须更新TextView元素
它应该是这样的:
public class GalleryFragment extends Fragment {
private FragmentActivity fa;
private LinearLayout ll;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
fa = super.getActivity();
ll = (LinearLayout) inflater.inflate(R.layout.gallery_fragment, container, false);
...
// ////////
// UPDATE THE "TextView elementToBeChanged" object in the parent FragmentActivity class HERE WHENEVER THIS FRAGMENTS BECOMES ACTIVE/VISIBLE...
// ////////
return ll;
}
在GalleryPageActivity中创建一个函数,例如updateTextView(字符串文本)。该函数将
TextView
设置为任何输入值。然后,在GalleryFragment中,只需调用此函数并显示如下文本:
((GalleryPagerActivity)getActivity()).updateTextView("my text");
我能为相反的情况做些什么?我想从fragmentActivity更新fragment中的一个元素。我跟随这个链接:;;;;;但他们说“不要每次设置文本时都创建新对象。使用与查看页面相同的片段对象。”。我怎样才能访问那个碎片对象?所以我遵循这个链接:;;;;但我不知道现在该怎么办!请看我的相关问题: