1

在图像上,抽屉布局位于 ViewPager + Tab 内。我想打开抽屉菜单,但是当我单击主页按钮时,什么也不做。我只是想在这种情况下显示菜单。

在某些时候,当我向右滑动要打开的抽屉时,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; 

抽屉布局片段

 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 没有快速更新。

4

1 回答 1

1

更新:

我也遇到了同样的问题,然后我厌倦了SlidingLayer

你也可以试一试,这正是你所需要的。

享受编码...

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

您似乎想在 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>

它看起来像这样:

在此处输入图像描述

于 2013-08-08T03:54:02.473 回答