我想调用简单的Android Activity点击Slider Menu Item,在片段的位置。 我试图使用片段调用活动,但它没有显示任何内容
请帮帮我。
有人可以建议替代方式而不是这个吗?
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
Log.e("", "In Fragment "+position);
fragment = new About_us_Activity();
//startActivity(new Intent(this,Home_Activity.class));
break;
case 1:
//fragment = new About_us_frag();
Log.e("", "In Fragment "+position);
break;
case 2:
Log.e("", "In Fragment "+position);
fragment = new About_us_frag1();
break;
default:
break;
}
if (fragment != null) {
Log.e("", "In Fragment");
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
// update selected item and title, then close the drawer
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
答案 0 :(得分:1)
首先将 onItemClickListener 应用于 抽屉列表 。
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mDrawerLayout.closeDrawers();
displayView(position);
}
});
在您的DisplayView方法中:
private void displayView(int position) {
switch (position) {
case 1:
Intent intent = new Intent(getApplicationContext(), MyActivity1.class);
startActivity(intent);
break;
case 2:
Intent intent1 = new Intent(getApplicationContext(),MyActivity2.class);
startActivity(intent1);
break;
default:break;
}
}
MyActivity1和MyActivity2是活动。你可以尝试这种方法,它对我有用。