我正在尝试从SherlockNavigationDrawer修改示例。它只有一个片段,但我不知道要添加更多片段,每个片段都具有自己的布局,但并非所有片段都相同...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FrameLayout frame = new FrameLayout(this);
frame.setId(CONTENT_VIEW_ID);
setContentView(frame, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
if (savedInstanceState == null) {
setInitialFragment();
}
}
private void setInitialFragment() {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(CONTENT_VIEW_ID, MainFragment.newInstance()).commit();
}
但是我的想法是在MainFragment内部的列表视图中单击一个项目时调用一个新的片段
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mContent.setText(Shakespeare.DIALOGUE[position]);
mActionBar.setTitle(Shakespeare.TITLES[position]);
mDrawerLayout.closeDrawer(listView);
//START A NEW FRAGMENT HERE DEPENDING OF THE POSITION CLICKED
}
}
有什么想法吗?
提前致谢
最佳答案
在sherlockFragment内部,即示例随附的唯一片段。
public class MainFragment extends SherlockFragment {
Customfragment1 fragment1;
Customfragment2fragment2;
@Override
public void onCreate(Bundle savedInstanceState) {
fragment1 = new ContactsFragment();
fragment2 = new GroupsFragment();
}
private void selectItem(int position) {
FragmentTransaction ft = getSherlockActivity().getSupportFragmentManager().beginTransaction();
// Locate Position
switch (position) {
case 0:
ft.replace(R.id.content_frame, fragment1, "Customfragment1");
ft.addToBackStack("Customfragment1");
break;
case 1:
ft.replace(R.id.content_frame, fragment2, "Customfragment2");
ft.addToBackStack("Customfragment2");
break;
}
ft.commit();
mDrawerLayout.closeDrawer(listView);
}
}
关于android - SherlockNavigationDrawer(实现actionbarsherlock),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19998348/