我正在使用ViewPager并使用 FragmentPage Adapter 在Fragment Activity中加载多个片段。我想访问包含 ImageButton 的片段。任何人都可以帮我从片段活动中的片段中访问 Imagebutton 吗?
这是我的代码:
片段活动
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_gallery);
mAdapter = new MyAdapter(getSupportFragmentManager());
mPager = (CustomPager) findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
mPager.setOnPageChangeListener(this);
pagerLayout = (LinearLayout) findViewById(R.id.tour_pager_lay);
mPageControl = (PageControl)findViewById(R.id.horizontal_pager);
mPageControl.addPagerControl(3);
}
public static class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public int getCount() {
return 3;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new ImageFragment(R.drawable.landing_logo);
case 1:
return new ImageFragment(R.drawable.welcome_postit);
case 2:
return new LoginFragment();
case 4:
return new RegisterFragment();
default:
return null;
}
}
}
答案 0 :(得分:1)
假设您的ImageButton在RegisterFragment上,那么您可以这样做:
RegisterFragment tmp = ((RegisterFragment) ((MyAdapter) mPager.getAdapter()).instantiateItem(mPager, 4));
//your ImageButton should be public or you can create a getter or something, I assume that it's public
tmp.imageButton.setText("foo");