Navigation Drawer with duplicity

Asked

Viewed 93 times

1

I have a Navigationdrawer with two Edittext and I would like these two fields to demonstrate the user data the name and email, but in my Telaprincipal class when I try to play the information for one of the two fields is demonstrated twice the Navigationdrawer.

inserir a descrição da imagem aqui

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

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/telaprincipal_nav_header_et_meuperfil"
        android:layout_marginBottom="2dp"
        android:layout_marginTop="60dp"
        style="@style/TextoMenuDrawerTituloPerfil"/>

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

        <TextView
            android:id="@+id/nav_header_telaprincipal_tv_nome"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            style="@style/TextoMenuDrawerTitulo"
            android:text=""
            android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

        <TextView
            android:id="@+id/nav_header_telaprincipal_tv_email"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="2dp"
            style="@style/TextoMenuDrawerNormal"
            android:text="" />
    </LinearLayout>
</LinearLayout>

Java class

public class TelaPrincipal extends BaseActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    //Componentes da Activity
    private Context mContext;
    private DrawerLayout mDrawerLayout;
    private NavigationView navigationView;
    private View mView;
    private TextView tvNome;
    private TextView tvEmail;

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

        //Titulo da Activity
        setTitle(getString(R.string.telaprincipal_titulo_activity));

        //inicializando as variaveis
        inicializaVariavel();
        inicializaAcao();

        //Método do Navegation Drawer
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.telaprincipal_drawer_aberto, R.string.telaprincipal_drawer_fechado);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        LayoutInflater layoutInflater = getLayoutInflater();
        mView = layoutInflater.inflate(R.layout.nav_header_tela_principal, navigationView, false);

        tvNome = (TextView) mView.findViewById(R.id.nav_header_telaprincipal_tv_nome);
        tvEmail = (TextView) mView.findViewById(R.id.nav_header_telaprincipal_tv_email);

        tvNome.setText("Denis");
        tvNome.setText("[email protected]");

        navigationView.addHeaderView(mView);
    }
  • 1

    Have you looked at this code snippet? tvNome.setText("Denis");&#xA; tvNome.setText("[email protected]");

  • Hello Luídne, as I said this duplicating the Navigation Drawer the information of the name and email is demonstrating, but the duplicity is wrong, I will try to put the image of the app to facilitate understanding.

1 answer

2


Good morning! All right? There are 2 errors in your code, let’s see the first:

You have in your main screen layout, a Navigationview that you gave the id nav_view then note the following, in it you have already set the header, but in your code you are inflating an extra header! Soon there are two headers on your Drawer, or as many as you wish to inflate! There are two ways to fix this, use whatever you think best:

Form 1: Simply remove the header inside the nav_view in the layout removing the line:

    app:headerLayout="@layout/nav_header_tela_principal"

Keeping the code exactly the way it is, you will no longer have the problem of duplicating your header.

Form 2: Removing these lines of code:

    LayoutInflater layoutInflater = getLayoutInflater();
    mView = layoutInflater.inflate(R.layout.nav_header_tela_principal, navigationView, false);

//O código entre estes dois trechos não deve ser alterado!

    navigationView.addHeaderView(mView);

Replacing by:

    mView = navigationView.getHeaderView(0);

with this it will get the first header defined in the layout, from this, follows with your code normally.

Either way will solve your problem.

Already your second problem, note that you are setting the text of TextView tvNome twice as noted by @Luídne!

  • Thank you Furflez solved my problem with the first solution.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.