2016-07-07 2 views
0

Я стараюсь следующий код для пользовательских DrawerLayoutв пользовательских drawerlayout, как сделать NavigationView перетаскиваемым

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> 

NavigationView не может быть правным, чтобы открыть, как заставить его перетащить?

обновление

Стараюсь исходный код может это сделать, но, когда я извлечь его настраиваемое представление, он не работает

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

мой вопрос как сделать выписку " android.support.v4.widget.DrawerLayout "в пользовательский вид и сделать NavigationView можно щелкнуть правой кнопкой, чтобы открыть

ответ

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

Я найти причину, я должен поставить NavigationView в последний DrawerLayout по:

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

в моем вопросе (ERR-код), 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(); 
    } 
} 
Смежные вопросы