【问题标题】:Navigator drawer header image does not set in Firebase query导航器抽屉标题图像未在 Firebase 查询中设置
【发布时间】:2019-08-27 06:52:31
【问题描述】:

我的 Firebase 查询中未设置导航器抽屉标题图片。它不会报错,但不会显示加载图片。

科特林;

val navigationView = findViewById(R.id.nav_view) as NavigationView
        val headerview = navigationView.getHeaderView(0)
        var imageViewUser = headerview.imgUserProfilNav as ImageView
        mUserAuthListener = object : FirebaseAuth.AuthStateListener {
            override fun onAuthStateChanged(p0: FirebaseAuth) {
                var kullanici = p0.currentUser
               //  Glide.with(this@ProfilNavMenu).load(R.drawable.ic_action_user).into(imageViewUser)
                headerview.imgUserProfilNav.setImageResource(R.drawable.ic_action_user)
                if (kullanici != null) {
                   // Picasso.with(this@ProfilNavMenu).load(R.drawable.ic_action_user).fit().centerCrop().into(headerview.imgUserProfilNav)

                }
            }
        }

ProfileNavMenu - nav_header_profil_nav_menu.xml ;

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">

    <ImageView
        android:id="@+id/imgUserProfilNav"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/nav_header_desc"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        app:srcCompat="@mipmap/ic_launcher_round" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:text="@string/nav_header_title"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/nav_header_subtitle" />

</LinearLayout>

如何使用 Kotlin 和 Firebase 设置图片?我什至尝试过Glide,但它仍然没有显示任何 url 或可绘制图片的图片。

【问题讨论】:

  • 附加您的 xml 文件。

标签: android firebase kotlin firebase-authentication


【解决方案1】:

而不是这个:

mUserAuthListener = object : FirebaseAuth.AuthStateListener {
            override fun onAuthStateChanged(p0: FirebaseAuth) {
                var kullanici = p0.currentUser
               //  Glide.with(this@ProfilNavMenu).load(R.drawable.ic_action_user).into(imageViewUser)
                headerview.imgUserProfilNav.setImageResource(R.drawable.ic_action_user)
                if (kullanici != null) {
                   // Picasso.with(this@ProfilNavMenu).load(R.drawable.ic_action_user).fit().centerCrop().into(headerview.imgUserProfilNav)

                }
            }
        }

试试这个:

 mUserAuthListener = object : FirebaseAuth.AuthStateListener {
            override fun onAuthStateChanged(p0: FirebaseAuth) {

           Toast.makeText(getContext(), "Set Image here", Toast.LENGTH_SHORT).show();
                }
            }
        }

如果您的 onAuthStatChanged 每次都在运行,这将对您有所帮助。如果每次都没有运行,您将不得不使用不同的方法。

最后运行的代码是:

val navigationView = findViewById(R.id.nav_view) as NavigationView
        val headerview = navigationView.getHeaderView(0)
        var imageViewUser = headerview.imgUserProfilNav as ImageView
              headerview.imgUserProfilNav.setImageResource(R.drawable.ic_action_user)
        mUserAuthListener = object : FirebaseAuth.AuthStateListener {
            override fun onAuthStateChanged(p0: FirebaseAuth) {
                var kullanici = p0.currentUser

                if (kullanici != null) {
                    //set image if there is any specific requirement
                }
            }
        }

【讨论】:

  • 你的意思是你没有得到任何吐司?
  • 这意味着您的图像也不应该设置。请将 headerview.imgUserProfilNav.setImageResource(R.drawable.ic_action_user) 这一行移到 onAuthStateChanged 之外。
  • 是的,你是对的,toast 消息没有显示任何内容.. 我怎样才能获得活动?
  • toast 只会在 onAuthStateChange() 调用时显示,否则它不会在此方法中运行任何代码。
  • 是的,你说得对,这是我的错。我问错了,非常感谢。你节省了我的时间:)
猜你喜欢
  • 1970-01-01
  • 2017-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-22
  • 2019-09-07
相关资源
最近更新 更多