在自定义drawerlayout中,如何使NavigationView可拖动

时间:2016-07-07 11:58:58

标签: android user-interface

我尝试使用代码来自定义DrawerLayout

class Nd extends DrawerLayout {

    ...

    void ii() {
        inflate(getContext(), R.layout.nd, this);
    }
}

和布局视图是:

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
       android:orientation="vertical"
       android:layout_width="match_parent"
       android:layout_height="match_parent">
    <android.support.design.widget.NavigationView android:layout_width="wrap_content"
                                                  android:layout_height="match_parent"></android.support.design.widget.NavigationView>
</merge>

当我尝试

<ro.rotry.Nd
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="ro.rotry.TestNd2">
<android.support.design.widget.CoordinatorLayout android:layout_width="match_parent"
                                                 layout_gravity="center"
                                                 android:layout_height="match_parent">
    <TextView android:text="t" android:layout_width="match_parent" android:layout_height="match_parent"/>
</android.support.design.widget.CoordinatorLayout>
</ro.rotry.Nd>

导航视图无法右键滑动打开,如何使其可拖动?

更新

我尝试原始代码可以做到这一点,但是当我将其提取到自定义视图时,它不起作用

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="ro.rotry.TestNd2">
    <android.support.design.widget.NavigationView
            android:id="@+id/nv"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:menu="@menu/menu_main"
            android:fitsSystemWindows="true">
    </android.support.design.widget.NavigationView>

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

我的问题是如何制作提取&#34; android.support.v4.widget.DrawerLayout&#34;代码进入自定义视图并使NavigationView可以右键滑动打开

2 个答案:

答案 0 :(得分:0)

首先创建一个像这样的基类

    public class BaseActivity extends AppCompatActivity {
    Toolbar toolbar;

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        bindViews();
    }

    protected void bindViews() {
        setupToolbar();
    }

    public void setContentViewWithoutInject(int layoutResId) {
        super.setContentView(layoutResId);
    }

    protected void setupToolbar() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        if (toolbar != null) {
            setSupportActionBar(toolbar);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public Toolbar getToolbar() {
        return toolbar;
    }
}

然后创建另一个类BaseDrawerActivity

public class BaseDrawerActivity extends BaseActivity {

DrawerLayout drawerLayout;
NavigationView vNavigation;

private ImageView ivMenuUserProfilePhoto;

@Override
public void setContentView(int layoutResID) {
    super.setContentViewWithoutInject(R.layout.activity_drawer);
    ViewGroup viewGroup = (ViewGroup) findViewById(R.id.flContentRoot);
    LayoutInflater.from(this).inflate(layoutResID, viewGroup, true);
    drawerLayout = (DrawerLayout)findViewById(R.id.drawerLayout);
    vNavigation = (NavigationView)findViewById(R.id.vNavigation);
    bindViews();
   // setupHeader();

}
@Override
protected void setupToolbar() {
    super.setupToolbar();
    if (getToolbar() != null) {
        getToolbar().setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                drawerLayout.openDrawer(Gravity.START);
            }
        });
    }
}

private void setupHeader() {
    View headerView = vNavigation.getHeaderView(0);
    ivMenuUserProfilePhoto = (ImageView) headerView.findViewById(R.id.ivMenuUserProfilePhoto);
    headerView.findViewById(R.id.vGlobalMenuHeader).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onGlobalMenuHeaderClick(v);
        }
    });


            .

public void onGlobalMenuHeaderClick(final View v) {
    drawerLayout.closeDrawer(Gravity.LEFT);
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {

        }
    }, 200);
}

  }

创建xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

    <android.support.design.widget.NavigationView
        android:id="@+id/vNavigation"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#ffffff"
        app:itemIconTint="#8b8b8b"
        app:itemTextColor="#666666"
        app:menu="@menu/drawer_menu" />

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

在内部菜单中添加此xml文件

 <menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:id="@+id/menu_group_1">
    <item
        android:id="@+id/menu_feed"
        android:icon="@drawable/ic_dashboard_black"
        android:title="Dashboard" />
    <item
        android:id="@+id/menu_direct"
        android:icon="@drawable/ic_subscriptions_black"
        android:title="Subscribe" />
    <item
        android:id="@+id/menu_news"
        android:icon="@drawable/nav_ic_result"
        android:title="Apply for quotation" /> 

      <item
        android:id="@+id/menu_account"
        android:icon="@drawable/account"
        android:title="My Account" /> 

     <item
        android:id="@+id/change_password"
        android:icon="@drawable/drawer_password"
        android:title="Change Password" /> 

</group>
<group android:id="@+id/menu_group_2">
    <item
        android:id="@+id/menu_settings"
        android:icon="@drawable/nav_ic_settings"
        android:title="Settings" />
    <item
        android:id="@+id/menu_about"
        android:icon="@drawable/nav_ic_about"
        android:title="About" />
</group>
</menu>

然后使用BaseDrawerActivity扩展您的活动

public class ActivityCart extends BaseDrawerActivity{

 ///  your code 
}}

答案 1 :(得分:-1)

我找到原因,我应该将NavigationView放在最后的DrawerLayout中:

//DrawerLayout#onInterceptTouchEvent
                final View child = mLeftDragger.findTopChildUnder((int) x, (int) y);
                if (child != null && isContentView(child)) {
                    interceptForTap = true;
                }

在我的问题(错误代码)中,mLeftDragger.findTopChildUnder总是返回CoordinatorLayout,这是错误的,它应该返回NavigationView,所以我使用以下代码添加bringtoFront来修复它:

class Nd extends DrawerLayout {

    public Nd(Context context) {
        super(context);
        ii();
    }

    public Nd(Context context, AttributeSet attrs) {
        super(context, attrs);
        ii();
    }

    void ii() {
        inflate(getContext(), R.layout.nd, this);
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        findViewById(R.id.nv).bringToFront();
    }
}