2

我目前正在使用 FragmentStatePagerAdapter,我希望它具有“向上导航”功能 (DisplayHomeAsUpEnabled)。当我设置以下代码时,它显示为正确的导航返回按钮。但是当我点击它时什么也没有发生,你们能看到我在我的代码中做错了什么吗?按下“向上导航”按钮时我没有收到任何错误,我也没有收到编译器错误。

这是代码,它在“public boolean onOptionsItemSelected(MenuItem item) {”中,它在底部变得有趣。

public class ShareholdingDetailFragment extends FragmentActivity {

    final int NUM_ITEMS = Portfolio.getPortfolio().count();

    MyAdapter mAdapter;

    ViewPager mPager;
    Bundle extras; 


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        System.out.println(NUM_ITEMS + "e");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.shareholdingdetailview_fragment_wrapper);

        mAdapter = new MyAdapter(getSupportFragmentManager());

        mPager = (ViewPager)findViewById(R.id.pager);
        mPager.setAdapter(mAdapter);


    }

    public class MyAdapter extends FragmentStatePagerAdapter {
        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        public int getCount() {
            return NUM_ITEMS;
        }

        public Fragment getItem(int position) {
            return ShareholdingFragment.newInstance(position);
        }
    }

    public static class ShareholdingFragment extends Fragment {
        int mNum;

        static ShareholdingFragment newInstance(int num) {
            ShareholdingFragment f = new ShareholdingFragment();

            // Supply num input as an argument.
            Bundle args = new Bundle();
            args.putInt("num", num);
            f.setArguments(args);

            return f;
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mNum = getArguments() != null ? getArguments().getInt("num") : 1;
        }

        @SuppressLint({ "NewApi", "NewApi" })
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.shareholdingdetailview_fragment, container, false);

            /****************Setting the Display as home and it shows*******************/
            ActionBar bar = getActivity().getActionBar();
            bar.setDisplayHomeAsUpEnabled(true);
            /**********************************/

            return view;
        }

            @Override
    public boolean onOptionsItemSelected(MenuItem item) {


        switch (item.getItemId()) {
                case android.R.id.home://Not working
System.out.println("test");//This isn't printed out
                    Intent upIntent = new Intent(getActivity(), DetailShareHoldingActivity.class);
                    if (NavUtils.shouldUpRecreateTask(getActivity(), upIntent)) {


                    getActivity().finish();
                } else {
                    NavUtils.navigateUpTo(getActivity(), upIntent);
                }
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
    }
}
4

3 回答 3

6

尝试添加: setHasOptionsMenu(true); 在 ShareholdingFragment 的 onCreate() 中。

于 2012-12-13T20:08:49.640 回答
2

setHomeButtonEnabled(true)配置操作栏时添加API 级别 14 及更高级别。在 API 级别 11-13 上,主页按钮会自动启用。

于 2012-12-13T20:44:12.073 回答
2

如果您使用的是新的 Toolbar 和 ActionbarDrawerToggle。您可以直接分配 clickHandler 。对于具有此抽屉工具栏的活动,我实现了一个界面以在根目录下启用抽屉,

    @Override
    public void enableDrawer(boolean enable) {
        mDrawerToggle.setDrawerIndicatorEnabled(enable);
        mDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
// Pop fragment back stack or pass in a custom click handler from the fragment.
            }
        });
    }
于 2015-03-02T07:26:20.687 回答