导航抽屉和碎片

时间:2014-08-10 16:24:47

标签: android android-fragments

我的导航抽屉工作正常。现在我创建了片段:

public class LabourFragment extends Fragment {
    private ListView listViewLabour;
    private String[] resourcesList;


    public LabourFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //set adapter
        //TODO: change later to proper resource pull from server

        //have to use getActivity() as this is fragment and findViewById works from activity only

        ListView listView = (ListView) getActivity().findViewById(R.id.drawerList);
        listView.setAdapter(new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_1, resourcesList));
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_labour, container, false);

    }


}

现在我从主要活动调用片段:

public void selectItem(int position) {
    listView.setItemChecked(position, true);
    setTitle(getResources().getTextArray(R.array.drawerMenuList)[position]);

    FragmentManager fragmentManager = getFragmentManager();
    //TODO: open proper acvtivity
    switch (position){ //positions reflects menu in drawer
        case 0:
            LabourFragment fragment = new LabourFragment();
            fragmentManager.beginTransaction()
                    .replace(R.id.mainContentFragment, fragment)
                    .commit();
            break;


    }

并收到错误消息:

错误:(100,25)错误:找不到合适的替换方法(int,LabourFragment) 方法FragmentTransaction.replace(int,Fragment)不适用 (实际参数LabourFragment无法通过方法调用转换转换为Fragment)

尝试将LabourFragment投射到Fragment但没有运气。 我做错了什么? 谢谢。

0 个答案:

没有答案