单击 ImageView 时如何打开滑动导航抽屉?
Posted
技术标签:
【中文标题】单击 ImageView 时如何打开滑动导航抽屉?【英文标题】:How to open the Sliding Navigation Drawer when ImageView is clicked? 【发布时间】:2017-10-02 04:16:30 【问题描述】:我正在尝试在我的仪表板Activity
上实现Navigation Drawer Activity
。在我的应用程序中,我创建了一个主题,其中删除了“工具栏/操作栏”,并将我的汉堡菜单的 ImageView
放在 custom layout
上,并将其包含在我的仪表板布局中。我想要的是,当单击此ImageView
时,将显示滑动抽屉。
我尝试通过 com.project.projectname -> 新建 -> 活动 -> 'Navigation Drawer Activity' 添加 Navigation Drawer Activity
并自动添加 NavigationActivity class , activity_navigation.xml
app_bar_navigation.xml, etc
。
但我想知道如何打开抽屉?
在我的Dashboard Activity
我添加了这个
public class DashboardActivity extends AppCompatActivity
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.dashboard);
View side_tab = findViewById(R.id.side_tab);
expanded_menu = (ImageView) side_tab.findViewById(R.id.expanded_menu);
expanded_menu.setOnClickListener(new View.OnClickListener()
public void onClick(View v)
// open drawer here
);
另外,在NavigationActivity
中它使用ActionBarDrawerToggle
,我不能使用它,因为我没有应用ActionBar
。但是我可以使用什么替代方案?
这是我的NavigationActivity
public class NavigationActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation);
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);
@Override
public void onBackPressed()
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START))
drawer.closeDrawer(GravityCompat.START);
else
super.onBackPressed();
@Override
public boolean onCreateOptionsMenu(Menu menu)
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.navigation, menu);
return true;
@Override
public boolean onOptionsItemSelected(MenuItem item)
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in androidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings)
return true;
return super.onOptionsItemSelected(item);
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item)
// Handle navigation view item clicks here.
int id = item.getItemId();
Intent goToSreen;
if (id == R.id.nav_dashboard)
goToSreen = new Intent(NavigationActivity.this, DashboardActivity.class);
startActivity(goToSreen);
else if (id == R.id.nav_others)
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
感谢您的热心帮助。
更新
所以在我的仪表板活动中,我添加了以下内容
NavigationActivity navigationActivity;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.dashboard);
View side_tab = findViewById(R.id.side_tab);
expanded_menu = (ImageView) side_tab.findViewById(R.id.expanded_menu);
expanded_menu.setOnClickListener(new View.OnClickListener()
public void onClick(View v)
navigationActivity = new NavigationActivity() ;
navigationActivity.drawer.openDrawer(GravityCompat.START);
);
在我的导航活动中,我添加了一个全局变量
public class NavigationActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener
DrawerLayout drawer;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
我已经设法在我的仪表板活动中调用了drawer
,但是当我单击我的汉堡菜单时,滑动菜单仍然不显示。应用崩溃并显示java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.widget.DrawerLayout.openDrawer(int)' on a null object reference
【问题讨论】:
创建导航视图的对象并在 onclick 方法中应用抽屉.openDrawer(GravityCompat.START); 根据需要从布局中删除默认操作栏 @AashutoshKumar 我应该把抽屉放在哪里。openDrawer(GravityCompat.START);在我的仪表板活动中?我已经从布局中删除了默认操作栏。 @AashutoshKumar 我现在有 2 个活动,我的仪表板活动和 NavigationActivity。我应该在哪里编辑它? 是的,首先你要创建navigationdarwer DrawerLayout 的对象drawer = (DrawerLayout) findViewById(R.id.nav_drawer); 【参考方案1】:将此添加到您的 NavigationActivity
public class NavigationActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener
//Add this line
public static DrawerLayout drawer;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation);
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();
.......
接下来改变这个
expanded_menu.setOnClickListener(new View.OnClickListener()
public void onClick(View v)
// open drawer here
NavigationActivity. drawer.openDrawer(GravityCompat.START);
);
试试这个希望它能解决你的问题。
【讨论】:
我有一个错误`java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.widget.DrawerLayout.openDrawer(int)' on an null object reference` 你的抽屉对象是空的,检查一下为什么它是空的? @mori 现在检查我的答案。 我没有使用 ActionBar 和工具栏。我尝试添加NavigationActivity. drawer.openDrawer(GravityCompat.START);
,但它说'无法解析符号抽屉'【参考方案2】:
在NavigationActivity
中添加以下代码:
DrawerLayout mDrawerLayout; // Declare globally
在onCreate()
:
mDrawerLayout = (DrawerLayout) findViewById(R.id.nav_drawer);
expanded_menu = (ImageView) side_tab.findViewById(R.id.expanded_menu);
expanded_menu.setOnClickListener(new View.OnClickListener()
public void onClick(View v)
// open drawer here
mDrawerLayout.openDrawer(Gravity.LEFT);
);
如果抽屉是打开的,关闭它onBackPressed()
:
@Override
public void onBackPressed()
if (mDrawerLayout.isDrawerOpen(Gravity.LEFT))
mDrawerLayout.closeDrawer(Gravity.LEFT);
return;
希望这会有所帮助。
【讨论】:
【参考方案3】:我将我的NavigationActivity
扩展为我的DashboardActivity
,如下所示
public class DashboardActivity extends NavigationActivity
side_tab = findViewById(R.id.side_tab);
expanded_menu = (ImageView) side_tab.findViewById(R.id.expanded_menu);
expanded_menu.setOnClickListener(new View.OnClickListener()
public void onClick(View v)
side_tab.setVisibility(View.INVISIBLE);
drawer.openDrawer(GravityCompat.START);
);
然后按照@Hassan Usman 和@tahsinRupam 的回答进行操作
之后我在我的dashboard.xml
中添加了DrawerLayout
【讨论】:
以上是关于单击 ImageView 时如何打开滑动导航抽屉?的主要内容,如果未能解决你的问题,请参考以下文章