2013-02-28 13 views
18

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:

Odpowiedz

7

W swojej klasie MyPagerAdapter, Android nazywa getItem(...) tylko wtedy, gdy chce tworzyć nowe fragmenty. Dlatego używanie referencji jest problematyczne, zamiast tego powinieneś instatiować fragmenty.

Sprawdź tę odpowiedź support-fragmentpageradapter-holds-reference-to-old-fragments

+3

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. –

+1

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

+0

To prawdopodobnie nie jest dostępne/napompowane w tym czasie. Dlaczego nie wykorzystasz fragmentu własnego cyklu życia? – faceman

1
@Override 
    public int getItemPosition(Object object) { 
    XXXFragment fragment = (XXXFragment) object; 
     if(…condition){ 
    fragment.refresh(…); 
    } 
     return super.getItemPosition(object); 
    } 

nadpisanie wyżej. gdy wywołasz notifyDataSetChanged(), wyzwolona jest powyższa metoda. wykonaj odświeżanie, jak możesz.

2

I rozwiązać ten problem tak, tworzyć zasilacz jak ten

package com.yubraj.sample.adapter; 

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentStatePagerAdapter; 

import com.yubaraj.sample.activities.RashiFragment; 
import java.util.List; 


public class PageSliderAdapter extends FragmentStatePagerAdapter { 
    static String[] titleList = new String[] { "today", 
      "this month", "this year" }; 
    List<String> rashi; 
    // private static String TAG = "pagerscreenslider"; 

    public PageSliderAdapter(FragmentManager fm, List<String> rashiList) { 
     super(fm); 
     this.rashi = rashiList; 
    } 
    public void updateData(List<String> rashisList){ 
     rashi.clear(); 
     this.rashi = rashisList; 
     notifyDataSetChanged(); 
    } 
    @Override 
    public int getItemPosition(Object object) { 
     // TODO Auto-generated method stub 
     return POSITION_NONE; 
    } 

    @Override 
    public Fragment getItem(int position) { 
     Fragment fragment = new RashiFragment(); 
     Bundle bundle = new Bundle(); 
     bundle.putString("RASHI", rashi.get(position)); 
     bundle.putInt("RASHI_TYPE", position); 
     fragment.setArguments(bundle); 
     return fragment; 
    } 

    @Override 
    public int getCount() { 
     return titleList.length; 
    } 

    @Override 
    public String getPageTitle(int position) { 
     return titleList[position]; 
    } 


} 

Następnie ze swoją aktywność

List<String> rashiCollection = getJSONFromDb(Dates); 
adapter.updateData(rashiCollection); 
Powiązane problemy