我正在使用mikepenz 的 Material Drawer。我试图实现的是为抽屉实现一个侦听器,因此当它被打开时,我可以向 API 发出请求并使用从 API 返回的数据更新标头。我有两个问题: 1. 当我尝试实现监听器时,它不会被触发。我的听众看起来像这样:
result.setOnDrawerNavigationListener(new Drawer.OnDrawerNavigationListener(){
@Override
public boolean onNavigationClickListener(View view) {
//If the drawer is not yet opened but at the end of the action it will be
if (!result.isDrawerOpen())
{
getCurrentUser(savedInstanceState);
return true;
}
else
onBackPressed();
return false;
}
});
考虑到我有一个自定义标题,我填充如下:
最终视图 sidebarHeader = factory.inflate(R.layout.sidebar_header, null); TextView 用户名 = (TextView) sidebarHeader.findViewById(R.id.username); 用户名.setText(u.getUsername()); CircleImageView profilePic = (CircleImageView) sidebarHeader.findViewById(R.id.profilePic); Picasso.with(this).load(BuildConfig.BASE_API_URL + profilePictureUrl).fit().placeholder(R.drawable.default_user_icon).error(R.drawable.default_user_icon).into(profilePic);
TextView email = (TextView) sidebarHeader.findViewById(R.id.email); email.setText(u.getEmail());
更新信息的最佳方式是什么?这种方法正确吗?
result.updateName(R.id.username, new StringHolder(response.body().getUsername()));
String profilePictureUrl = response.body().getProfilePicture();
CircleImageView profilePic = (CircleImageView) findViewById(R.id.profilePic);
Picasso.with(getApplicationContext()).load(BuildConfig.BASE_API_URL + profilePictureUrl).fit().placeholder(R.drawable.default_user_icon).error(R.drawable.default_user_icon).into(profilePic);
drawer.updateIcon(R.id.profilePic, new ImageHolder(profilePictureUrl));
或者我应该重新生成标题?
抽屉格式:
result = new DrawerBuilder()
.withActivity(this)
.withHeader(sidebarHeader)
.withToolbar(toolbar)
.addDrawerItems(
new PrimaryDrawerItem().withIdentifier(0).withName(R.string.dashboard).withIcon(FontAwesome.Icon.faw_tachometer),
new PrimaryDrawerItem().withIdentifier(1).withName(R.string.point_of_sale).withIcon(FontAwesome.Icon.faw_file_text_o),
new ExpandableDrawerItem().withName(R.string.ecommerce).withIcon(FontAwesome.Icon.faw_shopping_cart).withSubItems(
new SecondaryDrawerItem().withIdentifier(2).withName(R.string.shops)
),
new PrimaryDrawerItem().withIdentifier(3).withName(R.string.clients).withIcon(FontAwesome.Icon.faw_briefcase),
new PrimaryDrawerItem().withIdentifier(4).withName(R.string.invoices).withIcon(FontAwesome.Icon.faw_list_alt),
new PrimaryDrawerItem().withIdentifier(5).withName(R.string.payment_requests).withIcon(R.drawable.payment_request),
new ExpandableDrawerItem().withName(R.string.catalog).withIcon(FontAwesome.Icon.faw_folder_open).withSubItems(
new SecondaryDrawerItem().withIdentifier(6).withName(R.string.products),
new SecondaryDrawerItem().withIdentifier(7).withName(R.string.categories)
),
new PrimaryDrawerItem().withIdentifier(8).withName(R.string.settings).withIcon(FontAwesome.Icon.faw_cog),
new ExpandableDrawerItem().withName(R.string.reports).withIcon(FontAwesome.Icon.faw_list_ol).withSubItems(
new SecondaryDrawerItem().withIdentifier(9).withName(R.string.transactions),
new SecondaryDrawerItem().withIdentifier(10).withName(R.string.orders)
)
)
.addStickyDrawerItems(new PrimaryDrawerItem().withIdentifier(11).withName(R.string.sign_out).withIcon(FontAwesome.Icon.faw_lock))
.withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
@Override
public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
// do something with the clicked item :D
if (drawerItem != null) {
Fragment fragment = null;
switch ((int) drawerItem.getIdentifier()){
case 0:
fragment = new DashboardFragment();
break;
case 8:
fragment = new SettingsFragment();
break;
case 11:
Intent i = new Intent(getApplicationContext(), SplashScreenActivity.class);
MainProvider.sharedInstance().logOut(MainActivity.this);
Toast.makeText(MainActivity.this, "Logged Out", Toast.LENGTH_SHORT).show();
startActivity(i);
break;
}
if(fragment != null){
getSupportFragmentManager().beginTransaction().replace(R.id.mainFragment,(android.support.v4.app.Fragment) fragment, Integer.toString((int) drawerItem.getIdentifier())).addToBackStack(null).commit();
}
}
return false;
}
})
.withSavedInstance(savedInstanceState)
.build();
感谢您的时间!
更新:
result.updateName(R.id.username, new StringHolder(response.body().getData().getUsername()));
String profilePictureUrl = response.body().getData().getSettings().getProfilePicture();
CircleImageView profilePic = (CircleImageView) findViewById(R.id.profilePic);
Picasso.with(getApplicationContext()).load(BuildConfig.BASE_API_URL + profilePictureUrl).fit().placeholder(R.drawable.default_user_icon).error(R.drawable.default_user_icon).into(profilePic);
result.updateIcon(R.id.profilePic, new ImageHolder(profilePictureUrl));
result.updateName(R.id.email, new StringHolder(response.body().getData().getEmail()));