2015-12-22 3 views
7

Nachdem ich meinen Header-Text und das Bild in der NavigationView-Kopfzeile aktualisiert habe, erhalte ich einen doppelten Wert in der NavigationView-Kopfzeile.Doppelter Wert in der NavigationView-Kopfzeile nach dem Aktualisieren des Kopfzeilen-Textes

Hier ist mein Code Teil

public class MainActivity extends AppCompatActivity 
    implements NavigationView.OnNavigationItemSelectedListener { 

boolean doubleBackToExitPressedOnce = false; 
SQLiteHelper dbHelper; 
String setName, setMail; 
AlertDialog.Builder builder, builder_verify; 
public static int count = 0; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    dbHelper = new SQLiteHelper(getApplicationContext()); 
    builder_verify = new AlertDialog.Builder(this);   

    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); 
    View hView = navigationView.inflateHeaderView(R.layout.nav_header_main); 
    ImageView iv = (ImageView) hView.findViewById(R.id.imageView); 
    TextView headerName = (TextView) hView.findViewById(R.id.txtName); 
    TextView headerMail = (TextView) hView.findViewById(R.id.textEmail); 

    iv.setImageResource(R.drawable.logo); 
    headerName.setText("TEST"); 
    headerMail.setText("[email protected]");   
}  
} 

In dem obigen Code i Ansicht hinzugefügt habe meinen Kopftext zu aktualisieren. Ich habe auch Haupt-XML-Code unten

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

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <include 
     layout="@layout/app_bar_main" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <FrameLayout 
     android:id="@+id/flContent" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 
</LinearLayout> 

<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_main" 
    app:menu="@menu/activity_main_drawer" /> 
</android.support.v4.widget.DrawerLayout> 

auch hinzugefügt Navigation Header Layout

nav_header_main.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:gravity="bottom" 
android:orientation="vertical" 
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"> 

<ImageView 
    android:id="@+id/imageView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:paddingTop="@dimen/nav_header_vertical_spacing" 
    android:src="@drawable/logo"/> 

<TextView 
    android:id="@+id/txtName" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:paddingTop="@dimen/nav_header_vertical_spacing" 
    android:text="Sample" 
    android:textAppearance="@style/TextAppearance.AppCompat.Body2" 
    android:textStyle="bold" /> 

<TextView 
    android:id="@+id/textEmail" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Sample Inc." /></LinearLayout> 

auch hinzugefügt Ich habe den Screenshot Referenz

HearderView

hinzugefügt

Momentan sehe ich keine Möglichkeit s der Headeransicht ändern. Irgendwelche Vorschläge, um doppelte Kopfzeile Text und Bild zu vermeiden?

Vielen Dank!

Antwort

8

Gute Frage.

Die Lösung ist, anstelle von Kopf (es ist bereits aufgeblasen!) Durch navigationView.inflateHeaderView(R.layout.nav_header_main); von NavigationView via getHeaderView(int index) Methode aufblasen und füllen Sie es dann.

Hier ist der Code auszuführen:

navigationView.setNavigationItemSelectedListener(this); 
    View hView = navigationView.getHeaderView(0); 
    ImageView iv = (ImageView) hView.findViewById(R.id.imageView); 
    TextView headerName = (TextView) hView.findViewById(R.id.txtName); 
    TextView headerMail = (TextView) hView.findViewById(R.id.txtEmail); 
    iv.setImageResource(R.drawable.logo); 
+0

Vielen Dank :) Konstantin Loginov – nandha

5

In xml fie Sie bereits

app:headerLayout="@layout/nav_header_main" 

für NavigationView und wieder in Java-Klasse gesetzt haben you'r Ihre Ansicht Aufblasen mit

View hView = navigationView.inflateHeaderView(R.layout.nav_header_main); 

So gibt es eine doppelte Werte in NavigationView.

+0

Sie für Ihre wertvolle Antwort Piyush Gupta danken und mich erkennen, wo ich einen Fehler – nandha

+0

Np gemacht wurde. Herzlich willkommen ! ! – Piyush

0

Ich habe eine einfache Lösung für dieses Problem, und es ist für mich gearbeitet. Bevor Kopfzeilenansichten hinzugefügt werden, müssen die aktuellen Kopfzeilenansichten aus der Navigationsleiste entfernt werden. Dies ist das Code-Snippet.

if (navigationView != null) { 
    for (int i = 0; i < navigationView.getHeaderCount(); i++) { 
    if (navigationView.getHeaderView(i) != null) 
    navigationView.getHeaderView(i).setVisibility(View.GONE); 
    } 
    addHeaderLayout(navigationView); 
    } 
Verwandte Themen