我可以使用youtube https://www.youtube.com/playlist?list=PLGCjwl1RrtcR1j6EmpBxJyJYowK2QIsdT
中的firebase教程制作导航抽屉和谷歌登录应用我有 activity_navigation.xml :
<?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:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="rjbc.cpvc.NavigationActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout = "@layout/navigation_action"
android:layout_height="wrap_content"
android:layout_width="match_parent" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginStart="101dp"
android:layout_marginTop="201dp"
android:id="@+id/textView5" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@mipmap/ic_person_black_24dp"
android:layout_below="@+id/textView5"
android:layout_alignEnd="@+id/textView5"
android:layout_marginEnd="42dp"
android:layout_marginTop="53dp"
android:id="@+id/imageView" />
</RelativeLayout>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:menu="@menu/navigation_menu"
android:layout_gravity = "start"
app:headerLayout="@layout/navigation_header" />
</android.support.v4.widget.DrawerLayout>
和 navigation_header.xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="140dp"
android:background="@color/colorPrimary"
android:padding="20dp"
android:id="@+id/pName">
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="13dp"
android:id="@+id/textView"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:textColor="@color/common_google_signin_btn_text_dark_default" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@mipmap/ic_person_black_24dp"
android:id="@+id/pPic"
android:layout_above="@+id/textView"
android:layout_alignEnd="@+id/textView"
android:layout_marginEnd="16dp"
android:layout_marginBottom="18dp" />
</RelativeLayout>
这是我的navigation_activity.java
public class NavigationActivity extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;
private Toolbar mToolbar;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private TextView mName;
private String name;
private Uri photoUrl;
private ImageView mPic;
private NavigationView mNavigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation);
mToolbar = findViewById(R.id.nav_action);
setSupportActionBar(mToolbar);
mDrawerLayout = findViewById(R.id.drawerLayout);
mToggle = new ActionBarDrawerToggle(this,mDrawerLayout,R.string.open,R.string.close);
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mAuth = FirebaseAuth.getInstance();
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
if(firebaseAuth.getCurrentUser() == null) {
startActivity(new Intent(NavigationActivity.this,MainActivity.class));
}
}
};
mName = mNavigationView.getHeaderView(0).findViewById(R.id.textView);
mPic = mNavigationView.getHeaderView(0).findViewById(R.id.pPic);
getCurrentinfo();
mNavigationView = findViewById(R.id.nav_view);
mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(final MenuItem menuItem) {
int id = menuItem.getItemId();
switch (id) {
case R.id.nav_account:
Toast.makeText(NavigationActivity.this, "Account Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.nav_settings:
Toast.makeText(NavigationActivity.this, "Setting Selected", Toast.LENGTH_SHORT).show();
default:
return true;
}
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(mToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void getCurrentinfo() {
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
for (UserInfo profile : user.getProviderData()) {
// Id of the provider (ex: google.com)
String providerId = profile.getProviderId();
// UID specific to the provider
String uid = profile.getUid();
// Name, email address, and profile photo Url
name = profile.getDisplayName();
photoUrl = profile.getPhotoUrl();
mName.setText(name);
Picasso.with(getApplicationContext())
.load(photoUrl.toString())
.placeholder(R.drawable.logo)
.resize(100, 100)
.transform(new CircleTransform())
.centerCrop()
.into(mPic);
}
}
}
}
在我的getcurrentInfo
函数中,我可以在activity_navigation.xml
中放置我的姓名和个人资料照片,但我想将其放在navigation_header.xml
我尝试更改ImageView
和TextView
的ID以匹配navigation_header
中的ID,但我的应用崩溃了。
答案 0 :(得分:3)
在NavigationView中,您将使用&#34; navigationView.getHeaderView(0)&#34;访问textview和imageview。这一行
快速取样
TextView userName = (TextView) navigationView.getHeaderView(0).findViewById(R.id.username);
userName.setText(new UserPreference().getUser(MainActivity.this).userName);
一切都好!!
答案 1 :(得分:3)
在mNavigationView = (NavigationView) findViewById(R.id.nav_view);
TextView userName = (TextView)mNavigationView.getHeaderView(0).findViewById(R.id.username);
这是因为您在导航视图之前初始化textview 初始化
mName = (TextView)mNavigationView.getHeaderView(0).findViewById(R.id.textView);
mPic = (ImageView) mNavigationView.getHeaderView(0).findViewById(R.id.pPic);
getCurrentinfo();
mNavigationView = (NavigationView) findViewById(R.id.nav_view); //navigation view is initialized here
答案 2 :(得分:3)
这有望100%有效。 如果有效,请告诉我。
`
//Initializing firebase objects
firebaseAuth = FirebaseAuth.getInstance();
firebaseUser = firebaseAuth.getCurrentUser();
//Go to your specific database directory or Child
databaseReference = FirebaseDatabase.getInstance().getReference().child("Students").child(firebaseUser.getUid());
//Connect the views of navigation bar
mNavigationView = (NavigationView) findViewById(R.id.nav_view);
mName = (TextView)mNavigationView.getHeaderView(0).findViewById(R.id.textViewHeaderTitle);
mID = (TextView)mNavigationView.getHeaderView(0).findViewById(R.id.textViewHeaderSID);
mImageView = (CircleImageView) mNavigationView.getHeaderView(0).findViewById(R.id.imageViewHeaderIcon);
//Use you DB reference object and add this method to access realtime data
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//Fetch values from you database child and set it to the specific view object.
mName.setText(dataSnapshot.child("name").getValue().toString());
mID.setText(dataSnapshot.child("sid").getValue().toString());
String link =dataSnapshot.child("profile_picture").getValue().toString();
Picasso.with(getBaseContext()).load(link).into(mImageView);
}
//SIMPLE BRO. HAVE FUN IN ANDROID <3 GOOD LUCK
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
`