2012-11-29 90 views
10

我是Android新手,我正在實施一個小型項目,在該項目中我從一個Drupal網站獲得新聞。這個應用程序有3個選項卡,每個選項卡都有一個包含特定內容的文章列表。文章列表由文本瀏覽和排列在linearlayout中的圖像視圖創建。當我點擊標題時,文章將打開詳細信息。這些文章通過HTTP POST和ViewJSON模塊從Drupal站點加載。標籤用FragmentActivity和TabHost創建。一切正常,工作正常,直到這裏。如何刷新按鈕上的片段標籤內容點擊

我的問題是,我想做一個刷新按鈕,將其放在標籤上,當我按下它時,文章列表必須刷新或重新加載並保持當前標籤打開。 我試圖替換標籤片段內容並重新打開它,但是當我點擊一篇文章的標題打開它時,標籤內容仍保留在所有片段下的堆棧中。我提到,當我點擊文章的標題時,一個新的片段正在打開。我發送了一篇文章的ID作爲參數,並在同一個片段中打開文章的內容。

我正在嘗試使用此代碼,但沒有成功。

Button btn_refresh = (Button) findViewById(R.id.btn_refresh); 
     btn_refresh.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) { 
       String current_tab = mTabHost.getCurrentTabTag(); 
       if(current_tab.contains("POPULARE")){ 

        Fragment f; 
        f = new PopulareActivity(); 
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
        ft.replace(R.id.tot,f); 
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); 
        ft.commit(); 
       } 
       if(current_tab.contains("RECOMANDATE")){ 

        Fragment f; 
        f = new RecomandateActivity(); 
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
        ft.replace(R.id.tot,f); 
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); 
        ft.commit(); 
       } 

更確切地說我上的應用,按TAB2(顯示從TAB2文章列表),按刷新按鈕,打開一文中,按回車鍵移動設備的後退按鈕,讓我看看TAB2的內容。現在,我進入tab1(並顯示tab1中的文章列表),從tab1列表中打開一篇文章,向我展示我需要的內容,然後按下移動設備上的後退按鈕。在這一刻顯示了tab2內容(文章列表形式從我按下刷新按鈕的位置tab2)。提前

  tv.setOnClickListener(new View.OnClickListener() { 
        public void onClick(View v) { 

          Fragment f; 
          f = new DetaliiActivity(); 
          Bundle data = new Bundle(); 
          data.putInt("id", nid); 
          f.setArguments(data); 
          FragmentTransaction ft = getFragmentManager().beginTransaction(); 

          ft.replace(R.id.tot,f); 

          ft.addToBackStack(null); 
          ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); 
          ft.commit(); 
        } 
       }); 

感謝您的幫助:

要打開的文章中,我使用的詳細信息!

回答

6

要刷新片段的內容,可以保留對該片段的引用,並在該片段中調用公共(例如)refresh()方法。例如:

public class ArticleTabFragmentActivity extends FragmentActivity{ 
    private PopulareFragment populareFragment; 
    private RecomandateFragment recomandateFragment; 

    <code> 

    private void bindView(){ 
    Button btn_refresh = (Button) findViewById(R.id.btn_refresh); 

    btn_refresh.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) { 
       String current_tab = mTabHost.getCurrentTabTag(); 
       if(current_tab.contains("POPULARE")){ 
        populareFragment.refresh(); 
       } else if(current_tab.contains("RECOMANDATE")){ 
        recomandateFragment.refresh(); 
       } 
    }); 
    } 



} 

public class PopulareFragment extends Fragment{ 

    public void refresh(){ 
    <refresh the contents of the fragment> 
    } 

} 

<Same for other fragment> 

現在,當您創建的標籤片段,像PupulareFragment,使用pupulareFragment實例變量來存儲片段。所以它在刷新按鈕onClick方法中可用。

這樣,刷新時不會替換/創建任何新的碎片。因此,在閱讀文章詳細信息活動後返回tabActivity,可能會正常工作。

+3

你能詳細介紹一下刷新方法嗎?我不清楚這將如何很好地刷新片段仍然 – ChuckKelly

+0

刷新方法可以包含函數來刷新片段中的元素/數據等。它並沒有對片段本身做任何事情,但不必重新創建它來更新片段中的數據。 – NickL

+1

您也可以通過訪問((SectionsPagerAdapter)mViewPager.getAdapter())。的getItem當前片段(mViewPager.getCurrentItem())) – yesildal

-1
FragmentTabHost tabHost3 = (FragmentTabHost) findViewById(android.R.id.tabhost); 
String current_tab = tabHost3.getCurrentTabTag(); 
if (current_tab.equals("abc")) { 
    ClassFragment ni = (ClassFragment) getSupportFragmentManager().findFragmentByTag("abc"); 
    ni.refresh(); 
} 

試試這個,標籤下出現的所有東西都會再次執行。