In my Android app, I'm having trouble setting a TextView that is located in a fragment from the parent activity. I am using a ViewPager as follows:
In my main activity's onCreate():
List<Fragment> fragments = new Vector<Fragment>();
fragments.add(Fragment.instantiate(this, MainPageProfileFragment.class.getName()));
fragments.add(Fragment.instantiate(this, MainPageOtherFragment.class.getName()));
mainPagerAdapter = new ViewPagerAdapter(this.getSupportFragmentManager(), fragments);
ViewPager pager = (ViewPager)findViewById(R.id.viewpager);
pager.setAdapter(mainPagerAdapter);
with the ViewPagerAdapter:
public class ViewPagerAdapter extends FragmentPagerAdapter{
private List<Fragment> fragments;
public ViewPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
super (fm);
this.fragments = fragments;
}
@Override
public Fragment getItem(int arg0) {
return this.fragments.get(arg0);
}
}
And the MainPageProfileFragment below. This fragment provides an interface to update a TextView at runtime (after the view is created).
public class MainPageProfileFragment extends Fragment
implements View.OnClickListener {
private TextView profileImageLabelTextView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if(container == null) {
return null;
}
ScrollView view = (ScrollView)inflater.inflate(R.layout.fragment_main_page_profile, container, false);
labelTextView = (TextView) view.findViewById(R.id.profileLabel);
return view;
}
public void setLabelText(String text) {
labelTextView.setText(text);
}
}
The setLabelTextView() I'd like to call from the main activity's onResume() handler, but I haven't yet been able to get a handle on the fragment that would allow me to access this method. I'm going a bit in circles now as I have found different approaches for accessing views in fragments where this seemed to be the most plausible but I haven't succeeded yet. How would I call the setLabelText() method from my activity?
@Override
protected void onResume() {
super.onResume();
// set the labelTextView here
}
The straitforward way would be to lookup ViewPager
, then take ViewPagerAdapter
and then try to get the fragment. If fragment is there, you can call setLabelText()
method. Something like this.
@Override
protected void onResume() {
super.onResume();
ViewPager pager = (ViewPager)findViewById(R.id.viewpager);
ViewPagerAdapter adapter = (ViewPagerAdapter) pager.getAdapter();
Fragment fragment = adapter.getItem(0);
if (fragment instanceof MainPageProfileFragment) {
// if we are here, then fragment is not null and has correct type
((MainPageProfileFragment)fragment).setLabelText("my text");
}
}
To add getItem()
method to an Adapter
, you should extend FragmentPagerAdapter
and track list of Fragments by your own. Like this
public class PagerAdapter extends FragmentPagerAdapter {
private List<Fragment> mFragments = new ArrayList<Fragment>();
@Override
public Fragment getItem(int position) {
return mFragments.get(position);
}
@Override
public int getCount() {
return mFragments.size();
}
public void addFragment(Fragment fragment) {
mFragments.add(fragment);
notifyDataSetChanged();
}
}
But a more elegant and flexible solution would be to use a event bus for Activity
to Fragment
communication. There are some event buses implementation out there. You can try TinyBus or EventBus .
我不认为这是最好的解决方案,但你可以尝试使用内部名称按标签名称检索片段:
"android:switcher:" + viewId + ":" + index;
Try this
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
ViewPagerAdapter adapter = (ViewPagerAdapter) viewPager.getAdapter();
Fragment fragment = adapter.getItem(position);
if (fragment instanceof BiographyFragment) {
// if we are here, then fragment is not null and has correct type
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
((BiographyFragment) fragment).mTextViewBiography.setText(fromHtml(mBiography, Html.FROM_HTML_MODE_LEGACY));
} else {
((BiographyFragment) fragment).mTextViewBiography.setText(fromHtml(mBiography));
}
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.