nav header not displaying textvalues from MainActi

2019-09-05 02:33发布

问题:

I have an applcation that utilizes the Navigation drawer and I am supposed to change the nav_header values based on the user's input but I am unable to do that as the values never change. But if i pass this values to the content_main the values show up. this is my layout code

nav_header_main

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="@dimen/nav_header_height"
    android:background="@color/colorPrimary"
    android:gravity="bottom"
    android:theme="@style/ThemeOverlay.AppCompat.Dark"
    tools:layout_editor_absoluteY="81dp">

    <ImageView
        android:id="@+id/userImageNavHeader"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_marginBottom="8dp"
        android:layout_marginStart="12dp"
        android:contentDescription="@string/nav_header_desc"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        app:layout_constraintBottom_toTopOf="@+id/loginBtnNavHeader"
        app:layout_constraintStart_toStartOf="parent"
        app:srcCompat="@drawable/profiledefault" />

    <TextView
        android:id="@+id/userNameNavHeader"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1"
        app:layout_constraintStart_toEndOf="@+id/userImageNavHeader"
        app:layout_constraintTop_toTopOf="@+id/userImageNavHeader"
        tools:text="King Olami" />

    <TextView
        android:id="@+id/userEmailNavHeader"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintStart_toEndOf="@+id/userImageNavHeader"
        app:layout_constraintTop_toBottomOf="@+id/userNameNavHeader"
        tools:text="xxxx@gmail.com" />

    <Button
        android:id="@+id/loginBtnNavHeader"
        android:layout_width="wrap_content"
        android:layout_height="32dp"
        android:layout_marginBottom="8dp"
        android:background="@android:color/transparent"
        android:onClick="loginBtnNavClicked"
        android:text="Login"
        android:textAlignment="center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <ImageButton
        android:id="@+id/addChannelBtn"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:background="@drawable/addchannelbutton"
        android:onClick="addChannelClicked"
        android:padding="5dp"
        android:scaleType="fitXY"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

</android.support.constraint.ConstraintLayout>

and the code for in my activity.main is shown below

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setSupportActionBar(toolbar)
        val toggle = ActionBarDrawerToggle(
                this, drawer_layout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
        drawer_layout.addDrawerListener(toggle)
        toggle.syncState()

LocalBroadcastManager.getInstance(this).registerReceiver(userDataChangeReceiver, IntentFilter(BROADCAST_USER_DATA_CHANGED))
    }

    private val userDataChangeReceiver = object : BroadcastReceiver() {
        override fun onReceive(contect: Context?, intent: Intent?) {
            if (AuthService.isLoggedIn) {
                userNameNavHeader.text = UserDataService.username
                mainChannelName.text = UserDataService.email

                userEmailNavHeader.text = UserDataService.email
                val resourceId = resources.getIdentifier(UserDataService.avatarName, "drawable", packageName)
                userImageNavHeader.setImageResource(resourceId)
userImageNavHeader.setBackgroundColor(UserDataService.returnAvatarColor(UserDataService.avatarColor))
                loginBtnNavHeader.text = "Logout"
            }
        }
    }

further codes would be supplied based on request. Thanks

回答1:

Use this:

val navigationView = findViewById<NavigationView>(R.id.your_nav_view_id);
val header = navigationView?.getHeaderView(0)

val text = header?.findViewById<TextView>(R.id.textView); 
// initialize your widgets in here
// And simply use, "header" prefix before initializing your widgets..