fragment 跳转另一个fragment
public class NoteAllFragment extends Fragment { NoteAllFragment noteAllFragment = this; CircleNoteFragment circleNoteFragment = new CircleNoteFragment(); MyNoteFragment myNoteFragment = new MyNoteFragment(); ListView noteall_listview; @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_noteall, container, false); } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); } @Override public void onResume() { loadData(); super.onResume(); } private void loadData() { noteall_listview = getActivity().findViewById(R.id.noteall_listview); List<String> list = new ArrayList<>(); DataBaseHelper dataBaseHelper = DataBaseHelper.getInstance(getContext()); Dao<Note,Integer> notes = null; try { notes = dataBaseHelper.getDao(Note.class); for(Note note : notes.queryForAll()){ list.add(note.getTitle()); } } catch (SQLException e) { e.printStackTrace(); } ArrayAdapter<String> arrayAdapter = new ArrayAdapter(getContext(),android.R.layout.simple_list_item_1,list); noteall_listview.setAdapter(arrayAdapter); noteall_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { getActivity().getSupportFragmentManager() .beginTransaction() .hide(noteAllFragment).addToBackStack(null) .show(circleNoteFragment) .hide(myNoteFragment) .commit(); } }); } }
在item点击事件中,从当前fragment 到 另一个fragment 为空白,这是为什么?
0
收起
正在回答
2回答
把fragment切换中的addToBackStack(null)操作去掉,show()和hide()处理时fragment是存在的,无需添加addToBackStack(null),addToBackStack(null)一般是对replace()的操作。
irista23
2018-07-24 10:11:33
执行onCreate()时你需要先add()进来,之后再调用show()或hide()
this.getSupportFragmentManager() .beginTransaction() .add(R.id.container_content,mMainFragment) .add(R.id.container_content,mFindFragmenr) .hide(mFindFragmenr) .add(R.id.container_content,mMeFragment) .hide(mMeFragment) .commit();
Android从界面到数据存储2018版
- 参与学习 504 人
- 提交作业 1003 份
- 解答问题 1044 个
本专题是专为有Java语言基础想入坑Android开发的同学们准备的。从AndroidUI基础一直到网络,数据存储,一步到位入门Android开发。如果你会Java,那就不要犹豫了~ ~
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星