gpt4 book ai didi

android - 单击ImageView时如何打开Sliding Navigation Drawer?

转载 作者:行者123 更新时间:2023-11-29 15:39:18 37 4
gpt4 key购买 nike

我正在尝试在我的仪表板 Activity 上实现 Navigation Drawer Activity。在我的应用程序中,我创建了一个主题,其中删除了“Toolbar/ActionBar”,只是在 custom layout 上为我的汉堡菜单放置了一个 ImageView 并将其包含在我的仪表板布局。我想要的是,当单击此 ImageView 时,将显示滑动抽屉。

我尝试做的是通过 com.project.projectname -> New -> activity -> 'Navigation Drawer Activity' 添加一个 Navigation Drawer Activity 并自动添加 NavigationActivity 类, Activity 导航.xml
app_bar_navigation.xml 等

但我想知道如何打开抽屉?在我的 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;
}
}

感谢您的热心帮助。


更新

所以在我的仪表板 Activity 中我添加了以下内容

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);
}
});

然后在我的导航 Activity 中添加一个全局变量

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);
}

我已经设法在我的仪表板 Activity 中调用了 drawer,但是当我单击我的汉堡菜单时,滑动菜单仍然没有显示。应用程序崩溃并显示 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.widget.DrawerLayout.openDrawer(int)' on a null object reference

最佳答案

将此添加到您的 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);
}
});

试试这个,希望它能解决您的问题。

关于android - 单击ImageView时如何打开Sliding Navigation Drawer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43775173/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com