我有一個簡單的應用程序,有兩個片段,當在橫向模式下,兩個片段並排顯示,並在肖像中顯示片段A,然後如果他們選擇一個選項,啓動一個活動顯示片段B.我的問題是當我處於肖像模式並顯示片段B時,如果用戶選擇了一個菜單選項我想刷新或重繪與片段B相關聯的視圖,並且無法理解如何使其工作。我嘗試了getView方法和getLayoutInflater方法,但屏幕不會更改,因爲我認爲我正在創建一個新視圖。我也試圖獲得對片段的引用A我認爲我可以調用它的例程來構建一個新的片段並替換片段B,但由於它沒有被顯示而無法獲取對它的引用。我真正需要做的只是強制再次調用onCreateView方法來膨脹新視圖,但我嘗試了幾種方法來嘗試這樣做,但無法再次調用onCreateView。任何想法如何?安卓力量片段重建查看
14
A
回答
1
如果用戶選擇我要刷新或重繪與片段B關聯,並且不知道如何使這項工作
在onOptionsItemSelected()
查看菜單選項,有活動電話該片段上的一個方法使其用新內容更新其小部件。或者,讓活動執行FragmentTransaction
來替換片段(如果片段最初是通過FragmentTransaction
設置的)。
9
您將要執行FragmentTransaction
並使用replace()
方法
這應該不是太難的事,但答案將取決於您的菜單的位置(即是你onOptionsItemSelected()
再打你的內父活動還是在片段A/B?)。
假設爲了簡單起見,您的菜單實現和onOptionsItemSelected()
位於父級活動中,並且您希望在選擇menu_option1的情況下重新塑造片段。這將是這個樣子:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
//...
switch (item.getItemId()) {
case R.id.menu_option1:
//do something
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment newFragment = new YourFragmentClass();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.your_fragment_id, newFragment);
transaction.addToBackStack(null);
transaction.commit();
return true;
case R.id.menu_option2:
//do something else;
return true;
default:
return super.onOptionsItemSelected(item);
}
}
另外,如果你的菜單是你的一個片段(它應該是更多的可重用代碼的緣故)的孩子,再一個方法是要求主機活動實現由Fragment定義的接口,可以將其用作回調。 而在片段類中的onOptionsItemSelected()
回調中,您只需調用此回調方法即可。
儘管聽起來像一口滿口,但你只需要做幾件事。假裝這是你的片段類
public static class FragmentA extends ListFragment {
OnSelectedListener mListener;
// Container Activity must implement this interface
public interface OnSelectedListener {
public void onSelected();
}
...
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
//This is to ensure that the Activity has implemented the interface we set up above
try {
mListener = (OnSelectedListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement OnSelectedListener");
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
//...
switch (item.getItemId()) {
case R.id.menu_option1:
//do something
getActivity().onSelected();
return true;
case R.id.menu_option2:
//do something else;
return true;
default:
return super.onOptionsItemSelected(item);
}
}
...
}
然後在活動中,你會看到類似這樣的:
public class MainActivity extends Activity implements FragmentA.onSelectedListener{
...
public void onSelected(){
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment newFragment = new YourFragmentClass();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.your_fragment_id, newFragment);
transaction.addToBackStack(null);
transaction.commit();
}
}
相關問題
- 1. 安卓力量當屏幕更改方向時重新繪製片段
- 2. 安卓:從片段
- 3. 安卓重力問題
- 4. 安卓力量關閉seekbar
- 5. 安卓:錯誤創建YoutTubePlayerView內片段
- 6. 安卓片段和活動
- 7. 安卓:achartEngine與片段
- 8. 安卓與兩個片段
- 9. 安卓:在類片段
- 10. 片段頂部安卓ActionBar
- 11. 安卓:除了片段
- 12. 刷新片段 - 安卓
- 13. 安卓:使用GLSurfaceView片段
- 14. 安卓:ExpandAnimation隨着片段
- 15. 安卓:傳遞片段
- 16. 安卓:動態創建的佈局中查找片段
- 17. 查看鈦安卓日曆
- 18. 片段安卓:片段無法實例片段
- 19. 安卓重力CENTER_HORIZONTAL linearlayot不起作用
- 20. 「安卓主題」 屬性,忽略重力
- 21. Showcase查看文本重力
- 22. 在查看預覽器中總是重新創建「查看」的片段
- 23. 安卓 - 按鈕不會切換片段
- 24. 安卓SupportMapFragment錯誤膨脹片段類
- 25. 安卓:保持活動/片段狀態
- 26. 安卓:使用savedInstanceState與片段錯誤
- 27. 安卓入門白屏片段交易
- 28. 安卓自定義日曆片段
- 29. 安卓谷歌地圖在片段
- 30. 安卓android片段過渡動畫
當你更換R.id.your_fragment_id,你是假設它在XML聲明。但是如果我的原始片段是以編程方式構建的呢? –
@IgorGanapolsky這將是該片段容器的ID – dmSherazi