Ich habe ein DrawerLayout erstellt und es funktioniert gut. Aber ich will, dass es schließt, wenn der Benutzer den Hintergrund berührt. Dies kann mit einer mit listView
implementiert werden, aber hier verwende ich eine NavigationView
. Gibt es einen Weg, dies zu erreichen? HierWie kann ich ein androides DrawerLayout schließen, wenn ich es außerhalb davon berühre?
ist das Menü-Layout für die 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>
Hier wird der Java-Code
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;
}
}
}
Hier ist der DrawerLayout
Code
<?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>
Ich habe auch in this Frage sah , aber es löst nicht mein Problem
'DrawerLayout' bereits tut, unabhängig davon, was Sie für die Schublade verwenden, wenn Sie es richtig eingerichtet haben. Wenn deins nicht ist, dann ist es wahrscheinlich ein Problem mit dem Layout. –
Mike hat Recht, wenn es eine Schublade in Ihrer Aktivität gibt, dann ist es das Standardverhalten. Überprüfen Sie die Einrichtung der Schublade. – ADM
Zeigen Sie Ihr Layout, Problem möglicherweise in Ihrem Layout, wo Sie Ihre Schublade definieren – Jacky