public class Fragment_01 extends ListFragment {
String string[]={"Contact 1","Contact 2","Contact 3","Contact 4","Contact 5"};
public View onCreateView(LayoutInflater inflater, ViewGroup container ,Bundle saveInstanceState) {
View myview = inflater.inflate(R.layout.fragment_fragment_01, container, false);
ListView listView= (ListView) myview.findViewById(android.R.id.list);
ArrayAdapter<String> array = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1,string);
listView.setAdapter(array);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
FragmentManager fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
Fragment_03 f3 = new Fragment_03();
ft.replace(R.id.linearLayout2, f3);
ft.commit();
}
});
return myview;
}
}
0
A
回答
0
不要在另一個片段內更改片段。在活動中進行框架佈局。點擊列表視圖中的項目,通過接口將事件發送到活動,並在活動中替換片段。這是替換碎片的正確方法。
0
Fragment_03 f3 = new Fragment_03();
FragmentManager fragmentManager = ((FragmentActivity) getContext()).getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.content_frame, new Fragment_03())
.commit();
+0
仍然無法正常工作 –
0
//I am using this code without listView and this is working fine then why the other is not working ??
public class Fragment_01 extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container ,Bundle saveInstanceState){
View myview=inflater.inflate(R.layout.activity_fragment_01,container,false);
Button btn = (Button) myview.findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentManager fm =getFragmentManager();
FragmentTransaction ft =fm.beginTransaction();
Fragment_03 f3 = new Fragment_03();
ft.replace(R.id.linearLayout2,f3);
ft.commit();
}
});
return myview;
}
}
相關問題
- 1. 替換片段刪除另一片段
- 2. 片段列表視圖替換
- 3. 通知片段列表視圖時動作的另一片段
- 4. 替換片段視圖
- 5. 用另一個片段替換一個片段
- 6. 在viewpager中用另一個片段替換一個片段
- 7. 更換片段與另一片段顯示兩個片段
- 8. 帶有列表視圖的片段沒有被帶有textview的片段替換
- 9. 使用ImageView從另一個片段打開新片段onClick
- 10. 嘗試用另一個片段替換main_activity片段onbuttonclick
- 11. 如何用另一個片段替換片段?
- 12. 如何用另一個片段替換自定義片段?
- 13. 將一個片段替換爲另一個片段
- 14. 如何從另一片段的onTouch()內部替換片段?
- 15. ActionBar選項卡 - 用細節片段替換列表片段
- 16. 在另一個片段上顯示的列表視圖
- 17. 替換ListView片段與細節片段上的項目點擊
- 18. Android將當前片段替換爲另一個片段
- 19. 用子片段替換主要片段
- 20. 無法從具有列表視圖的片段導航到另一個片段
- 21. 使用片段列表視圖
- 22. 可擴展列表視圖與片段
- 23. OnClick替換圖片
- 24. 替換ViewPager與新片段
- 25. 使用意圖調用另一個片段的片段
- 26. 如何替換片段內的片段?
- 27. 更換片段與活動組內的另一個片段
- 28. 更新一個列表視圖片段
- 29. 更換一個片段與另一個
- 30. 從另一個片段調用片段
什麼是你的問題? – Bobby