2017-07-25 35 views
1

我一直按步骤this tutorial,但是当我想加载配置文件图像和标题背景没有显示。我添加了互联网许可和wifi已打开并初始化drawerimageloader。问题是什么?无法从URL加载配置文件图像和标题背景

主要活动:

import android.net.Uri; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.view.View; 
import android.widget.ImageView; 

import com.mikepenz.materialdrawer.DrawerBuilder; 
import com.mikepenz.materialdrawer.AccountHeader; 
import com.mikepenz.materialdrawer.AccountHeaderBuilder; 
import com.mikepenz.materialdrawer.model.ProfileDrawerItem; 
import com.mikepenz.materialdrawer.model.interfaces.IProfile; 

import com.bumptech.glide.Glide; 


public class MainActivity extends AppCompatActivity {; 

Toolbar toolbar; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    new DrawerBuilder().withActivity(this).build(); 

    AccountHeader headerResult = new AccountHeaderBuilder() 
      .withActivity(this) 
      .addProfiles(
        new ProfileDrawerItem().withName("Mike 
    Penz").withEmail("[email protected]").withIcon(Uri. 
    parse("https://avatars3.githubusercontent.com/u/887462?v=3&s=460")) 
      ) 
      .withOnAccountHeaderListener(new 
    AccountHeader.OnAccountHeaderListener() { 
       @Override 
       public boolean onProfileChanged(View view, IProfile profile, 
    boolean currentProfile) { 
        return false; 
       } 
      }) 
      .build(); 

    new DrawerBuilder() 
      .withActivity(this) 
      .withAccountHeader(headerResult) 
      .build(); 

    ImageView view = headerResult.getHeaderBackgroundView(); 
    Glide.with(this).load("www.api.androidhive.info/images/nav-menu-header- 
    bg.jpg").into(view); 
    } 

    } 

自定义活动:

import android.app.Application; 
import android.content.Context; 
import android.graphics.drawable.Drawable; 
import android.net.Uri; 
import android.widget.ImageView; 

import com.bumptech.glide.Glide; 
import com.mikepenz.iconics.IconicsDrawable; 
import com.mikepenz.materialdrawer.util.AbstractDrawerImageLoader; 
import com.mikepenz.materialdrawer.util.DrawerImageLoader; 
import com.mikepenz.materialdrawer.util.DrawerUIUtils; 

/** 
* Created by mikepenz on 27.03.15. 
* 
*/ 
public class CustomApplication extends Application { 

@Override 
public void onCreate() { 
    super.onCreate(); 

    /* 
    //initialize and create the image loader logic 
    DrawerImageLoader.init(new AbstractDrawerImageLoader() { 
     @Override 
     public void set(ImageView imageView, Uri uri, Drawable placeholder) 
    { 

    Picasso.with(imageView.getContext()).load(uri).placeholder(placeholder) 
    .into(imageView); 
     } 
     @Override 
     public void cancel(ImageView imageView) { 
      Picasso.with(imageView.getContext()).cancelRequest(imageView); 
     } 
    }); 
    */ 

    //initialize and create the image loader logic 
    DrawerImageLoader.init(new AbstractDrawerImageLoader() { 
     @Override 
     public void set(ImageView imageView, Uri uri, Drawable placeholder, 
    String tag) { 
    Glide.with(imageView.getContext()).load(uri) 
    .placeholder(placeholder).into(imageView); 
     } 

     @Override 
     public void cancel(ImageView imageView) { 
      Glide.clear(imageView); 
     } 

     @Override 
     public Drawable placeholder(Context ctx, String tag) { 
      //define different placeholders for different imageView targets 
      //default tags are accessible via the DrawerImageLoader.Tags 
      if (DrawerImageLoader.Tags.PROFILE.name().equals(tag)) { 
       return DrawerUIUtils.getPlaceHolder(ctx); 
      } else if 
(DrawerImageLoader.Tags.ACCOUNT_HEADER.name().equals(tag)) { 
       return new IconicsDrawable(ctx).iconText(" 
").backgroundColorRes(com.mikepenz.materialdrawer.R.color.primary) 
.sizeDp(56); 
      } else if ("customUrlItem".equals(tag)) { 
       return new IconicsDrawable(ctx).iconText(" 
").backgroundColorRes(R.color.md_red_500).sizeDp(56); 
      } 

      //we use the default one for 
      //DrawerImageLoader.Tags.PROFILE_DRAWER_ITEM.name() 

      return super.placeholder(ctx, tag); 
     } 
    }); 
} 
} 

清单:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="ir.techend.yusof.ghaza"> 

<uses-permission android:name="android.permission.INTERNET"/> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:roundIcon="@mipmap/ic_launcher_round" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity 
     android:name=".MainActivity" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 
</manifest> 
+0

你指的是什么教程? – DroiDev

+0

mikepenz教程 – Yusof

+0

我应该知道这是什么?你能提出一个参考吗?你的问题..... nm - >你没有 – DroiDev

回答

1

请删除重复的

new DrawerBuilder().withActivity(this).build(); 

,你必须在你的MainActivity,你只能.build()一个Drawer(在某些情况下.append()另一个)

Drawer的代码将确保不会2 DrawerLayout的充气。

此外,我检查标题图像的网址。这个不存在。 (http://www.api.androidhive.info/images/nav-menu-header-bg.jpg返回404)

+0

嘿迈克,我有网络问题,并没有添加init映像加载程序。现在我很好。感谢你的图书馆;)。 – Yusof

0

使用毕加索库为

Picasso.with(YourActivityName.this).load(url_img).into(imageView); 

,并在您gradle这个依赖把

compile 'com.squareup.picasso:picasso:2.5.2'