BottomNavigationView:从网址加载图标不能在慢速互联网上工作

时间:2017-08-01 18:52:45

标签: android picasso bottomnavigationview

我正在尝试从url加载BottomNavigationView的菜单项图标。我使用Picasso Library来加载图片。

    navigationView = (BottomNavigationView)findViewById(R.id.bottom_navigation);
    disableShiftMode(navigationView);

    Menu menu = navigationView.getMenu();



 int i=0;
        for(BottomNavigationItem bottomNavigationItem : bottomNavigationItemList){
         i(i<5){
            final MenuItem navigationMenuItem = menu.getItem(i);

            navigationMenuItem.setTitle(bottomNavigationItem.Name);
            String icon_url = bottomNavigationItem.IconUrl;

            final Target mTarget = new Target() {
                @Override
                public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom loadedFrom) {
                    Log.d("DEBUG", "onBitmapLoaded");
                    BitmapDrawable mBitmapDrawable = new BitmapDrawable(getResources(), bitmap);
                    navigationMenuItem.setIcon(mBitmapDrawable);
                }

                @Override
                public void onBitmapFailed(Drawable drawable) {
                    Log.d("DEBUG", "onBitmapFailed");
                }

                @Override
                public void onPrepareLoad(Drawable drawable) {
                    Log.d("DEBUG", "onPrepareLoad");
                }
            };
            Picasso.with(this).load(icon_url).into(mTarget);
           }
            i +=1;
        }

使用 网速慢 : enter image description here

使用高速互联网:enter image description here

如何确保加载所有图标?

1 个答案:

答案 0 :(得分:0)

您可以在picasso调用中使用 .placeholder(),以便在加载网址图片时显示自定义图片。

这样的事情:

Picasso.with(this) .load(icon_url) .placeholder(R.drawable.image_name) .into(mTarget);