2

我正在尝试修改 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
    }
}

有什么想法吗?提前致谢

4

2 回答 2

1

谢谢 amalBit,但你的方法 selectItem 我已经放在我的第二个和第三个片段中......它有效,但这个解决方案迫使我将此代码放入我创建的每个附加片段中,我的意思是,第二个,第三个。 ……

//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);

        selectItem(position);
    }
}

private void selectItem(int position) {

    FragmentTransaction ft = getSherlockActivity().getSupportFragmentManager().beginTransaction();
    // Locate Position
    switch (position) {
    case 0:
        ft.replace(666, SecondFragment.newInstance(), "SecondFragment");
        ft.addToBackStack("SecondFragment");
        break;
    case 1:
        ft.replace(666, ThirdFragment.newInstance(), "ThirdFragment");
        ft.addToBackStack("ThirdFragment");
        break;
    }
    ft.commit();
    mDrawerLayout.closeDrawer(listView);
}   

有没有机会将此代码移动到控制器 MainActivity?

于 2013-11-16T09:31:11.430 回答
1

在 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);
        }
}
于 2013-11-15T10:09:00.387 回答