gpt4 book ai didi

android - 抽屉导航 : How do I set the selected item at startup?

转载 作者:IT老高 更新时间:2023-10-28 12:52:49 24 4
gpt4 key购买 nike

我的代码完美运行:每次单击抽屉导航中的项目时,都会选择该项目。

当然,我想使用默认 fragment (主页)启动应用程序,但 Navigation Drawer 没有选择该项目。如何以编程方式选择该项目?

public class BaseApp extends AppCompatActivity {

//Defining Variables
protected String LOGTAG = "LOGDEBUG";
protected Toolbar toolbar;
protected NavigationView navigationView;
protected DrawerLayout drawerLayout;

private DateManager db = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.base_layout);

navigationView = (NavigationView) findViewById(R.id.navigation_view);


// set the home/dashboard at startup

FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frame, new DashboardFragment());
fragmentTransaction.commit();

setNavDrawer();
}

private void setNavDrawer(){

// Initializing Toolbar and setting it as the actionbar
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

//Initializing NavigationView

//Setting Navigation View Item Selected Listener to handle the item click of the navigation menu
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {

// This method will trigger on item Click of navigation menu
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {


//Checking if the item is in checked state or not, if not make it in checked state

// I THINK THAT I NEED EDIT HERE...

if (menuItem.isChecked()) menuItem.setChecked(false);
else menuItem.setChecked(true);

//Closing drawer on item click
drawerLayout.closeDrawers();


//Check to see which item was being clicked and perform appropriate action
switch (menuItem.getItemId()) {

//Replacing the main content with ContentFragment

case R.id.home:

DashboardFragment dashboardFragment = new DashboardFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frame, dashboardFragment,"DASHBOARD_FRAGMENT");
fragmentTransaction.commit();
return true;
[...]

我认为我需要在这里编辑:

if (menuItem.isChecked()) menuItem.setChecked(false);
else menuItem.setChecked(true);

或在应用启动时的 onCreate 中使用 FragmentTransaction。

感谢您的支持。

最佳答案

使用下面的代码:

navigationView.getMenu().getItem(0).setChecked(true);

调用setNavDrawer();

后调用该方法

getItem(int index) 方法获取 MenuItem 然后您可以在该 MenuItem 上调用 setChecked(true); ,你剩下要做的就是找出默认的元素索引,并将0替换为该索引。

您可以通过调用选择(突出显示)项目

onNavigationItemSelected(navigationView.getMenu().getItem(0));

这是一个引用链接:http://thegeekyland.blogspot.com/2015/11/navigation-drawer-how-set-selected-item.html

编辑不适用于 nexus 4,支持库修订版 24.0.0。我建议使用

navigationView.setCheckedItem(R.id.nav_item);

下面的@kingston 回答了。

关于android - 抽屉导航 : How do I set the selected item at startup?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31233279/

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