如何解决Xamarin.Android-ToolbarNavigationClickListener
我的问题类似于此问题Android - Switch ActionBar Back Button to Navigation Button,但是我在使用更改为c#的Java代码时遇到了问题。
import android.support.v7.app.ActionBarDrawerToggle
import android.support.v4.widget.DrawerLayout
ActionBarDrawerToggle mDrawerToggle;
DrawerLayout drawerLayout;
private boolean mToolBarNavigationListenerIsRegistered = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setSupportActionBar(mToolbar);
getSupportActionBar().setdisplayShowTitleEnabled(false);
// Get DrawerLayout ref from layout
drawerLayout = (DrawerLayout)findViewById(R.id.drawer);
// Initialize ActionBarDrawerToggle,which will control toggle of hamburger.
// You set the values of R.string.open and R.string.close accordingly.
// Also,you can implement drawer toggle listener if you want.
mDrawerToggle = new ActionBarDrawerToggle (this,drawerLayout,mToolbar,R.string.open,R.string.close);
// Setting the actionbarToggle to drawer layout
drawerLayout.addDrawerListener(mDrawerToggle);
// Calling sync state is necessary to show your hamburger icon...
// or so I hear. Doesn't hurt including it even if you find it works
// without it on your test device(s)
mDrawerToggle.syncState();
}
/**
* To be semantically or contextually correct,maybe change the name
* and signature of this function to something like:
*
* private void showBackButton(boolean show)
* Just a suggestion.
*/
private void enableViews(boolean enable) {
// To keep states of ActionBar and ActionBarDrawerToggle synchronized,// when you enable on one,you disable on the other.
// And as you may notice,the order for this operation is disable first,then enable - VERY VERY IMPORTANT.
if(enable) {
//You may not want to open the drawer on swipe from the left in this case
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
// Remove hamburger
mDrawerToggle.setDrawerIndicatorEnabled(false);
// Show back button
getSupportActionBar().setdisplayHomeAsUpEnabled(true);
// when DrawerToggle is disabled i.e. setDrawerIndicatorEnabled(false),navigation icon
// clicks are disabled i.e. the UP button will not work.
// We need to add a listener,as in below,so DrawerToggle will forward
// click events to this listener.
if(!mToolBarNavigationListenerIsRegistered) {
mDrawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Doesn't have to be onBackpressed
onBackpressed();
}
});
mToolBarNavigationListenerIsRegistered = true;
}
} else {
//You must regain the power of swipe for the drawer.
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
// Remove back button
getSupportActionBar().setdisplayHomeAsUpEnabled(false);
// Show hamburger
mDrawerToggle.setDrawerIndicatorEnabled(true);
// Remove the/any drawer toggle listener
mDrawerToggle.setToolbarNavigationClickListener(null);
mToolBarNavigationListenerIsRegistered = false;
}
// So,one may think "Hmm why not simplify to:
// .....
// getSupportActionBar().setdisplayHomeAsUpEnabled(enable);
// mDrawer.setDrawerIndicatorEnabled(!enable);
// ......
// To re-iterate,the order in which you enable and disable views IS important #dontSimplify.
}
因此,我的问题是如何将按钮导航单击侦听器更改为从用于打开导航抽屉的Hambuger到用于关闭活动的后退按钮。是否可以像Java代码一样使用ToolbarNavigationClickListener,如果可以,如何使用?
解决方法
使用您提供的链接和描述,您似乎想要将后退按钮更改为汉堡包图标,并具有此汉堡包的点击事件。
在Xamarin.Android中,您可以使用ActionBarDrawerToggle
。创建自己的MyActionBarDrawerToggle
。
internal class MyActionBarDrawerToggle : ActionBarDrawerToggle
{
NavigationDrawerActivity owner;
public MyActionBarDrawerToggle (NavigationDrawerActivity activity,DrawerLayout layout,int imgRes,int openRes,int closeRes)
: base (activity,layout,imgRes,openRes,closeRes)
{
owner = activity;
}
public override void OnDrawerClosed (View drawerView)
{
owner.ActionBar.Title = owner.Title;
owner.InvalidateOptionsMenu ();
}
public override void OnDrawerOpened (View drawerView)
{
owner.ActionBar.Title = owner.mDrawerTitle;
owner.InvalidateOptionsMenu ();
}
}
然后为该布局设置mDrawerToggle。
mDrawerToggle = new MyActionBarDrawerToggle (this,mDrawerLayout,Resource.Drawable.ic_drawer,Resource.String.drawer_open,Resource.String.drawer_close);
mDrawerLayout.SetDrawerListener (mDrawerToggle);
if (savedInstanceState == null) //first launch
selectItem (0);
为RecyclerView创建点击侦听器。
public interface OnItemClickListener{
void OnClick(View view,int position);
}
然后在布局中实现它。
public void OnClick (View view,int position)
{
selectItem (position);
}
屏幕截图:
您可以从链接下载源文件以供参考。
https://docs.microsoft.com/en-us/samples/xamarin/monodroid-samples/android50-navigationdrawer/
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。