导航抽屉标题Imageview没有更新

时间:2018-10-17 11:50:46

标签: android android-layout imageview navigation-drawer picasso

我正在尝试使用回调接口将片段中的图像加载到导航抽屉标题中。我调试了该应用程序,以发现传递的字符串URL是有效的,但是picasso无法将图像加载到标题Imageview中。

这是我的回调接口代码:

    @Override
public void changeUserStatus(boolean isLogin, String email,String profilePhotoUrl) {
    if (isLogin) {
        logoutBTN.setText(R.string.logout);
        //Toast.makeText(MainScreenActivity.this,email,Toast.LENGTH_SHORT).show();
        userEmail.setText(email);
    }

    if (!profilePhotoUrl.equals("")) {
        Picasso.with(getApplicationContext()).load(profilePhotoUrl).placeholder(R.drawable.logo).noFade().into(navImageView);
    }
    else {
        navImageView.setImageResource(R.drawable.logo);
    }
}

这是我的导航标题ImageView的xml布局:

  <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="200dp"
    android:background="@color/White"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal"
        android:orientation="vertical">
        <de.hdodenhof.circleimageview.CircleImageView
            android:id="@+id/profile_image"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:layout_marginTop="10dp"
            android:src="@drawable/logo">
        </de.hdodenhof.circleimageview.CircleImageView>
        <TextView
            android:gravity="center_horizontal"
            android:id="@+id/email_header"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:text=""
            android:textColor="@color/Black"
            android:textSize="14sp" />
        <TextView
            android:id="@+id/logoutBTN"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:gravity="center_horizontal"
            android:paddingBottom="4dp"
            android:text="@string/login_txt"
            android:textAllCaps="true"
            android:textColor="@color/Black"
            android:textSize="16sp"
            android:textStyle="bold"
            android:typeface="sans" />
    </LinearLayout>
</RelativeLayout>

我已经在我的Activity onCreate()中初始化了导航标题ImageView:

    navigationView.setNavigationItemSelectedListener(this);
    navigationView.inflateHeaderView(R.layout.nav_header);
    View header = navigationView.getHeaderView(0);
    userEmail = (TextView) header.findViewById(R.id.email_header);
    logoutBTN = (TextView) header.findViewById(R.id.logoutBTN);
    navImageView = (ImageView) header.findViewById(R.id.profile_image);

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

遇到类似问题时,我向实际的导航视图本身添加了一个Android id,然后在导航标题初始化之前就将变量初始化为NavigationView。

View navView = (NavigationView) findViewById(R.id.nav_view);
View navHead = (NavigationHeader) navView.getHeaderView(0);
Textview tvEmail = (TextView) navHead.findViewById(R.id.userEmail); 
tvEmail.setText("theusersemail@whatever.com");