如何在 Master/Detail 片段中获取支持操作栏以设置 NAVIGATION_MODE_TABS?

How to get Support Action Bar inside a Master/Detail Fragment to set NAVIGATION_MODE_TABS?

现在我有一个典型的 master/detail 模式,在详细信息区域内我应该显示某种选项卡结构,但使用 ViewPager 以实现滑动功能以在这些选项卡之间切换。

在详细信息区域内的单窗格模式(使用设备时)中,我部署了 AreaDetailActivity。在此 activity 中,我调用 supportActionBar (v7),设置导航模式,并设置选项卡,如下所示:

public class AreaDetailActivity extends ActionBarActivity implements ActionBar.TabListener {    

    private ViewPagerAdapter viewPagerAdapter;
    private ViewPager viewPager;    

    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView( R.layout.view_pager );

        String idTipoAreaActual = getIntent().getStringExtra(  AreaDetailFragment.ARG_ITEM_ID );

        final ActionBar actionBar = getSupportActionBar();
        actionBar.setNavigationMode( ActionBar.NAVIGATION_MODE_TABS );

        //Adapter that returns the selected fragment
        viewPagerAdapter = new ViewPagerAdapter( this, idTipoAreaActual, getSupportFragmentManager() );

        viewPager = (ViewPager) findViewById( R.id.view_pager );
        viewPager.setAdapter( viewPagerAdapter );

        viewPager.setOnPageChangeListener( new ViewPager.SimpleOnPageChangeListener(){
            @Override
            public void onPageSelected( int position ){
                actionBar.setSelectedNavigationItem( position );
            }
        });

        actionBar.addTab( actionBar.newTab().setText("Tab1").setTabListener(this) );
        actionBar.addTab( actionBar.newTab().setText("Tab2").setTabListener(this) );
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction fragmentTransaction) {
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction fragmentTransaction) {
        viewPager.setCurrentItem( tab.getPosition() );
    }

    @Override
    public void onTabUnselected(Tab arg0, FragmentTransaction arg1){        
    }
}

上面的代码工作得很好,但我需要一种方法来在双窗格模式(实际上是平板电脑屏幕)中实现相同的功能。在 Two-Pane 中,细节区域部署了一个片段,因此我需要调用支持操作栏并使用 ViewPager 实现滑动功能,但是当我将 ActionBar 转换为 AreaDetailActivity(从 ActionBarActivity 扩展)时,它显示了一个 Casting错误。这是我在双窗格模式下显示的 AreaDetailFragment:

public class AreaDetailFragment extends Fragment implements ActionBar.TabListener {

    private Activity activity;
    private ViewPagerAdapter viewPagerAdapter;
    private ViewPager viewPager;

    public AreaDetailFragment( Activity activity ) {
        this.activity = activity;
    }   

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu( true );
    }

    @Override 
    public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater){
        super.onCreateOptionsMenu(menu, menuInflater);
        menuInflater.inflate(R.menu.ccmaction_bar, menu);
    }   

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

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState){
        super.onViewCreated(view, savedInstanceState);

        String idTipoAreaActual = getArguments().getString( AreaDetailFragment.ARG_ITEM_ID );

        final ActionBar actionBar = ( (AreaDetailActivity) getActivity()).getSupportActionBar();
        actionBar.setNavigationMode( ActionBar.NAVIGATION_MODE_TABS );

        viewPagerAdapter = new ViewPagerAdapter( getActivity().getApplicationContext(), idTipoAreaActual, getChildFragmentManager() );

        viewPager = (ViewPager) view.findViewById( R.id.view_pager );
        viewPager.setAdapter( viewPagerAdapter );       

        viewPager.setOnPageChangeListener( new ViewPager.SimpleOnPageChangeListener(){
            @Override
            public void onPageSelected( int position ){
                actionBar.setSelectedNavigationItem( position );
            }
        });  
    } 

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction fragmentTransaction) {
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction fragmentTransaction) {
        viewPager.setCurrentItem( tab.getPosition() );
    }

    @Override
    public void onTabUnselected(Tab arg0, FragmentTransaction arg1){        
    }
}

有什么想法吗???感谢您的宝贵时间!!!

SWIPE/TAP/PAGES 的精彩教程:http://indyvision.net/2014/08/android-tutorial-screen-with-tabs-and-swipe-part-i/

关于 ACTION BAR 的技术信息:http://developer.android.com/guide/topics/ui/actionbar.html

关于 SWIPE/TAP/PAGES 的好教程:http://indyvision.net/2014/08/android-tutorial-screen-with-tabs-and-swipe-part-i/

关于 ACTION BAR 的技术信息:http://developer.android.com/guide/topics/ui/actionbar.html