У меня есть приложение, которое использует блок навигации, и я должен изменить значения nav_header на основе ввода пользователя, но я не могу этого сделать, так как значения никогда не меняются.Но если я передам эти значения в content_main
, то значения появятся.это код моего макета
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>
, а код для моей деятельности.main показан ниже
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"
}
}
}
другие кодыбудет поставляться на основании запроса.Спасибо