gpt4 book ai didi

android - 如何选择抽屉导航中的第一项并在应用程序启动时打开 fragment

转载 作者:IT老高 更新时间:2023-10-28 23:26:43 25 4
gpt4 key购买 nike

我用 NavigationView 创建了 MainActivity。当 Activity 打开时,我想自动选择抽屉导航中的第一个项目并在该项目下打开 Fragment。我搜索了很多,但没有找到任何合适的解决方案。

这样做的正确方法是什么?

主要 Activity :

public class MainActivity extends AppCompatActivity implements Config {

private NavigationView navigationView;
private DrawerLayout drawerLayout;

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

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

toolbar.setTitleTextColor(getResources().getColor(R.color.colorIcons));

if (null != getSupportActionBar())
getSupportActionBar().setLogo(R.drawable.ic_blogger_white);

//Start PostListFragmentWebView
/*PostListFragmentWebView postListFragmentWebView = new PostListFragmentWebView();
getSupportFragmentManager().beginTransaction()
.replace(R.id.frame, postListFragmentWebView)
.commit();*/

//Initializing NavigationView
navigationView = (NavigationView) findViewById(R.id.navigationView);

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

@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {


//Checking if the item is in checked state or not, if not set it to checked state.
if (menuItem.isChecked()) menuItem.setChecked(false);
else menuItem.setChecked(true);

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

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

case R.id.posts:
PostListFragmentWebView postListFragment = new PostListFragmentWebView();
getSupportFragmentManager().beginTransaction()
.replace(R.id.frame, postListFragment)
.commit();
return true;

case R.id.pages:
PageListFragmentWebView pagetListFragment = new PageListFragmentWebView();
getSupportFragmentManager().beginTransaction()
.replace(R.id.frame, pagetListFragment)
.commit();
return true;

case R.id.blog:
BlogInfoFragmentWebView blogInfoFragment = new BlogInfoFragmentWebView();
getSupportFragmentManager().beginTransaction()
.replace(R.id.frame, blogInfoFragment)
.commit();
return true;

default:
Toast.makeText(getApplicationContext(), getResources().getString(R.string.drawer_error), Toast.LENGTH_SHORT).show();
return true;

}

}
});

// Initializing Drawer Layout and ActionBarToggle
drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
ActionBarDrawerToggle actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open_drawer, R.string.close_drawer) {

@Override
public void onDrawerClosed(View drawerView) {
// Code here will be triggered once the drawer closes as we don't want anything to happen so we leave this blank.
super.onDrawerClosed(drawerView);
}

@Override
public void onDrawerOpened(View drawerView) {
// Code here will be triggered once the drawer open as we dont want anything to happen so we leave this blank.

super.onDrawerOpened(drawerView);
}
};

//Setting the actionbarToggle to drawer layout
drawerLayout.setDrawerListener(actionBarDrawerToggle);
drawerLayout.getChildAt(0).setSelected(true);

//calling sync state is necessary or else your hamburger icon wont show up
actionBarDrawerToggle.syncState();
}
}

最佳答案

onCreate()中,以下代码将在第一次启动时加载第一项的 fragment :

if (savedInstanceState == null) {
navigationView.getMenu().performIdentifierAction(R.id.posts, 0);
}

感谢 calvinfly for this comment .

关于android - 如何选择抽屉导航中的第一项并在应用程序启动时打开 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31043219/

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