Mam prosty viewPager z dwoma fragmentami. Każdy fragment zawiera TextView. Chciałbym móc zaktualizować tekst bieżącego textView wyświetlanego z działania.Jak zaktualizować zawartość fragmentów z działania (viewpager)?
aktywny:
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
ArrayList<Fragment> fragments = new ArrayList<Fragment>();
fragments.add(Fragment.instantiate(this, Live.class.getName()));
fragments.add(Fragment.instantiate(this, Hit.class.getName()));
this.pagerAdapter = new MyPagerAdapter(getSupportFragmentManager(),fragments);
ViewPager pager = (ViewPager) findViewById(R.id.viewpager);
pager.setAdapter(pagerAdapter);
}
}
adaptera:
public class MyPagerAdapter extends FragmentPagerAdapter {
private final ArrayList<Fragment> fragments;
public MyPagerAdapter(FragmentManager fm, ArrayList<Fragment> fragments)
{
super(fm);
this.fragments = fragments;
}
@Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
@Override
public int getCount() {
return this.fragments.size();
}
}
fragment:
public class Live extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.live, container,false);
}
}
XML fragment:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:text="First Fragment"
android:id="@+id/status"/>
</LinearLayout>
Każdy fragment posiada TextView z "status" id. Uaktualnianie widoku tekstu w pracach onCreateView
. Ale jak mogę zaktualizować textView z dowolnego miejsca w działaniu?
Próbowałem to bez sukcesu:
Fragment:
public class Live extends Fragment {
public TextView tv;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.live, container,false);
tv = (TextView) view.findViewById(R.id.status);
return view;
}
}
aktywny:
@Override
protected void onResume() {
super.onResume();
Live frag = (Live) pagerAdapter.getItem(0);
frag.tv.setText("Test 2");
}
Ale to daje mi NullPointerException na tv
, prawdopodobnie dlatego fragmentu jeszcze nie jest zawyżone.
ja już przeczytać poniższe pytania bez powodzenia:
- Updating fragments from Activity in a ViewPager
- Update a TextView in a ViewPager from another Fragment
Jest to 100% prawda. Zrobiłem już ten sam błąd co OP. NIE przechowuj własnej tablicy 'Fragment's.'FragmentManager' w' FragmentPagerAdapter' zawiera wszystkie 'Fragment's i zarządza zapisywaniem i przywracaniem ich stanu. Można je również przywoływać - http://stackoverflow.com/questions/8785221/retrieve-a-fragment-from-viewpager/14530629#14530629. –
Dobrze, zrobiłem to używając: http://tamsler.blogspot.fr/2011/10/android-viewpager-and-fragments.html i drugiego rozwiązania http://tamsler.blogspot.fr/2011/11/android-viewpager i-fragmenty-część-ii.html. Ale z tego, czego doświadczyłem, nie jest możliwe uzyskanie dostępu do fragmentu przy tworzeniu lub wznowieniu w działaniu, prawda? – grunk
To prawdopodobnie nie jest dostępne/napompowane w tym czasie. Dlaczego nie wykorzystasz fragmentu własnego cyklu życia? – faceman