2012-07-20 62 views
2

在我的FragmentActivity中,我有一個片段。該片段從服務器加載數據。 我想我們用戶點擊刷新按鈕,然後我調用片段中的方法並執行刷新過程。Android,如何重新啓動/刷新從FragmentActivty片段?

我在FragmentActivity中創建了一個接口,並在用戶單擊刷新按鈕時設置其變量。該代碼是這樣的:

public class MainScreen extends FragmentActivity { 
    public interface OnRefreshSelected { 
     public void refreshFragment(boolean flag); 
    } 

    private OnRefreshSelected onRefreshSelected; 

    @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 

      Log.i(TAG, "Try to create MainScreen..."); 

      setContentView(R.layout.activity_main); 

     onRefreshSelected = (OnRefreshSelected) MainScreen.this; 
    } 

    @Override 
     public boolean onCreateOptionsMenu(Menu menu) { 
      getMenuInflater().inflate(R.menu.activity_main, menu); 
      return true; 
     } 

     @Override 
     public boolean onOptionsItemSelected(MenuItem item) { 
      switch (item.getItemId()) { 
        case R.id.menu_refresh: 
         onRefreshSelected.refreshFragment(true); 
         return true; 
        default: 
         return super.onOptionsItemSelected(item); 
     } 
    } 
} 

而在片段I類實現該接口:

public class CategoryFragment extends Fragment implements OnRefreshSelected { 
    @Override 
    public void refreshFragment(boolean flag) { 
     Log.i(TAG, "refresh requested. Try to reload data for this fragment..."); 

     getData(); 
    } 
} 

當運行時,應用程序崩潰和logcat的示出這樣的信息:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.astro.recipe.activities/com.astro.recipe.activities.MainScreen}: java.lang.ClassCastException: com.astro.recipe.activities.MainScreen 

和指向此行:

onRefreshSelected = (OnRefreshSelected) MainScreen.this; 

從主機訪問片段方法的最佳方式是什麼? 任何建議,將不勝感激。由於

+0

我能用AppCompatActivity代碼您的活動。 – 2015-12-11 07:27:15

回答

4

執行此

onRefreshSelected = (OnRefreshSelected) MainScreen.this; 

您在onRefreshSelected領域分配您的活動,試圖將它轉換爲你的界面,但你的活動不實現接口,這就是爲什麼提出ClassCastException

而是使用類似這樣

@Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
       case R.id.menu_refresh: 
        CategoryFragment fragment = (CategoryFragment) getFragmentManager() 
         .findFragmentById(R.id.category_fragment); 
        if (fragment != null) { 
         fragment.getData(); 
        } 
        return true; 
       default: 
        return super.onOptionsItemSelected(item); 
    } 

您的活動可以通過獲取FragmentManager的片段參考,使用findFragmentById()findFragmentByTag()調用片段的方法。

+0

謝謝,但我更喜歡使用接口。因此,我更換onRefreshSelected =(OnRefreshSelected)MainScreen.this;通過onRefreshSelected = new TopRecipesFragment();和onRefreshSelected = new CategoryFragment();現在它工作正常。再次感謝。 – Hesam 2012-07-24 04:52:18

+0

請參閱我的相關問題:http://stackoverflow.com/questions/24833912/refresh-fragment-ui-from-fragmentactivity – 2014-07-19 07:45:59

-1

我同意biegleux ...但我的建議是

public class CategoryFragment extends Fragment{ 

    private static CategoryFragment categoryFrag; 

    private CategoryFragment(){} 

    public static CategoryFragment getInstance(){ 
     if(null == categoryFrag){ 
      categoryFrag = new CategoryFragment(); 
     } 

     return categoryFrag; 
    } 

    public void getData(){ 
     /*** Screen Refresh code****/ 
    } 
} 

我的時候,他選擇選項

@Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
       case R.id.menu_refresh: 
        CategoryFragment fragment = CategoryFragment.getInstance(); 
        fragment.getData(); 

        return true; 
       default: 
        return super.onOptionsItemSelected(item); 
    }