我正在添加材质抽屉Material Drawer并创建了一个方法 addNavigationDrawer 来添加导航抽屉并在我从 Firebase Firestore 数据库获取用户详细信息后调用该方法(您可以在 OnCreate 方法下的 Auth Listener 中找到它)但是在第一次打开这个如您在图像中看到的那样,活动栏正在切割但是当我将其最小化并再次打开时,它的工作绝对正常。请建议怎么做?
这是Activity的完整代码
public class Main2Activity extends AppCompatActivity {
ProfileInformationDialog profileInformationDialog;
FirebaseAuth firebaseAuth;
FirebaseAuth.AuthStateListener authStateListener;
FirebaseFirestore db;
DocumentReference documentReference;
Button button;
GoogleApiClient googleApiClient;
/*@BindView(R.id.toolbar)
Toolbar toolbar;*/
ProgressDialog progressDialog;
String user_name,shop_name;
Toolbar toolbar;
Drawer result;
AccountHeader headerResult;
@Override
public void onStart()
{
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build();
googleApiClient = new GoogleApiClient.Builder(this)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
googleApiClient.connect();
super.onStart();
firebaseAuth.addAuthStateListener(authStateListener);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
toolbar = (Toolbar)findViewById(R.id.toolbar);
toolbar.setTitle("Hello");
setSupportActionBar(toolbar);
//DrawerUtil.getDrawer(Main2Activity.this,toolbar);
//addNavigationDrawer();
progressDialog = new ProgressDialog(this);
db = FirebaseFirestore.getInstance();
profileInformationDialog = new ProfileInformationDialog(this);
FacebookSdk.sdkInitialize(getApplicationContext());
AppEventsLogger.activateApp(this);
ButterKnife.bind(this);
button = (Button) findViewById(R.id.log_out);
firebaseAuth = FirebaseAuth.getInstance();
authStateListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
final FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
if (firebaseUser != null) {
progressDialog.setMessage("Loading...");
progressDialog.show();
progressDialog.setCancelable(false);
//User is signed in
documentReference = db.collection("Users").document(firebaseUser.getUid());
documentReference.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot)
{
if (documentSnapshot != null && documentSnapshot.exists()) {
progressDialog.hide();
User user1 = documentSnapshot.toObject(User.class);
user_name = user1.getName();
shop_name = user1.getShop_name();
addNavigationDrawer();
//DrawerUtil.getDrawer(Main2Activity.this,toolbar);
headerResult.updateProfile(new ProfileDrawerItem().withIcon(R.drawable.logout_icon256).withName(user_name));
Toast.makeText(Main2Activity.this, "Document Exists", Toast.LENGTH_SHORT).show();
}
else
{
progressDialog.hide();
Toast.makeText(Main2Activity.this, "No Such Document", Toast.LENGTH_SHORT).show();
profileInformationDialog.show();
profileInformationDialog.setCancelable(false);
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e)
{
progressDialog.hide();
Toast.makeText(Main2Activity.this, "Exception " + e, Toast.LENGTH_SHORT).show();
}
});
Log.d("TAG", "onAuthStateChanged:signed_in:" + firebaseUser.getUid());
} else {
// User is signed out
Log.d("TAG", "onAuthStateChanged:signed_out");
}
}
};
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Auth.GoogleSignInApi.signOut(googleApiClient).setResultCallback(
new ResultCallback<Status>() {
@Override
public void onResult(@NonNull Status status)
{
//Toast.makeText(getApplicationContext(),"Google Logged Out",Toast.LENGTH_SHORT).show();
}
});
firebaseAuth.signOut();
LoginManager.getInstance().logOut();
startActivity(new Intent(Main2Activity.this,SignInActivity.class));
finish();
}
});
}
private void addNavigationDrawer()
{
headerResult = new AccountHeaderBuilder()
.withActivity(this)
.withHeaderBackground(R.drawable.curve_shape)
.withSelectionListEnabledForSingleProfile(true)
.addProfiles(
new ProfileDrawerItem().withName(user_name).withIcon(R.drawable.logout_icon256)
)
.withOnAccountHeaderListener(new AccountHeader.OnAccountHeaderListener() {
@Override
public boolean onProfileChanged(View view, IProfile profile, boolean currentProfile)
{
Toast.makeText(Main2Activity.this, profile.getName() + "", Toast.LENGTH_SHORT).show();
return false;
}
})
.build();
PrimaryDrawerItem makeBillItem = new PrimaryDrawerItem().withIdentifier(1)
.withName("Make new Bill");
SecondaryDrawerItem logoutItem = new SecondaryDrawerItem().withIdentifier(2)
.withName("Log Out").withIcon(R.drawable.logout_icon256);
result = new DrawerBuilder()
.withAccountHeader(headerResult)
.withActivity(this)
.withToolbar(toolbar)
.withTranslucentStatusBar(false)
.withDisplayBelowStatusBar(false)
.withActionBarDrawerToggle(true)
.withActionBarDrawerToggleAnimated(true)
.withCloseOnClick(true)
.withSelectedItem(-1)
.addDrawerItems(
makeBillItem,
new DividerDrawerItem(),
logoutItem
)
.withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
@Override
public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
Toast.makeText(Main2Activity.this, position + " = position", Toast.LENGTH_SHORT).show();
return true;
}
})
.build();
}
@Override
public void onStop() {
super.onStop();
if (firebaseAuth != null) {
firebaseAuth.removeAuthStateListener(authStateListener);
}
}
}