2016-04-28 45 views
0

我使用畢加索設置了一個drawrlayout背景,並且看着this問題,並按照步驟操作,但它的工作原理是,它發生的奇怪的事情是它沒有更新在活動第一次打開的背景(這意味着我必須關閉它,然後再次打開它,並顯示背景)使用畢加索設置版面背景並沒有更新第一次打開的背景

這是我的佈局

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout 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/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:openDrawer="start"> 
<android.support.v7.widget.Toolbar 
    android:id="@+id/toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="?attr/actionBarSize" 
    android:background="?attr/colorPrimary" /> 
<android.support.design.widget.NavigationView 
    android:id="@+id/nav_view" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    android:fitsSystemWindows="true" 
    app:headerLayout="@layout/nav_header_store" 
    app:menu="@menu/activity_store_drawer" /> 

</android.support.v4.widget.DrawerLayout> 

編輯:建議後 這是我的活動課:

import android.content.Intent; 
import android.graphics.Bitmap; 
import android.graphics.drawable.BitmapDrawable; 
import android.graphics.drawable.Drawable; 
import android.os.Bundle; 
import android.support.design.widget.NavigationView; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentTransaction; 
import android.support.v4.view.GravityCompat; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBarDrawerToggle; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.util.Log; 
import android.view.MenuItem; 

import com.squareup.picasso.Picasso; 
import com.squareup.picasso.Target; 

public class Store extends AppCompatActivity 
     implements NavigationView.OnNavigationItemSelectedListener { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_store); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     getSupportActionBar().setHomeButtonEnabled(false); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(false); 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
       this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
     drawer.setDrawerListener(toggle); 
     toggle.syncState(); 

     NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
     navigationView.setNavigationItemSelectedListener(this); 
    final DrawerLayout layout = (DrawerLayout) Store.this.findViewById(R.id.drawer_layout); 
    Target t = new Target() { 
     @Override 
     public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { 
      final Bitmap p = bitmap; 
      runOnUiThread(new Runnable() { 
       @Override 
       public void run() { 
        layout.setBackgroundDrawable(new BitmapDrawable(Store.this.getResources(), p)); 
       } 
      }); 
      Log.d("TAG", "Loaded"); 
     } 

     @Override 
     public void onBitmapFailed(final Drawable errorDrawable) { 
      Log.d("TAG", "FAILED"); 
     } 

     @Override 
     public void onPrepareLoad(final Drawable placeHolderDrawable) { 
      Log.d("TAG", "Prepare Load"); 
     } 
    }; 

    } 

    @SuppressWarnings("StatementWithEmptyBody") 
    @Override 
    public boolean onNavigationItemSelected(MenuItem item) { 

     return true; 
    } 
} 

回答

0
runOnUiThread(new Runnable() { 
    @Override 
    public void run() { 
     layout.setBackgroundDrawable(new BitmapDrawable(Store.this.getResources(), bitmap)); 
    } 
}); 
+0

我應該將此添加到活動類或目標類嗎? – user1

+0

你需要一些東西,如設置背景n所有... –

+0

你能告訴我,如果編輯是正確的?因爲它仍然在做同樣的事情 – user1

0

畢加索保持弱引用的目標對象,因此它很可能是垃圾收集。因此,創建一個單獨的目標類並設置數據。此外,您正在使用錯誤的佈局進行背景更改。

public class Store extends AppCompatActivity 
    implements NavigationView.OnNavigationItemSelectedListener { 

private DrawerLayout layout; 
private NavigationView navigationView; 

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

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    getSupportActionBar().setHomeButtonEnabled(false); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(false); 

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
      this, drawer, R.string.app_name, R.string.app_name); 
    drawer.addDrawerListener(toggle); 
    toggle.syncState(); 

    navigationView = (NavigationView) findViewById(R.id.nav_view); 
    navigationView.setNavigationItemSelectedListener(this); 
    layout = (DrawerLayout) Store.this.findViewById(R.id.drawer_layout); 

    Picasso.with(this).load("http://a2.mzstatic.com/us/r30/Purple5/v4/58/2e/b9/582eb968-988b-ad42-dee6-92d396cbde5c/icon256.png").into(mTarget); 
} 

@SuppressWarnings("StatementWithEmptyBody") 
@Override 
public boolean onNavigationItemSelected(MenuItem item) { 

    return true; 
} 

final Target mTarget = new Target() { 
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN) 
    @Override 
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom loadedFrom) { 
     Log.d("DEBUG", "onBitmapLoaded"); 
     BitmapDrawable mBitmapDrawable = new BitmapDrawable(getResources(), bitmap); 
     navigationView.setBackground(mBitmapDrawable); 
    } 

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


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