java - 抽屉菜单未显示在 ViewPager + ActionBar.Listener 中

标签 java android android-fragments drawerlayout

在图像上,抽屉布局位于 ViewPager + Tab 内。我想打开抽屉菜单,但是当我点击 Home 按钮 时,没有任何反应。我只想在这种情况下显示菜单。

在某些时候,当我向右滑动要打开的抽屉时,viewPager 却被触发并转移到下一页。

我是否可以在抽屉菜单中放置一个类似可绘制的边框,以便在我单击边框或拖动它时它会打开抽屉?蓝线只是添加的编辑,而不是在实际场景中。

enter image description here

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

DrawerLayoutFragment

 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(); 
    }
}

drawer_layout_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<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">

    <!-- 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. -->
    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#111"/>
</android.support.v4.widget.DrawerLayout>

对不起,先生,iDroid 没有这么快更新。

最佳答案

更新:

我也遇到了同样的问题然后我累了SlidingLayer .

您也可以尝试一下,这正是您所需要的。

享受编码...

============================================= ======

您似乎想在 DrawerLayout 中添加另一个 View 。如果是,那么你可以像下面那样做:

这是我的带有 drawerlayout 的 xml 布局代码:

    <?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>

它看起来像这样:

enter image description here

关于java - 抽屉菜单未显示在 ViewPager + ActionBar.Listener 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18117214/

相关文章:

java - 排序行为不同?

javascript - 背景图像大于视口(viewport)

android - 华为应用市场发布版6003错误码

Android ResideMenu 库,Fragment 底部有 Cropping 问题

java - 为什么在这个 instanceof 测试中会出现编译器错误

Java相当于javascript中的unencode函数来解析转义的unicode

Android 主从流示例

java - Android 接收 null Parcelable 对象

java - 在 Android 中使用事件总线以及 Activity Lifecycle 方法和库实现

java - 有没有办法通过 jaxb 解码来自定义 getter?