【问题标题】:How can I close an android DrawerLayout when touched outside of it?在 Android DrawerLayout 外部触摸时如何关闭它?
【发布时间】:2017-12-06 08:17:28
【问题描述】:

我创建了一个 DrawerLayout,它工作正常。但我希望它在用户触摸背景时关闭。这可以通过DrawerLayoutlistView 来实现,但这里我使用的是NavigationView。那么有没有办法做到这一点呢?

这是NavigationView的菜单布局

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/home" android:title="Home Parent" android:icon="@drawable/ic_home_black"/>
    <item android:id="@+id/send" android:title="Send" android:icon="@drawable/ic_send_black"/>
    <item android:id="@+id/add" android:title="Add" android:icon="@drawable/ic_add_black"/>
</menu>

这里是java代码

public class ParentActivity extends AppCompatActivity {

    private NavigationView mNavigationView;
    private User mCurrentUser;
    private UserLocalStore mUserLocalStore;
    private CircleImageView mProfilePic;
    private TextView mProfileName;
    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mToggle;

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

        mUserLocalStore = new UserLocalStore(this);
        mCurrentUser = mUserLocalStore.getUserDetails();

        mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer);
        mToggle = new ActionBarDrawerToggle(this,mDrawerLayout,R.string.open,R.string.close);
        mDrawerLayout.addDrawerListener(mToggle);
        mToggle.syncState();
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        mNavigationView = (NavigationView)findViewById(R.id.navigationView);
        setNavigationViewMenu(mCurrentUser.userType);
        mProfileName = (TextView) mNavigationView.getHeaderView(0).findViewById(R.id.profileName);
        mProfileName.setText(mCurrentUser.getName());
        mProfilePic = (CircleImageView) mNavigationView.getHeaderView(0).findViewById(R.id.circleImageProfile);
        Picasso.with(this).load("https://www.sonypark360.net/wp-content/uploads/2017/08/profile-pictures.png").into(mProfilePic);

        mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                mDrawerLayout.closeDrawers();
                return false;
            }
        });

    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(mToggle.onOptionsItemSelected(item)){
            return true;
        }
        return super.onOptionsItemSelected(item);
    }



    private void setNavigationViewMenu(String userType) {
        switch (userType){
            case "s":
                mNavigationView.inflateMenu(R.menu.menu_student_navigation_drawer);
                break;
            case "pa":
                mNavigationView.inflateMenu(R.menu.menu_parent_navigation_drawer);
                break;
            case "pr":
                mNavigationView.inflateMenu(R.menu.menu_principal_navigation_drawer);
                break;
            case "t":
                mNavigationView.inflateMenu(R.menu.menu_teacher_navigation_drawer);
                break;
        }
    }
}

这是DrawerLayout 代码

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.mlpj.www.morascorpions.ParentActivity">

    <android.support.design.widget.NavigationView
        android:id="@+id/navigationView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/navigation_header_layout"

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

我也调查了this 的问题,但它并没有解决我的问题

【问题讨论】:

  • DrawerLayout 已经这样做了,不管你用什么抽屉,如果你设置正确的话。如果你的不是,那么很可能是布局有问题。
  • Mike 是对的,如果您的活动中有一个抽屉,那么它是默认行为。检查抽屉设置。
  • 显示您的布局,问题可能出在您定义抽屉的布局中
  • 你好,jacky,我已经添加了布局代码,你可以看看吗

标签: android navigation-drawer drawerlayout navigationview


【解决方案1】:

试试这个

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    Rect viewRect = new Rect();
    mNavigationView.getGlobalVisibleRect(viewRect);


    if (!viewRect.contains((int) ev.getRawX(), (int) ev.getRawY())) {

        //hide your navigation view here. 

    }
    return super.dispatchTouchEvent(ev);;
}

【讨论】:

  • 抱歉之前的评论。实际上它起作用了。非常感谢哈沙德
  • 但仍在寻找 Mike.M、ADM 和 Jacky 的默认行为的想法
猜你喜欢
  • 2020-05-06
  • 2020-04-11
  • 2014-06-25
  • 1970-01-01
  • 1970-01-01
  • 2012-08-19
  • 2012-11-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多