Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
219 views
in Technique[技术] by (71.8m points)

android - Why i can't lock DrawerLayout with layout gravity

I use DrawerLayout and recently i want to change gravity of listView in drawerLayout. But after i change gravity of listView to android:layout_gravity="start|bottom"from android:layout_gravity="start", drawerLayout can't be lock to

mDrawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);

setDrawerLockMode() work with;

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

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</RelativeLayout>

<ListView
    android:id="@+id/drawer_list"
    android:layout_width="320dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#F3F3F4"
    android:choiceMode="singleChoice" >
</ListView>

But it doesn't lock with;

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

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</RelativeLayout>

<ListView
    android:id="@+id/drawer_list"
    android:layout_width="320dp"
    android:layout_height="match_parent"
    android:layout_gravity="start|bottom"
    android:background="#F3F3F4"
    android:choiceMode="singleChoice" >
</ListView>

`

Any clues of why can't I use lock mode with other gravities?

Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Based on the documentation, the only available gravities that can be used are Gravity.LEFT, Gravity.RIGHT or GravityCompat.START, GravityCompat.END.

(Emphasis mine):

Drawer positioning and layout is controlled using the android:layout_gravity attribute on child views corresponding to which side of the view you want the drawer to emerge from: left or right. (Or start/end on platform versions that support layout direction.)

Looking at the source code

public void setDrawerLockMode(int lockMode, int edgeGravity) {
  final int absGrav = GravityCompat.getAbsoluteGravity(edgeGravity,
                                                       ViewCompat.getLayoutDirection(this));
  if (absGrav == Gravity.LEFT) {
    mLockModeLeft = lockMode;
  } else if (absGrav == Gravity.RIGHT) {
    mLockModeRight = lockMode;
  }
  if (lockMode != LOCK_MODE_UNLOCKED) {
    // Cancel interaction in progress
    final ViewDragHelper helper = absGrav == Gravity.LEFT ? mLeftDragger : mRightDragger;
    helper.cancel();
  }
  switch (lockMode) {
    case LOCK_MODE_LOCKED_OPEN:
      final View toOpen = findDrawerWithGravity(absGrav);
      if (toOpen != null) {
        openDrawer(toOpen);
      }
      break;
    case LOCK_MODE_LOCKED_CLOSED:
      final View toClose = findDrawerWithGravity(absGrav);
      if (toClose != null) {
        closeDrawer(toClose);
      }
      break;
      // default: do nothing
  }
}

The method itself only checks if the gravity is LEFT or RIGHT (but uses a GravityCompat method, so START and END should be appropriately translated).

This would mean that by setting a gravity of "start|bottom", you're introducing an invalid gravity, which causes setDrawerLockMode() to do nothing.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...