Java 抽屉菜单未显示在ViewPager+;ActionBar.Listener

Java 抽屉菜单未显示在ViewPager+;ActionBar.Listener,java,android,android-fragments,drawerlayout,Java,Android,Android Fragments,Drawerlayout,在图像上,抽屉布局位于ViewPager+选项卡内。我想打开抽屉菜单,但当我单击“主页”按钮时,没有任何作用。我只是想在这个场景中显示菜单 switch(arg0){ /** Android tab is selected */ case 0: DrawerLayoutFragment androidFragment = new DrawerLayoutFragment(); data

在图像上,抽屉布局位于ViewPager+选项卡内。我想打开抽屉菜单,但当我单击“主页”按钮时,没有任何作用。我只是想在这个场景中显示菜单

switch(arg0){
            /** Android tab is selected */
            case 0:
                DrawerLayoutFragment androidFragment = new DrawerLayoutFragment();
                data.putInt("current_page", arg0+1);
                androidFragment.setArguments(data);
                return androidFragment; 
在某种程度上,当我向右滑动抽屉以打开时,取而代之的是viewPager被触发并转移到下一页

我是否可以在抽屉菜单中放置一个类似drawable的边框,这样当我单击或拖动边框时,它就会打开抽屉?蓝线只是添加的编辑,但不在实际场景中

switch(arg0){
            /** Android tab is selected */
            case 0:
                DrawerLayoutFragment androidFragment = new DrawerLayoutFragment();
                data.putInt("current_page", arg0+1);
                androidFragment.setArguments(data);
                return androidFragment; 

drawerlayioutfragment

 public class DrawerLayoutFragment extends Fragment implements SimpleGestureListener{

   private SimpleGestureFilter detector; 

    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;

    private CharSequence mDrawerTitle;
    private CharSequence mTitle;
    private String[] mPlanetTitles;

    public void onCreate (Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        detector = new SimpleGestureFilter(getActivity(),this);

         mTitle = mDrawerTitle = getActivity().getTitle();


    }

    @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.drawer_layout_fragment, container, false);

        mPlanetTitles = rootView.getResources().getStringArray(R.array.planets_array);
        mDrawerLayout = (DrawerLayout) rootView.findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) rootView.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>(getActivity(),
                R.layout.drawer_list_item, mPlanetTitles));
        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());


        //enable ActionBar app icon to behave as action to toggle nav drawer

        getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
        getActivity().getActionBar().setHomeButtonEnabled(true);

        // ActionBarDrawerToggle ties together the the proper interactions
        // between the sliding drawer and the action bar app icon
        mDrawerToggle = new ActionBarDrawerToggle(
                getActivity(),                  /* 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) {
                getActivity().getActionBar().setTitle(mTitle);
                getActivity().invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()

                Log.d("onDrawerClosed", "inside");
            }

            public void onDrawerOpened(View drawerView) {
                getActivity().getActionBar().setTitle(mDrawerTitle);
                getActivity().invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);



        if (savedInstanceState == null) {
            selectItem(0);
        }


        return rootView;
    }

     public boolean onCreateOptionsMenu(Menu menu) {
            MenuInflater inflater = getActivity().getMenuInflater();
           inflater.inflate(R.menu.main_menu, menu);
            return getActivity().onCreateOptionsMenu(menu);
        }

     @Override
        public boolean onOptionsItemSelected(MenuItem item) {

             // The action bar home/u p action should open or close the drawer.
             // ActionBarDrawerToggle will take care of this.
            if (mDrawerToggle.onOptionsItemSelected(item)) {

                return true;
            }
            // Handle action buttons
            switch(item.getItemId()) {
            default:
                Log.d("onOptionsItemSelected", "inside");
                return super.onOptionsItemSelected(item);
            }
        }


     protected void onPostCreate(Bundle savedInstanceState) {
            super.onCreate(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);
        }



    /* 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);

            Log.d("DrawerItemClickListener", "inside");
        }
    }

    private void selectItem(int position) {
     Log.d("selectItem", "inside");
       // update the main content by replacing fragments
      PlanetFragment fragment = new PlanetFragment();
       Bundle args = new Bundle();
       args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);
       fragment.setArguments(args);

       FragmentTransaction ft = getChildFragmentManager().beginTransaction();
                 ft.add(R.id.content_frame, fragment);
         ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
         ft.commit();

       // update selected item and title, then close the drawer
       mDrawerList.setItemChecked(position, true);
       setTitle(mPlanetTitles[position]);
       mDrawerLayout.closeDrawer(mDrawerList);
   }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public void setTitle(CharSequence title) {
        mTitle = title;
        getActivity().getActionBar().setTitle(mTitle);
    }


    public static class PlanetFragment extends SherlockFragment {
        public static final String ARG_PLANET_NUMBER = "planet_number";

        public PlanetFragment() {
            // Empty constructor required for fragment subclasses
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_planet, container, false);
            int i = getArguments().getInt(ARG_PLANET_NUMBER);
            String planet = getResources().getStringArray(R.array.planets_array)[i];

            int imageId = getResources().getIdentifier(planet.toLowerCase(Locale.getDefault()),
                            "drawable", getActivity().getPackageName());
            ((ImageView) rootView.findViewById(R.id.image)).setImageResource(imageId);
            getActivity().setTitle(planet);
            return rootView;
        }
    }


    //Simple Gesture
    public boolean dispatchTouchEvent(MotionEvent me){ 
      this.detector.onTouchEvent(me);
     return getActivity().dispatchTouchEvent(me); 
    }

     @Override
    public void onSwipe(int direction) {
     String str = "";

     switch (direction) {

     case SimpleGestureFilter.SWIPE_RIGHT : str = "Swipe Right";

         mDrawerLayout.openDrawer(mDrawerList);
         break;

     case SimpleGestureFilter.SWIPE_LEFT :  str = "Swipe Left";
         mDrawerLayout.closeDrawer(mDrawerList);
         break;
     case SimpleGestureFilter.SWIPE_DOWN :  str = "Swipe Down";
                                                    break;
     case SimpleGestureFilter.SWIPE_UP :    str = "Swipe Up";
                                                    break;

     } 
      Toast.makeText(getActivity(), str, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDoubleTap() {
       Toast.makeText(getActivity(), "Double Tap", Toast.LENGTH_SHORT).show(); 
    }
}
公共类DrawerLayoutFragment扩展片段实现SimpleGetStureListener{
专用单纯形滤波器检测器;
私人抽屉布局mDrawerLayout;
私有列表视图mDrawerList;
私有操作bardrawertoggle mDrawerToggle;
私有字符序列mDrawerTitle;
私有字符序列mTitle;
私有字符串[]mplanettiles;
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
检测器=新的SimpleGestureFilter(getActivity(),this);
mTitle=mDrawerTitle=getActivity().getitle();
}
@TargetApi(构建版本代码冰淇淋三明治)
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图根视图=充气机。充气(R.layout.drawer\u layout\u碎片,容器,false);
mPlanetTitles=rootView.getResources().getStringArray(R.array.planets\u数组);
mDrawerLayout=(抽屉布局)rootView.findViewById(R.id.drawer\u布局);
mDrawerList=(ListView)rootView.findViewById(R.id.left\u抽屉);
//设置抽屉打开时覆盖主要内容的自定义阴影
mDrawerLayout.setDrawerShadow(R.drawable.drawer\u shadow,GravityCompat.START);
//使用项目设置抽屉的列表视图,然后单击listener
mDrawerList.setAdapter(新的ArrayAdapter(getActivity()),
R.布局图.抽屉(列表项,MPLANETITLES);
setOnItemClickListener(新的DrawerItemClickListener());
//启用ActionBar应用程序图标作为切换导航抽屉的操作
getActivity().getActionBar().setDisplayHomeAsUpEnabled(true);
getActivity().getActionBar().setHomeButtonEnabled(true);
//ActionBarDrawerToggle将适当的交互连接在一起
//在滑动抽屉和操作栏应用程序图标之间
mDrawerToggle=新操作bardrawertoggle(
getActivity(),/*主机活动*/
mDrawerLayout,/*抽屉布局对象*/
R.drawable.ic_抽屉,/*导航抽屉图像替换“Up”插入符号*/
R.string.drawer\u open,/*“open drawer”描述,用于辅助功能*/
R.string.drawer\u close/*“close drawer”可访问性说明*/
) {
公共无效onDrawerClosed(视图){
getActivity().getActionBar().setTitle(mTitle);
getActivity().InvalidateOptions菜单();//创建对OnPrepareOptions菜单()的调用
日志d(“onDrawerClosed”、“inside”);
}
打开图纸上的公共空白(视图抽屉视图){
getActivity().getActionBar().setTitle(mDrawerTitle);
getActivity().InvalidateOptions菜单();//创建对OnPrepareOptions菜单()的调用
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
如果(savedInstanceState==null){
选择项目(0);
}
返回rootView;
}
公共布尔onCreateOptions菜单(菜单){
MenuInflater充气器=getActivity()。getMenuInflater();
充气机。充气(右菜单。主菜单,菜单);
返回getActivity().onCreateOptions菜单(菜单);
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
//操作栏home/u p操作应打开或关闭抽屉。
//ActionBarDrawerToggle会处理好的。
如果(MDRAWERTOGLE.onOptionsItemSelected(项目)){
返回true;
}
//手柄动作按钮
开关(item.getItemId()){
违约:
Log.d(“onoptionItemSelected”、“inside”);
返回super.onOptionsItemSelected(项目);
}
}
后期创建时受保护的空(捆绑包savedInstanceState){
super.onCreate(savedInstanceState);
//在onRestoreInstanceState发生后同步切换状态。
mDrawerToggle.syncState();
}
@凌驾
公共无效OnConfiguration已更改(配置newConfig){
super.onConfigurationChanged(newConfig);
//将任何配置更改传递给抽屉toggls
mDrawerToggle.onConfigurationChanged(newConfig);
}
/*在导航抽屉中单击ListView的listner*/
私有类DrawerItemClickListener实现ListView.OnItemClickListener{
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
选择项目(位置);
Log.d(“抽屉式”、“内部”);
}
}
私有void selectItem(内部位置){
Log.d(“selectItem”、“inside”);
//通过替换片段更新主要内容
PlanetFragment片段=新的PlanetFragment();
Bundle args=新Bundle();
args.putInt(PlanetFragment.ARG_行星编号、位置);
fragment.setArguments(args);
FragmentTransaction ft=getChildFragmentManager().beginTransaction();
ft.add(R.id.content\u frame,fragment);
ft.setTransition(碎片事务处理、传输、碎片淡入淡出);
    <?xml version="1.0" encoding="UTF-8"?>
<!--
 A DrawerLayout is intended to 
    be used as the top-level content view using match_parent for both width and 
    height to consume the full space available.
-->
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:ignore="UnusedResources"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     >

    <!--
     As the main content view, the view below consumes the entire space available 
    using match_parent in both dimensions.
    -->

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!--
         android:layout_gravity="start" tells DrawerLayout to treat this as 
        a sliding drawer on the left side for left-to-right languages and on the 
        right side for right-to-left languages. The drawer is given a fixed width 
        in dp and extends the full height of the container. A solid background is 
        used for contrast with the content view.
    -->
     <LinearLayout  android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"

        android:orientation="horizontal">



    <LinearLayout

        android:layout_width="240dp"
        android:layout_height="match_parent"
         android:background="@drawable/list_background_gradient"

        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:layout_margin="10dp"
            android:gravity="center_vertical" 
            android:background="@drawable/search_box"
            android:orientation="horizontal">

            <EditText
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginLeft="2dp"
                android:layout_marginRight="2dp"
                android:paddingLeft="13dp"
                android:paddingRight="13dp"
                android:layout_weight="1"
                android:singleLine="true"
                android:hint="SEARCH USER"
                android:maxLength="30"
                android:textColor="#FFFFFF"
                 android:background="@android:color/transparent"
                android:textSize="15sp" />
             <ImageView android:layout_height="20dp"
                android:layout_width="wrap_content"
                android:layout_marginRight="5dp"
                android:layout_marginTop="5dp"
                android:layout_marginBottom="5dp"
                android:layout_gravity="center"

                android:src="@drawable/category_search"
                />
        </LinearLayout>

        <TextView  android:layout_width="240dp"
            android:layout_height="wrap_content"
            android:text="SELECT A LINE"
            android:textSize="20sp"
            android:textColor="#FFFFFF"
            android:gravity="center_horizontal"
            android:paddingTop="5dp"
            android:paddingBottom="5dp"
            android:layout_marginBottom="2dp"
            android:background="#43BBED"
            />


        <ListView
            android:id="@+id/list_drawer"
            android:layout_width="240dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:layout_margin="2dp"
            android:cacheColorHint="#00000000"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp"
            android:scrollbars="none" />
    </LinearLayout>
     <ImageView android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:src="@drawable/ic_launcher"
                android:scrollX="20dp"
                />
    </LinearLayout> 

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