작업 표시 줄에 서랍 레이아웃이있는 Android 앱을 개발 중입니다. 서랍을 열거 나 닫을 때들을 수 있도록 drawerlayout의 리스너를 추가했습니다. 드로어 레이아웃에 첨부 된 탐색 뷰가 있습니다.Android 햄버거 메뉴 상호 작용
서랍은 열림, 열림, 열림, 닫힘 및 열림 - 닫힘과 같은 다양한 방법으로 열고 닫을 수 있습니다. 이벤트를 추가하여 각 이벤트를 개별적으로 식별해야합니다. 나는이 방법으로 drawerlayout 및 navigationview을 추가 한
: 활동에
<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:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity">
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
>
<include
android:id="@+id/toolbar"
layout="@layout/tool_bar"
/>
<FrameLayout
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/navigation_view"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_gravity="start"
app:headerLayout="@layout/header"
app:menu="@menu/drawer"
/>
노, 나는 navigationview 및 drawerlayout에서 다음 청취자를 추가했습니다.
//Setting Navigation View Item Selected Listener to handle the item click of the navigation menu
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
// This method will trigger on item Click of navigation menu
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
//Checking if the item is in checked state or not, if not make it in checked state
if(menuItem.isChecked()) menuItem.setChecked(false);
else menuItem.setChecked(true);
//Closing drawer on item click
drawerLayout.closeDrawers();
//Check to see which item was being clicked and perform appropriate action
switch (menuItem.getItemId()){
// Switch case identify different options
}
}
});
// Initializing Drawer Layout and ActionBarToggle
drawerLayout = (DrawerLayout) findViewById(R.id.drawer);
ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.openDrawer, R.string.closeDrawer){
@Override
public void onDrawerClosed(View drawerView) {
// Code here will be triggered once the drawer closes as we dont want anything to happen so we leave this blank
super.onDrawerClosed(drawerView);
}
@Override
public void onDrawerOpened(View drawerView) {
// Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank
super.onDrawerOpened(drawerView);
}
};
//Setting the actionbarToggle to drawer layout
drawerLayout.setDrawerListener(actionBarDrawerToggle);
//calling sync state is necessay or else your hamburger icon wont show up
actionBarDrawerToggle.syncState();
이제 onDrawerOpened
및 onDrawerClosed
은 서랍을 열고 닫을 때에 대해 나에게 정보를 제공합니다. 상호 작용을 열거 나 닫을 때 어떻게 구별합니까? 식별 할 상호 작용 : tap-to-open, swipe-to-open, swipe-to-close and tap-outside-to-close
.