我按照教程将登录时的facebook信息推送到带导航的活动,然后将其放入顶部,如图所示,经过几天的代码更改后,我终于开始工作了。 但现在我不能让原来的标题消失。我试过改变代码的每一部分,但我总是最终得到全部或全部。我知道两个人比一个人好,但这是一个例外。
另外,我想我无法发布图片,因此,原始标题位于顶部,应该在哪里。 我的脸谱资料图片和信息的新标题就在下面。 在下面,启动导航抽屉菜单。
public class HomeActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
JSONObject response, profile_pic_data, profile_pic_url;
TextView user_name, user_email, tokens;
ImageView user_picture;
NavigationView navigation_view;
String name;
Button button2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
tokens = (TextView)findViewById(R.id.textView17);
button2 = (Button)findViewById(R.id.button2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Intent intent = getIntent();
String jsondata = intent.getStringExtra("jsondata");
final String uid = intent.getStringExtra("Uid");
setNavigationHeader(); // call setNavigationHeader Method.
setUserProfile(jsondata, uid);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setVisibility(View.INVISIBLE);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Firebase ref = new Firebase("https://luckycashslots.firebaseio.com/data/users/" + MainActivity.uID + "/");
Tokens token = new Tokens("100");
ref.setValue(token);
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
//Toast.makeText(getApplicationContext(), uid, Toast.LENGTH_LONG).show();
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Toast.makeText(getApplicationContext(), name, Toast.LENGTH_LONG).show();
updateText();
}
});
Firebase ref = new Firebase("https://luckycashslots.firebaseio.com/data/users/" + MainActivity.uID + "/");
Firebase tokRef = ref.child("tokens");
//tokRef.setValue(mAuthData.getProvider());
//Tokens token = new Tokens(100);
//ref.setValue(token);
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.child("tokens").getValue() != null) {
name = (String) dataSnapshot.child("tokens").getValue().toString();
tokens.setText(name);
//tokens.setText(dataSnapshot.getValue().toString());
// Toast.makeText(getApplicationContext(), name, Toast.LENGTH_LONG).show();
// System.out.println(dataSnapshot.getValue());
// String woot = dataSnapshot.getValue().toString();
// tokens.setText(woot);
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
Toast.makeText(getApplicationContext(), "couldnt update token text", Toast.LENGTH_LONG).show();
}
});
}
public void updateText(){
Firebase ref = new Firebase("https://luckycashslots.firebaseio.com/data/users/" + MainActivity.uID + "/");
Firebase tokRef = ref.child("tokens");
//tokRef.setValue(mAuthData.getProvider());
// Tokens token = new Tokens(100);
// ref.setValue(token);
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot tokenSnapshot: dataSnapshot.getChildren()){
Tokens token = tokenSnapshot.getValue(Tokens.class);
System.out.println(token.toString());
name = token.toString();
// name = (String) dataSnapshot.child("tokens").getValue();
tokens.setText(name);
}
// name = (String) dataSnapshot.child("tokens").getValue().toString();
// tokens.setText(name);
}
@Override
public void onCancelled(FirebaseError firebaseError) {
Toast.makeText(getApplicationContext(), "couldnt update token text", Toast.LENGTH_LONG).show();
}
});
}
public void setNavigationHeader(){
navigation_view = (NavigationView) findViewById(R.id.nav_view);
navigation_view.removeHeaderView(null);
View header = LayoutInflater.from(this).inflate(R.layout.nav_header_home, null);
navigation_view.addHeaderView(header);
user_name = (TextView) header.findViewById(R.id.username);
user_picture = (ImageView) header.findViewById(R.id.profile_pic);
user_email = (TextView) header.findViewById(R.id.email);
}
public void setUserProfile(String jsondata, String uid){
try
{
response = new JSONObject(jsondata);
user_email.setText(response.get("email").toString());
// user_email.setText(MainActivity.uEmail);
user_name.setText(response.get("name").toString());
// user_name.setText(MainActivity.uName);
profile_pic_data = new JSONObject(response.get("picture").toString());
profile_pic_url = new JSONObject(profile_pic_data.getString("data"));
Picasso.with(this).load(profile_pic_url.getString("url")).into(user_picture);
}
catch
(Exception e) {
e.printStackTrace();
}
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_slots) {
// Handle the camera action1
Intent intent2 = new Intent(this, SlotPageView.class);
startActivity(intent2);
} else if (id == R.id.nav_spin) {
Intent intent2 = new Intent(this, DailySpinActivity.class);
startActivity(intent2);
} else if (id == R.id.nav_offers) {
} else if (id == R.id.nav_prizes) {
Intent intent2 = new Intent(this, PrizesActivity.class);
startActivity(intent2);
} else if (id == R.id.nav_winners) {
Intent intent2 = new Intent(this, WinnersActivity.class);
startActivity(intent2);
} else if (id == R.id.nav_stats) {
} else if (id == R.id.nav_account) {
Intent intent2 = new Intent(this, AccountActivity.class);
startActivity(intent2);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
activity_home.xml
<?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_layout"
android:layout_width="match_parent" android:layout_height="match_parent"
android:fitsSystemWindows="true" tools:openDrawer="start">
<include layout="@layout/app_bar_home" android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView android:id="@+id/nav_view"
android:layout_width="wrap_content" android:layout_height="match_parent"
android:layout_gravity="start" android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_home" app:menu="@menu/activity_home_drawer" />
App bar home xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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:layout_width="match_parent"
android:layout_height="match_parent" android:fitsSystemWindows="true"
tools:context="learn2crack.learn2crackfb.HomeActivity">
<android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar android:id="@+id/toolbar"
android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_home" />
<android.support.design.widget.FloatingActionButton android:id="@+id/fab"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
导航标题主页xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="@dimen/nav_header_height"
android:background="@drawable/side_nav_bar"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:theme="@style/ThemeOverlay.AppCompat.Dark" android:orientation="vertical"
android:gravity="bottom">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/profile_pic"
android:layout_width="80dp"
android:layout_height="80dp"
android:paddingTop="@dimen/nav_header_vertical_spacing"
android:src="@android:drawable/sym_def_app_icon"
/>
<TextView android:id="@+id/username"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:paddingTop="@dimen/nav_header_vertical_spacing"
android:text="Android Studio"
android:textSize="14dp"
android:textAppearance="@style/TextAppearance.AppCompat.Body1"
android:textColor="#000000" />
<TextView
android:id="@+id/email"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="android.studio@android.com" />
</LinearLayout>
答案 0 :(得分:4)
你必须将标题视图传递给
navigation_view.removeHeaderView(navigation_view.getHeaderView(0);
而不是null
navigation_view.removeHeaderView(null);
答案 1 :(得分:1)
使用app:headerLayout="@layout/nav_header_home"
添加标题时,如下所示
<android.support.design.widget.NavigationView android:id="@+id/nav_view"
android:layout_width="wrap_content" android:layout_height="match_parent"
android:layout_gravity="start" android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_home" app:menu="@menu/activity_home_drawer" />
然后无需再从代码中添加标题
从setNavigationHeader()
方法
navigation_view.removeHeaderView(null);
View header = LayoutInflater.from(this).inflate(R.layout.nav_header_home, null);
navigation_view.addHeaderView(header);
删除方法后如下所示
编辑/
public void setNavigationHeader(){
navigation_view = (NavigationView) findViewById(R.id.nav_view);
查看header = LayoutInflater.from(this).inflate(R.layout.nav_header_home,null);
user_name = (TextView) navigation_view.findViewById(R.id.username);
user_picture = (ImageView) navigation_view.findViewById(R.id.profile_pic);
user_email = (TextView) navigation_view. findViewById(R.id.email);
}
答案 2 :(得分:0)
尝试一下,它是可行且经过测试的。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
navigationView.removeHeaderView(navigationView.getHeaderView(0));
View headerLayout = navigationView.inflateHeaderView(R.layout.nav_header_main);
ImageView user_dp = headerLayout.findViewById(R.id.user_dp);
TextView user_fullname = headerLayout.findViewById(R.id.user_fullname);
TextView user_email = headerLayout.findViewById(R.id.user_email);
user_dp.setBackgroundResource(R.drawable.user);
user_fullname.setText(fullName);
user_email.setText(uemail);
}