如何在Android中使用带NavigationDrawer的ViewPager

如何在Android中使用带NavigationDrawer的ViewPager,android,android-viewpager,android-tabhost,android-navigation,Android,Android Viewpager,Android Tabhost,Android Navigation,我试图在同一活动中使用NavigationDrawer和ViewPager,但它无法正常工作。我遵循我想有三个标签,每一个将显示一个不同的可扩展列表,如果它被选中。目前,NavigationDrawer和ExpandableList运行良好,但是TabBar和DeViewPager无法管理滑动和单击事件 有人能帮我吗?我已经阅读和寻找了很久 我的主要活动是这样的: public class HomeActivity extends FragmentActivity implements Acti

我试图在同一活动中使用NavigationDrawer和ViewPager,但它无法正常工作。我遵循我想有三个标签,每一个将显示一个不同的可扩展列表,如果它被选中。目前,NavigationDrawer和ExpandableList运行良好,但是TabBar和DeViewPager无法管理滑动和单击事件

有人能帮我吗?我已经阅读和寻找了很久

我的主要活动是这样的:

public class HomeActivity extends FragmentActivity implements ActionBar.TabListener{
    // ExpandableList
    private ArrayList<String> parentItems = new ArrayList<>();
    private ArrayList<Object> childItems = new ArrayList<>();

    // SlideMenu
    private String[] mPlanetTitles;
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private CharSequence mDrawerTitle;
    private CharSequence mTitle;
    private ActionBarDrawerToggle mDrawerToggle;

    // Declaraciones Pager
    AppSectionsPagerAdapter mAppSectionsPagerAdapter;
    ViewPager mViewPager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        ///// EXPANDABLELISTVIEW PART /////
        ExpandableListView expandableList = (ExpandableListView) findViewById(R.id.expandableListView1);
        expandableList.setGroupIndicator(null);
        expandableList.setClickable(true);
        setGroupParents();
        setChildData();
        MyExpandableAdapter adapter = new MyExpandableAdapter(parentItems, childItems);
        adapter.setInflater((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE), this);
        expandableList.setAdapter(adapter);
        expandableList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                return false;
            }
        });

        /////// ACTIONBAR PART///////////
        ActionBar mActionBar = getActionBar();
        mActionBar.setDisplayShowHomeEnabled(false);
        mActionBar.setDisplayShowTitleEnabled(false);
        LayoutInflater mInflater = LayoutInflater.from(this);
        View mCustomView = mInflater.inflate(R.layout.custom_actionbar, null);
        TextView mTitleTextView = (TextView) mCustomView.findViewById(R.id.title_text);
        mTitleTextView.setText("Título");
        mActionBar.setCustomView(mCustomView);
        mActionBar.setDisplayShowCustomEnabled(true);

        /////// NAVIGATION DRAWER PART////////
        mTitle = mDrawerTitle = getTitle();
        mPlanetTitles = getResources().getStringArray(R.array.prueba_array);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);

        // set a custom shadow that overlays the main content when the drawer opens
        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
        // set up the drawer's list view with items and click listener
        mDrawerList.setAdapter(new ArrayAdapter<String>(this,
                R.layout.drawer_list_item, mPlanetTitles));
        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

        // enable ActionBar app icon to behave as action to toggle nav drawer
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);

        // ActionBarDrawerToggle ties together the the proper interactions
        // between the sliding drawer and the action bar app icon
        mDrawerToggle = new ActionBarDrawerToggle(
                this,                  /* host Activity */
                mDrawerLayout,         /* DrawerLayout object */
                R.drawable.ic_drawer,  /* nav drawer image to replace 'Up' caret */
                R.string.drawer_open,  /* "open drawer" description for accessibility */
                R.string.drawer_close  /* "close drawer" description for accessibility */
        ) {
            public void onDrawerClosed(View view) {
                //getActionBar().setTitle(mTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }

            public void onDrawerOpened(View drawerView) {
                //getActionBar().setTitle(mDrawerTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);
//        if (savedInstanceState == null) {
//            selectItem(0);
//        }

        ////////////PART VIEWPAGER///////////////

        // Create the adapter that will return a fragment for each of the three primary sections
        // of the app.
        mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(getSupportFragmentManager());

        // Set up the action bar.
        final ActionBar actionBar = getActionBar();

        // Specify that the Home/Up button should not be enabled, since there is no hierarchical
        // parent.
        actionBar.setHomeButtonEnabled(false);

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

        // Set up the ViewPager, attaching the adapter and setting up a listener for when the
        // user swipes between sections.
        mViewPager = (ViewPager) findViewById(R.id.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);
            }
        });

        // For each of the sections in the app, add a tab to the action bar.
        for (int i = 0; i < mAppSectionsPagerAdapter.getCount(); i++) {
            // 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()
                            .setText(mAppSectionsPagerAdapter.getPageTitle(i))
                            .setTabListener(this));
        }

    }

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

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        // When the given tab is selected, switch to the corresponding page in the ViewPager.
        mViewPager.setCurrentItem(tab.getPosition());
    }

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


    /* Called whenever we call invalidateOptionsMenu() */
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // If the nav drawer is open, hide action items related to the content view
        //boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
        //menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu items for use in the action bar
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_home, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        //boolean res = false;
        // The action bar home/up action should open or close the drawer.
        // ActionBarDrawerToggle will take care of this.
        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }

        switch (item.getItemId()) {
            case R.id.add:
                Toast.makeText(this, "add element", Toast.LENGTH_LONG).show();
                break;
            case R.id.search:
                Toast.makeText(this, "search a text", Toast.LENGTH_LONG).show();
                break;
            case R.id.edit:
                Toast.makeText(this, "edit a element", Toast.LENGTH_LONG).show();
                break;
            case R.id.delete:
                Toast.makeText(this, "delete a element", Toast.LENGTH_LONG).show();
                break;
            case R.id.action_settings:
                Toast.makeText(this, "acction settings", Toast.LENGTH_LONG).show();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    /* The click listner for ListView in the navigation drawer */
    private class DrawerItemClickListener implements ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selectItem(position);
        }
    }

    private void selectItem(int position) {
        //TODO
        switch (position) {
            case 0:
                Toast.makeText(this, "elemento 0", Toast.LENGTH_LONG).show();
                break;
            case 1:
                Toast.makeText(this, "elemento 1", Toast.LENGTH_LONG).show();
                break;
            case 2:
                Toast.makeText(this, "elemento 2", Toast.LENGTH_LONG).show();
                break;

        }

    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // Pass any configuration change to the drawer toggls
        mDrawerToggle.onConfigurationChanged(newConfig);
    }


    public void setGroupParents() {
        parentItems.add("Androwwid");
        parentItems.add("Core Java");
        parentItems.add("Desktop Java");
        parentItems.add("Enterprise Java");
    }

    public void setChildData() {

        // Android
        ArrayList<String> child = new ArrayList<>();
        child.add("Core");
        child.add("Games");
        childItems.add(child);

        // Core Java
        child = new ArrayList<>();
        child.add("Apache");
        child.add("Applet");
        child.add("AspectJ");
        child.add("Beans");
        child.add("Crypto");
        childItems.add(child);

        // Desktop Java
        child = new ArrayList<>();
        child.add("Accessibility");
        child.add("AWT");
        child.add("ImageIO");
        child.add("Print");
        childItems.add(child);

        // Enterprise Java
        child = new ArrayList<>();
        child.add("EJB3");
        child.add("GWT");
        child.add("Hibernate");
        child.add("JSP");
        childItems.add(child);
    }

    public static class AppSectionsPagerAdapter extends FragmentPagerAdapter {

        public AppSectionsPagerAdapter(android.support.v4.app.FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            switch (i) {
                case 0:
                    // The first section of the app is the most interesting -- it offers
                    // a launchpad into the other demonstrations in this example application.
                    return new DummySectionFragment();

                default:
                    // The other sections of the app are dummy placeholders.
                    Fragment fragment = new DummySectionFragment();
                    Bundle args = new Bundle();
                    args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1);
                    fragment.setArguments(args);
                    return fragment;
            }
        }

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

        @Override
        public CharSequence getPageTitle(int position) {
            return "Section " + (position + 1);
        }
    }
    public static class DummySectionFragment extends Fragment {

        public static final String ARG_SECTION_NUMBER = "section_number";

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_section_dummy, container, false);
            Bundle args = getArguments();
//            ((TextView) rootView.findViewById(android.R.id.text1)).setText(
//                    getString(R.string.dummy_section_text, args.getInt(ARG_SECTION_NUMBER)));
            return rootView;
        }
    }
}
公共类HomeActivity扩展FragmentActivity实现ActionBar.TabListener{
//可扩展列表
private ArrayList parentItems=new ArrayList();
private ArrayList childItems=new ArrayList();
//滑梯门
私有字符串[]mplanettiles;
私人抽屉布局mDrawerLayout;
私有列表视图mDrawerList;
私有字符序列mDrawerTitle;
私有字符序列mTitle;
私有操作bardrawertoggle mDrawerToggle;
//声明寻呼机
应用分区SPAGERAAdapter映射分区SPAGERAAdapter;
ViewPager mViewPager;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
/////可扩展列表视图部分/////
ExpandableListView expandableList=(ExpandableListView)findViewById(R.id.expandableListView1);
expandableList.setGroupIndicator(空);
可扩展列表。可设置可点击(true);
setGroupParents();
setChildData();
MyExpandableAdapter=新的MyExpandableAdapter(父项、子项);
setInflater((LayoutInflater)getSystemService(Context.LAYOUT\u充气器\u服务),这个;
expandableList.setAdapter(适配器);
expandableList.setOnChildClickListener(新的ExpandableListView.OnChildClickListener(){
@凌驾
公共布尔onChildClick(ExpandableListView父视图、视图v、int-groupPosition、int-childPosition、长id){
返回false;
}
});
///////动作栏部分///////////
ActionBar mActionBar=getActionBar();
mActionBar.setDisplayShowHomeEnabled(false);
mActionBar.setDisplayShowTitleEnabled(假);
LayoutInflater mInflater=LayoutInflater.from(this);
视图mCustomView=mInflater.flate(R.layout.custom\u actionbar,null);
TextView mTitleTextView=(TextView)mCustomView.findViewById(R.id.title_text);
mTitleTextView.setText(“Título”);
mActionBar.setCustomView(mCustomView);
mActionBar.setDisplayShowCustomEnabled(true);
///////导航抽屉部件////////
mTitle=mDrawerTitle=getTitle();
mPlanetTitles=getResources().getStringArray(R.array.prueba_数组);
mDrawerLayout=(抽屉布局)findViewById(R.id.抽屉布局);
mDrawerList=(ListView)findViewById(R.id.left\u抽屉);
//设置抽屉打开时覆盖主要内容的自定义阴影
mDrawerLayout.setDrawerShadow(R.drawable.drawer\u shadow,GravityCompat.START);
//使用项目设置抽屉的列表视图,然后单击listener
mDrawerList.setAdapter(新阵列适配器,
R.布局图.抽屉(列表项,MPLANETITLES);
setOnItemClickListener(新的DrawerItemClickListener());
//启用ActionBar应用程序图标作为切换导航抽屉的操作
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
//ActionBarDrawerToggle将适当的交互连接在一起
//在滑动抽屉和操作栏应用程序图标之间
mDrawerToggle=新操作bardrawertoggle(
此,/*主机活动*/
mDrawerLayout,/*抽屉布局对象*/
R.drawable.ic_抽屉,/*导航抽屉图像替换“Up”插入符号*/
R.string.drawer\u open,/*“open drawer”描述,用于辅助功能*/
R.string.drawer\u close/*“close drawer”可访问性说明*/
) {
公共无效onDrawerClosed(视图){
//getActionBar().setTitle(mTitle);
InvalidateOptions SMenu();//创建对OnPrepareOptions SMenu()的调用
}
打开图纸上的公共空白(视图抽屉视图){
//getActionBar().setTitle(mDrawerTitle);
InvalidateOptions SMenu();//创建对OnPrepareOptions SMenu()的调用
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
//如果(savedInstanceState==null){
//选择项目(0);
//        }
////////////零件视图寻呼机///////////////
//创建适配器,该适配器将为三个主要部分中的每个部分返回一个片段
//应用程序的名称。
mAppSectionsPagerAdapter=新的AppSectionsPagerAdapter(getSupportFragmentManager());
//设置操作栏。
最终ActionBar ActionBar=getActionBar();
//指定不应启用Home/Up按钮,因为没有层次结构
//家长。
actionBar.setHomeButtonEnabled(假);
//指定我们将在操作栏中显示选项卡。
actionBar.setNavigationMode(actionBar.NAVIGATION\u MODE\u选项卡);
//设置ViewPager,连接适配器并设置侦听器,以便
//用户在分区之间滑动。
mViewPager=(ViewPager)findViewById(R.id.pager);
mViewPager.setAdapter(mAppSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(新的ViewPager.SimpleOnPageChangeListener(){
@凌驾
已选择页面上的公共无效(内部位置){
//在不同的应用程序分区之间滑动时,请选择相应的选项卡。
//我们也可以使用ActionBar.Tab#select()来完成这项工作,如果我们有
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


    <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <fragment
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:name="com.example.julio.listviewactionbar.Fragment_A"
            android:id="@+id/fragment_explist"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            tools:layout="@layout/fragment_explistview" />

        <ListView
            android:id="@+id/left_drawer"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:background="#111"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp" />

    </android.support.v4.widget.DrawerLayout>

</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <android.support.v4.view.ViewPager
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <fragment
            android:id="@+id/fragment_explist"
            android:name="com.example.julio.listviewactionbar.Fragment_A"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            tools:layout="@layout/fragment_explistview" />
    </FrameLayout>

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#111"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp" />

</android.support.v4.widget.DrawerLayout>