在我的导航抽屉中,我使用这种方法打开一个活动: 编辑
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
Fragment fragment = null;
@SuppressLint("ResourceAsColor") @Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Fragment fragment = null;
FragmentManager fragmentManager = getFragmentManager();
Bundle bundle = new Bundle();
switch (position) {
case 0:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(str)));
break;
case 1:
fragment = new TrimFragment();
fragment.setArguments(bundle);
fragmentManager.beginTransaction()
.replace(R.id.content_frame, fragment).commit();
break;
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(strg)));
break;
case 3:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(strtwit)));
break;
}
}
});
}
但我想要的是现在打开一个片段。是否有可能从这个结构中做到这一点?谢谢
答案 0 :(得分:0)
在适当的情况下添加/替换要在容器中显示的片段
答案 1 :(得分:0)
在切换案例中,请使用此代码。如果要将现有片段替换为导航抽屉列表中单击的片段,则可以使用此方法。
//通过替换片段来更新主要内容
Fragment fragment = null;
FragmentManager fragmentManager = getFragmentManager();
Bundle bundle = new Bundle();
switch (position) {
case 0:
fragment = new Dashboard_Fragment();
break;
case 1:
fragment = new SearchCustomer_Fragment();
//Request Origin
bundle.putString("origin", "searchCustomer");
fragment.setArguments(bundle);
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
break;
case 2:
fragment = new STB_Check_Fragment();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
break;
}
答案 2 :(得分:0)
您可以显示片段而不是活动:
请参阅此片段。
mDrawerList.setOnItemClickListener(new DrawerItemClickListener()); //onclick listener
/* The click listner for ListView in the navigation drawer */
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
setCharacter(position);
}
}
//here a new fragment is created and replaces the old one.
public void setCharacter(int position){
Fragment fragment = new CharacterFragment();
Bundle args = new Bundle();
args.putInt(CharacterFragment.ARG_CHAR_NUMBER, position);
fragment.setArguments(args);
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
setTitle(mCharacterTitles[position]);
mDrawerLayout.closeDrawer(mLinearLayout);
}
@Override
public void setTitle(CharSequence title) {
// TODO Auto-generated method stub
mTitle = title;
getActionBar().setTitle(mTitle);
}
/**
* Fragment that appears in the "content_frame", shows a planet
*/
public static class CharacterFragment extends Fragment {
public static final String ARG_CHAR_NUMBER = "character_number";
public CharacterFragment() {
// Empty constructor required for fragment subclasses
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_character, container, false);
int i = getArguments().getInt(ARG_CHAR_NUMBER);
String charName = getResources().getStringArray(R.array.characters_array)[i];
TextView charTextView = (TextView) rootView.findViewById(R.id.text);
charTextView.setText(charName);
getActivity().setTitle(charName);
return rootView;
}
}
希望这有帮助。