gpt4 book ai didi

java - 如何通过单击上一个抽屉项目的特定 fragment 来打开下一个抽屉

转载 作者:行者123 更新时间:2023-12-01 09:53:39 37 4
gpt4 key购买 nike

在我的应用程序中,我想通过单击 android 中抽屉导航的前一个抽屉中的导航栏项目来打开新抽屉。就像现在我有 mainactivity.java 和 fragment 涉及该 Activity 。现在我想要的是,当我单击 mainactivity.java 抽屉项目的任何项目时,它必须打开另一个项目中具有不同值的抽屉,我正在附加 mainActivity.java 的代码

public class MainActivity extends AppCompatActivity{
DrawerLayout mDrawerLayout;
NavigationView mNavigationView;
FragmentManager mFragmentManager;
FragmentTransaction mFragmentTransaction;

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

/**
*Setup the DrawerLayout and NavigationView
*/

mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
mNavigationView = (NavigationView) findViewById(R.id.shitstuff) ;

/**
* Lets inflate the very first fragment
* Here , we are inflating the TabFragment as the first Fragment
*/

mFragmentManager = getSupportFragmentManager();
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.containerView,new TabFragment()).commit();
/**
* Setup click events on the Navigation View Items.
*/

mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
mDrawerLayout.closeDrawers();



if (menuItem.getItemId() == R.id.nav_item_home) {
FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.containerView,new coordinatorPanelFragment()).commit();

}


if (menuItem.getItemId() == R.id.nav_item_fllclass) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView,new fillClassFragment()).commit();
}
///

if (menuItem.getItemId() == R.id.nav_item_fillstudent) {
FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.containerView,new fillStudentFragment()).commit();

}


if (menuItem.getItemId() == R.id.nav_item_fillschedule) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView,new fillscheduleFragment()).commit();
}


if (menuItem.getItemId() == R.id.nav_item_seebunk) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView,new seeBunkedStudentFragment()).commit();
}

if (menuItem.getItemId() == R.id.nav_item_cus) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView,new ContactusFragment()).commit();
}

if (menuItem.getItemId() == R.id.nav_item_help) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView,new helpCoorFragment()).commit();
}





return false;
}

});

/**
* Setup Drawer Toggle of the Toolbar
*/

android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this,mDrawerLayout, toolbar,R.string.app_name,
R.string.app_name);

mDrawerLayout.setDrawerListener(mDrawerToggle);

mDrawerToggle.syncState();

}

这些是我制作一个包含 6 个项目的抽屉的代码,现在我希望当我单击任何项​​目时,它必须打开该特定 fragment 的特定抽屉,我已经搜索过,但在谷歌上没有找到答案。它与谷歌驱动器的应用程序。

最佳答案

看看这些例子,我希望它们能帮助您解决问题。 http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/

这是Android滑动菜单使用抽屉导航的教程。所需的代码是我粘贴在这里:-

private class SlideMenuClickListener implements
ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// display view for selected nav drawer item
displayView(position);
}
}

/**
* Diplaying fragment view for selected nav drawer list item
* */
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new HomeFragment();
break;
case 1:
fragment = new FindPeopleFragment();
break;
case 2:
fragment = new PhotosFragment();
break;
case 3:
fragment = new CommunityFragment();
break;
case 4:
fragment = new PagesFragment();
break;
case 5:
fragment = new WhatsHotFragment();
break;

default:
break;
}

if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();

// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}

关于java - 如何通过单击上一个抽屉项目的特定 fragment 来打开下一个抽屉,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37409757/

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