片段列表视图替换

时间:2016-05-31 22:03:35

标签: android listview android-fragments

当我替换片段Listview位置片段Listview时 我在照片中遇到了麻烦,但我无法修复它。 我需要解决它,PLZ。 UI_screenshot

Code

@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        final String[] titel = {"asdasd", "asda", "bdfg", "asdqweqwe"};
        int idimg = R.mipmap.icon_main_list;
        MyArrayAdapter adapter = new MyArrayAdapter(getActivity(), titel, idimg);
        setListAdapter(adapter);
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        switch (position) {
            case 0:
                Anmalz anmalz=new Anmalz();
                Main_List_Fragment main_list_fragment=new Main_List_Fragment();
               FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.fragment, anmalz);
                fragmentTransaction.remove(main_list_fragment).commit();

                break;

1 个答案:

答案 0 :(得分:0)

将您的onCreateView()更改为 -

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.main_fragment_list, container, false);
    }

此外,在onListItemClick()中,您不应该反复删除/替换相同的片段。您应该只刷新列表适配器。