其他碎片没有显示

时间:2015-07-06 10:29:33

标签: android android-fragments

我一直在寻找fragments,但问题仍然存在,我无法让它发挥作用。

我的问题是,当我将repeat_entry添加到菜单中以显示另一个片段时,该片段未显示,但是当我点击action_settings时,该片段的片段正在显示。

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.setCustomAnimations(android.R.animator.fade_in,android.R.animator.fade_out);

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            selectTabToShow(String.valueOf(R.id.action_search));
            CategorySettings fragemnt = (CategorySettings) getFragmentManager().findFragmentById(R.id.categorySettings);
            fragemnt.refresCategoryList();
            Fragment fragment = new CategorySettings();
            ft.add(R.id.category_cont,fragment,"asdf");
            ft.commit();
            return true;
        }else if(id == R.id.repeat_entry){

            Toast.makeText(MainActivity.this,"ssdf sdf",Toast.LENGTH_SHORT).show();
            Fragment fragment = new RepeatEntry();
            ft.add(R.id.category_cont,fragment,"repeat tag");
            ft.commit();
            Toast.makeText(MainActivity.this,"ssdf sdf",Toast.LENGTH_SHORT).show();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

截至目前,这是我的RepeatEntry片段

public class RepeatEntry extends Fragment{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState){
        View v = inflater.inflate(R.layout.repeat_entry,container,false);

        return v;
    }
}

请帮助我们,我是新手。

1 个答案:

答案 0 :(得分:0)

尝试替换

ft.replace(R.id.category_cont,fragment,"repeat tag");