Android导航抽屉点击事件问题
Posted
技术标签:
【中文标题】Android导航抽屉点击事件问题【英文标题】:Android Navigation Drawer click Event Issue 【发布时间】:2020-04-29 19:06:33 【问题描述】:我正在创建一个包含导航抽屉活动的应用程序。我在我的主屏幕中加载不同的片段。现在,当我当时必须调用 Fragment 时它工作正常,但是当我想调用某种服务(如调用、图像选择器)时,我添加了 setNavigationItemSelectedListener,它工作正常,但所有其他片段现在都没有加载。
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow, R.id.nav_franchise,
R.id.nav_tieUP, R.id.nav_giftVoucher, R.id.nav_faq, R.id.nav_privacy_policy,
R.id.nav_tools, R.id.nav_send)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener()
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem)
int id = menuItem.getItemId();
if (id == R.id.callUs)
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + "XXXxxxXXX"));
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions(MainActivity.this, new String[]Manifest.permission.CALL_PHONE,REQUEST_PHONE_CALL);
else
startActivity(intent);
drawer.closeDrawer(GravityCompat.START);
return true;
);
【问题讨论】:
这个问题也可以搜索为-Override default navigation or override setupWithNavController,只是在此处给搜索引擎添加关键字。 【参考方案1】:当您调用NavigationUI.setupWithNavController(navigationView, navController)
时,您是说您希望NavController
处理来自您的NavigationView 的点击事件,并按照the NavigationUI documentation 导航到相关屏幕。这必然会在内部调用setNavigationItemSelectedListener()
。
通过调用setNavigationItemSelectedListener
之后,你删除了原来的监听器,这就是为什么你的其他项目不再做任何事情了。你可以通过调用NavigationUI.onNavDestinationSelected()
来触发默认行为
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem)
int id = menuItem.getItemId();
if (id == R.id.callUs)
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + "XXXxxxXXX"));
if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions(MainActivity.this, new String[]Manifest.permission.CALL_PHONE,REQUEST_PHONE_CALL);
else
startActivity(intent);
else
// Make your navController object final above
// or call Navigation.findNavController() again here
NavigationUI.onNavDestinationSelected(menuItem, navController);
drawer.closeDrawer(GravityCompat.START);
return true;
【讨论】:
【参考方案2】:我尝试制作一个片段并制作了一个抽屉,这是我的代码:
导入android.support.v4.app.FragmentManager;
导入android.support.v4.app.Fragment;
导入android.content.res.Configuration;
导入android.support.design.widget.Snackbar;
导入android.support.v4.widget.DrawerLayout;
导入android.support.v7.app.ActionBarDrawerToggle;
导入android.support.v7.app.AppCompatActivity;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.support.v7.widget.Toolbar;
导入android.support.design.widget.NavigationView;
导入android.view.View;
导入android.widget.LinearLayout;
导入android.widget.RelativeLayout;
公共类 MainActivity 扩展 AppCompatActivity 实现 View.OnClickListener、NavigationView.OnNavigationItemSelectedListener
私有工具栏 mToolBar;
私有 NavigationView mDrawer;
私有 ActionBarDrawerToggle mdrawerToggle;
私有 DrawerLayout mDrawerLayout;
私有RelativeLayout数字前沿;
私有RelativeLayout forwardThinkers;
私有RelativeLayoutpreferedActions;
private LinearLayout innerParent;
公共字符串视图变量;
@覆盖 protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
digitalFrontier.setOnClickListener(this);
forwardThinkers.setOnClickListener(this);
preferedActions.setOnClickListener(this);
@覆盖 public boolean onCreateOptionsMenu(Menu menu)
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
@覆盖 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);
@覆盖 public void onConfigurationChanged(Configuration newConfig)
super.onConfigurationChanged(newConfig);
mdrawerToggle.onConfigurationChanged(newConfig);
private void initViews()
mToolBar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(mToolBar);
mDrawer = (NavigationView) findViewById(R.id.main_drawer);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_parent);
mdrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
mToolBar,
R.string.drawer_open,
R.string.drawer_close);
mDrawerLayout.setDrawerListener(mdrawerToggle);
// indicator based on whether the drawerlayout is in open or closed
mdrawerToggle.syncState();
digitalFrontier = (RelativeLayout) findViewById(R.id.digital_frontier);
forwardThinkers = (RelativeLayout) findViewById(R.id.forward_thinkers);
preferedActions = (RelativeLayout) findViewById(R.id.prefered_actions);
innerParent = (LinearLayout) findViewById(R.id.inner_parent);
mDrawer.setNavigationItemSelectedListener(this);
@覆盖 public void onClick(View v)
if(v.getId() == R.id.digital_frontier || v.getId() == R.id.forward_thinkers ||
v.getId() == R.id.prefered_actions )
detailActivity(v);
private void detailActivity(View view)
switch (view.getId())
case R.id.digital_frontier:
viewVar = "digital frontier";
break;
case R.id.forward_thinkers:
viewVar = "forward thinkers";
break;
case R.id.prefered_actions:
viewVar = "prefered actions";
break;
Snackbar.make(mDrawerLayout, viewVar , Snackbar.LENGTH_LONG).show();
@覆盖 public boolean onNavigationItemSelected(MenuItem menuItem)
selectDrawerItem(menuItem);
return false;
public void selectDrawerItem(MenuItem menuItem)
// Create a new fragment and specify the planet to show based on
// position
Fragment fragment = null;
Class fragmentClass = null;
switch(menuItem.getItemId())
case R.id.agency_menu_item:
fragmentClass = FragmentAgency.class;
break;
try
fragment = (Fragment) fragmentClass.newInstance();
catch (Exception e)
e.printStackTrace();
// Insert the fragment by replacing any existing fragment
FragmentManager fragmentManager = this.getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.fragment_content,
片段).commit();
// Highlight the selected item, update the title, and close the drawer
menuItem.setChecked(true);
setTitle(menuItem.getTitle());
//mDrawer.closeDrawers();
【讨论】:
以上是关于Android导航抽屉点击事件问题的主要内容,如果未能解决你的问题,请参考以下文章
求教android studio大神:用fragment写一个导航栏,在每个fragment里面加listview,能实现点击事件