Android 如何在使用ActionBar.NAVIGATION\u MODE\u选项卡时更改单击滑动菜单项时的片段

Android 如何在使用ActionBar.NAVIGATION\u MODE\u选项卡时更改单击滑动菜单项时的片段,android,slidingmenu,Android,Slidingmenu,我搜索了一下,但找不到问题的确切答案。 我有一个使用ActionBar.NAVIGATION\u MODE\u选项卡的MainActivity应用程序,它允许主寻呼机支持可滑动的选项卡片段。 当我打开菜单(滑动菜单)并单击某个项目时。我想要一个新的片段(或布局)替换主分页器布局(包含tabs-fragment)。 例如,打开应用程序后,我们可以看到4个选项卡:短信、通话、日程安排、新闻。 打开滑动菜单,我们有3个项目:设置,日历,关于。 单击设置,新内容将替换旧内容。现在我们只看到一页。 我的M

我搜索了一下,但找不到问题的确切答案。 我有一个使用ActionBar.NAVIGATION\u MODE\u选项卡的MainActivity应用程序,它允许主寻呼机支持可滑动的选项卡片段。 当我打开菜单(滑动菜单)并单击某个项目时。我想要一个新的片段(或布局)替换主分页器布局(包含tabs-fragment)。 例如,打开应用程序后,我们可以看到4个选项卡:短信、通话、日程安排、新闻。 打开滑动菜单,我们有3个项目:设置,日历,关于。 单击设置,新内容将替换旧内容。现在我们只看到一页。 我的MainActivity.java代码:

    package com.giacngumo.SMS_Call_Calendar_Tracking;

import android.util.Log;
import com.actionbarsherlock.app.ActionBar;
import android.os.Bundle;
import android.support.v4.app.*;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.actionbarsherlock.internal.app.ActionBarImpl;
import com.actionbarsherlock.internal.app.ActionBarWrapper;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.giacngumo.SMS_CallLog_Calendar_Tracking.SMS_CallLog_Calendar_Tracking.R;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class MainActivity extends SlidingFragmentActivity {

    AppSectionsPagerAdapter mAppSectionsPagerAdapter;
    ViewPager mViewPager;
    protected ListFragment mFrag;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        try {
            setTitle(R.string.title_bar_slide);

            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            // set the Behind View
            setBehindContentView(R.layout.menu_frame);
            FragmentTransaction t = this.getSupportFragmentManager().beginTransaction();
            mFrag = new MenuListFragment();
            t.replace(R.id.menu_frame, mFrag);
            t.commit();

            // configure the SlidingMenu
            final SlidingMenu slidingMenu = getSlidingMenu();
            slidingMenu.setShadowWidthRes(R.dimen.shadow_width);
            slidingMenu.setShadowDrawable(R.drawable.shadow);
            slidingMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
            slidingMenu.setFadeDegree(0.35f);
            slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);

            // Set up the action bar.
            final ActionBar actionBar = getSupportActionBar();
            //pre-ICS
            if (actionBar instanceof ActionBarImpl) {
                enableEmbeddedTabs(actionBar);

                //ICS and forward
            } else if (actionBar instanceof ActionBarWrapper) {
                try {
                    Field actionBarField = actionBar.getClass().getDeclaredField("mActionBar");
                    actionBarField.setAccessible(true);
                    //enableEmbeddedTabs(actionBarField.get(actionBar));
                } catch (Exception e) {
                    //Log.e(TAG, "Error enabling embedded tabs", e);
                }
            }
            // Specify that the Home/Up button should not be enabled, since there is no hierarchical
            // parent.
            actionBar.setHomeButtonEnabled(true);
            actionBar.setIcon(R.drawable.hamburger_icon);

            // Specify that we will be displaying tabs in the action bar.
            actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
            actionBar.setDisplayShowTitleEnabled(false);

            // Create the adapter that will return a fragment for each of the four primary sections
            // of the app.
            mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());
            // Set up the ViewPager, attaching the adapter and setting up a listener for when the
            // user swipes between sections.
            mViewPager = (ViewPager) findViewById(R.id.main_pager);
            mViewPager.setAdapter(mAppSectionsPagerAdapter);
            mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                @Override
                public void onPageSelected(int position) {
                    // When swiping between different app sections, select the corresponding tab.
                    // We can also use ActionBar.Tab#select() to do this if we have a reference to the
                    // Tab.
                    actionBar.setSelectedNavigationItem(position);
                    switch (position) {
                        case 0:
                            slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
                            break;
                        default:
                            slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
                    }
                }
            });
            // For each of the sections in the app, add a tab to the action bar.
            // Create a tab with text corresponding to the page title defined by the adapter.
            // Also specify this Activity object, which implements the TabListener interface, as the
            // listener for when this tab is selected.
            actionBar.addTab(
                    actionBar.newTab()
                            .setCustomView(R.layout.tab_sms)
                            .setTabListener(tabListener));
            actionBar.addTab(
                    actionBar.newTab()
                            .setCustomView(R.layout.tab_calllog)
                            .setTabListener(tabListener));
            actionBar.addTab(
                    actionBar.newTab()
                            .setCustomView(R.layout.tab_schedule)
                            .setTabListener(tabListener));
            actionBar.addTab(
                    actionBar.newTab()
                            .setCustomView(R.layout.tab_news)
                            .setTabListener(tabListener));
        } catch (Exception ex) {
            Log.e("error", ex.getMessage());
        }
    }

    //helper method
    private void enableEmbeddedTabs(Object actionBar) {
        try {
            Method setHasEmbeddedTabsMethod = actionBar.getClass().getDeclaredMethod("setHasEmbeddedTabs", boolean.class);
            setHasEmbeddedTabsMethod.setAccessible(true);
            setHasEmbeddedTabsMethod.invoke(actionBar, true);
        } catch (Exception e) {
            //Log.e(TAG, "Error marking actionbar embedded", e);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                toggle();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getSupportMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    ActionBar.TabListener tabListener = new ActionBar.TabListener() {
        public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
            // When the tab is selected, switch to the
            // corresponding page in the ViewPager.
            mViewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
        }

        @Override
        public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
        }
    };

    /**
     * A {@link android.support.v4.app.FragmentPagerAdapter} that returns a fragment corresponding to one of the primary
     * sections of the app.
     */
    public static class AppSectionsPagerAdapter extends FragmentPagerAdapter {

        public AppSectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            Fragment fragment = null;
            Bundle args = new Bundle();
            if (i == 0) {
                // sms case
                fragment = new Sms_ListFragment();
            } else if (i == 1) {
                // call log case
                fragment = new Call_ListFragment();
            } else if (i == 2) {
                // schedule case
                fragment = new Call_ListFragment();
            } else if (i == 3) {
                fragment = new Call_ListFragment();
            }
            return fragment;
        }

        @Override
        public int getCount() {
            return 4;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0:
                    return "SMS";
                case 1:
                    return "Call Log";
                case 2:
                    return "Schedule";
                case 3:
                    return "News";
                default:
                    return "Section " + (position + 1);
            }
        }
    }

    public void switchContent(Fragment fragment) {
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.main_pager, fragment)
                .commit();
        getSlidingMenu().showContent();
    }
}
以及处理滑动菜单项的MenuListFragment的代码: package com.giacngumo.SMS_Call_Calendar_Tracking

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.giacngumo.SMS_CallLog_Calendar_Tracking.SMS_CallLog_Calendar_Tracking.R;

public class MenuListFragment extends ListFragment {

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.list, null);
    }

    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        MenuListAdapter adapter = new MenuListAdapter(getActivity());

        adapter.add(new MenuListRowItem("Backup into Calendar", R.drawable.ic_calendar));
        adapter.add(new MenuListRowItem("Backup into Gmail", R.drawable.ic_action_mail));
        adapter.add(new MenuListRowItem("Auto Sync", android.R.drawable.ic_popup_sync));
        adapter.add(new MenuListRowItem("Advanced Filter", R.drawable.filter_data_48));
        adapter.add(new MenuListRowItem("Advanced Setting", R.drawable.schedule_icon));
        adapter.add(new MenuListRowItem("Restore", R.drawable.ic_restore));
        adapter.add(new MenuListRowItem("Tutorial", R.drawable.ic_launcher));

        setListAdapter(adapter);
    }

    private class MenuListRowItem {
        public String tag;
        public int iconRes;
        public MenuListRowItem(String tag, int iconRes) {
            this.tag = tag; 
            this.iconRes = iconRes;
        }
    }

    public class MenuListAdapter extends ArrayAdapter<MenuListRowItem> {

        public MenuListAdapter(Context context) {
            super(context, 0);
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(getContext()).inflate(R.layout.menu_row, null);
            }
            ImageView icon = (ImageView) convertView.findViewById(R.id.menu_row_icon);
            icon.setImageResource(getItem(position).iconRes);
            TextView title = (TextView) convertView.findViewById(R.id.menu_row_title);
            title.setText(getItem(position).tag);
            //icon.setBackgroundColor(Color.BLACK);
            //title.setBackgroundColor(Color.BLACK);
            title.setTextColor(Color.WHITE);

            return convertView;
        }
    }
    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        //l.setItemChecked(position, !mAdapter.isPositionChecked(position));
        //Toast.makeText(getActivity(), "click on "+position, Toast.LENGTH_SHORT).show();
        FragmentManager manager = getFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        Fragment frag = null;
        switch (position) {
            case 0:
                //frag = new CalendarFragment();
                frag = new ColorFragment(R.color.green);
                break;

            case 1:
                //frag = new FormFragment();
                break;

            case 2:
                //frag = new CompFragment();
                break;
            case 3:
                //frag = new CompFragment();
                break;
            case 4:
                //frag = new CompFragment();
                break;
            case 5:
                //frag = new CompFragment();
                break;
            case 6:
                //frag = new CompFragment();
                break;
            default:
                //frag = new ContactFragment();
                break;
        }
        if (frag != null)
            switchFragment(frag);
    }
    // the meat of switching the above fragment
    private void switchFragment(Fragment fragment) {
        if (getActivity() == null)
            return;

        if (getActivity() instanceof MainActivity) {
            MainActivity mainActivity = (MainActivity) getActivity();
            mainActivity.switchContent(fragment);
        }/* else if (getActivity() instanceof ResponsiveUIActivity) {
            ResponsiveUIActivity ra = (ResponsiveUIActivity) getActivity();
            ra.switchContent(fragment);
        }*/
    }
}
导入android.content.Context;
导入android.graphics.Color;
导入android.os.Bundle;
导入android.support.v4.app.Fragment;
导入android.support.v4.app.FragmentManager;
导入android.support.v4.app.FragmentTransaction;
导入android.support.v4.app.ListFragment;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.*;
导入com.giacngumo.SMS_CallLog_Calendar_Tracking.SMS_CallLog_Calendar_Tracking.R;
公共类MenuListFragment扩展了ListFragment{
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
返回充气机。充气(R.layout.list,空);
}
已创建ActivityState上的公共无效(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
MenuListAdapter=new MenuListAdapter(getActivity());
add(新的MenuListRowItem(“备份到日历”,R.drawable.ic_日历));
add(新的MenuListRowItem(“备份到Gmail”,R.drawable.ic_action_mail));
add(新的MenuListRowItem(“自动同步”,android.R.drawable.ic_popup_Sync));
adapter.add(新菜单strowitem(“高级过滤器”,R.drawable.Filter_data_48));
adapter.add(新菜单strowitem(“高级设置”,R.drawable.schedule_图标));
add(新的MenuListRowItem(“Restore”,R.drawable.ic_Restore));
add(新的MenuListRowItem(“教程”,R.drawable.ic_启动器));
setListAdapter(适配器);
}
私有类MenuListRowItem{
公共字符串标签;
公共国际学院;
公共菜单项(字符串标记,int-iconRes){
this.tag=tag;
this.iconRes=iconRes;
}
}
公共类MenuListAdapter扩展了ArrayAdapter{
公共菜单StatAdapter(上下文){
超级(上下文,0);
}
公共视图getView(int位置、视图转换视图、视图组父视图){
if(convertView==null){
convertView=LayoutInflater.from(getContext()).flate(R.layout.menu_行,null);
}
ImageView图标=(ImageView)convertView.findViewById(R.id.menu\u row\u图标);
icon.setImageResource(getItem(position).iconRes);
TextView title=(TextView)convertView.findViewById(R.id.menu\u row\u title);
title.setText(getItem(position.tag);
//图标.setBackgroundColor(颜色.黑色);
//标题.背景色(颜色.黑色);
title.setTextColor(Color.WHITE);
返回视图;
}
}
@凌驾
public void onListItemClick(列表视图l、视图v、整数位置、长id){
//l、 setItemChecked(位置,!mAdapter.isPositionChecked(位置));
//Toast.makeText(getActivity(),“单击”+位置,Toast.LENGTH\u SHORT.show();
FragmentManager=getFragmentManager();
FragmentTransaction=manager.beginTransaction();
片段frag=null;
开关(位置){
案例0:
//frag=新日历片段();
frag=新的颜色片段(R.color.green);
打破
案例1:
//frag=新的FormFragment();
打破
案例2:
//frag=新的CompFragment();
打破
案例3:
//frag=新的CompFragment();
打破
案例4:
//frag=新的CompFragment();
打破
案例5:
//frag=新的CompFragment();
打破
案例6:
//frag=新的CompFragment();
打破
违约:
//frag=新的ContactFragment();
打破
}
如果(frag!=null)
开关碎片(frag);
}
//切换上述片段的肉
私有void开关片段(片段片段){
如果(getActivity()==null)
返回;
如果(getActivity()实例为MainActivity){
MainActivity MainActivity=(MainActivity)getActivity();
mainActivity.switchContent(片段);
}/*else if(getActivity()instanceof ResponsiveUIActivity){
ResponsiveUIActivity ra=(ResponsiveUIActivity)getActivity();
ra.含量(片段);
}*/
}
}