3

我跟着一个教程,从登录页面将facebook信息导入到一个带有导航的活动中,然后将它放在顶部,如图所示,在改变代码的几天之后,我终于找到了它。 但现在我不能得到原始标题消失。我试过改变代码的每一部分,但我总是以全部或全部结束。我知道两个头比一个好,但这是规则的例外。为什么android导航抽屉会创建两个标题?

此外,我想我不能发布图像,所以,原来的标题是顶部,它应该在哪里。 新的标题与我的脸谱个人资料图片和信息,是在下面。 下面,启动导航抽屉菜单。

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" /> 

应用栏家里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="[email protected]" /> 

</LinearLayout> 
+0

向我们展示activity_home xml文件 –

+0

布局代码添加 – JDKSoftDev

+0

app_bar_home ??? –

回答

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); 

除去下面线remove方法之后看起来象下面

编辑/

public void setNavigationHeader(){ 
    navigation_view = (NavigationView) findViewById(R.id.nav_view); 

查看头= LayoutInflater.from(本).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); 
} 
+0

不起作用。首先,“View header = LayoutInflator ... etc”行必须保留,否则它不知道标题的以下行中的“标题”是什么。findviewbyid。但是当我删除addHeaderView时,它只显示原始的导航标题,而不是我的Facebook信息填充的那个。但是非常感谢你为此付出努力修复这个 – JDKSoftDev

+0

下载和理解代码https://github.com/chrisbanes/cheesesquare –

+0

等等,你的答案恰到好处。我得到它的工作!在最终的代码中,查看标题行必须保留,其余的则调用navigation_view.findviewbyid,而不是header.findviewbyid。但真棒工作,谢谢! – JDKSoftDev

4

u必须头视图进入

navigation_view.removeHeaderView(navigation_view.getHeaderView(0); 

,而不是空

navigation_view.removeHeaderView(null); 
+0

我用removeHeaderView特别删除了什么,或者我应该说什么,调用的原始页眉视图是什么?我可以让命令不出错的唯一方法是,如果它调用删除“标题”,放在后面我声明新的“标题”,这是多余的,因为我不想删除新的“标题”。任何建议? – JDKSoftDev