多个ListFragments的片段管理最佳实践

时间:2012-02-04 14:04:56

标签: android listview android-activity android-fragments

我计划在一个活动中包含3个片段列表。目标是从第一个列表中选择talk选项,然后根据您在talk list中单击的内容转换到运行列表,然后在运行列表中,根据您单击的内容将转换到最终的eat list。这应该发生在片段本身(就像我拥有它)或调用活动来处理来回传递数据到片段?

public class OptionsActivity extends Activity {

    protected TalkFragment talk;
    protected RunFragment run;
    protected EatFragment eat;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        talk = new TalkFragment();
        run = new RunFragment();
        eat = new EatFragment();
    } 
}


public class TalkFragment extends ListFragment {
    private Cursor mCursor;
    int mCurCheckPosition = 0;

    @Override
    public void onActivityCreated(Bundle savedState) {
        super.onActivityCreated(savedState);

    }
    @Override
    public void onListItemClick(ListView l, View v, int pos, long id) {
        mCurCheckPosition = pos;
        // We can display everything in-place with fragments.
        // Have the list highlight this item and show the data.
        getListView().setItemChecked(pos, true);

        // Check what fragment is shown, replace if needed.
        RunFragment run_frag = (RunFragment) getFragmentManager().findFragmentById(R.id.fragment_run);
        if (run_frag == null || run_frag.getShownIndex() != pos) {
            run_frag = RunFragment.newInstance(pos);
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            ft.replace(R.id.details, details);
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
            ft.commit();
        }

    } 
}

这显然只是嗤之以鼻,但你明白了。如果我这样做,我不确定如何正确地传递某些参数片段。理想情况下,RunFragment会根据在TalkFragment中单击的项目的ID知道要显示的内容。这些应该通过活动吗?

1 个答案:

答案 0 :(得分:2)

我通常这样做的方法是将Activity作为处理片段的交通警察。您的onListItemClick实现可以告诉Activity它想要做什么:

public class OptionsActivity extends Activity {

    protected TalkFragment talk;
    protected RunFragment run;
    protected EatFragment eat;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        talk = new TalkFragment();
        run = new RunFragment();
        eat = new EatFragment();
    }

    public void showRunFragment() {
        showFragment(R.id.fragment_run);
    }

    public void showEatFragment() {
        showFragment(R.id.fragment_eat);
    }

    public void showFragment(int fragmentId) {
        // Check what fragment is shown, replace if needed.

        ...
    }
}


public class TalkFragment extends ListFragment {
    private Cursor mCursor;
    int mCurCheckPosition = 0;

    @Override
    public void onActivityCreated(Bundle savedState) {
        super.onActivityCreated(savedState);

    }

    @Override
    public void onListItemClick(ListView l, View v, int pos, long id) {
        mCurCheckPosition = pos;
        // We can display everything in-place with fragments.
        // Have the list highlight this item and show the data.
        getListView().setItemChecked(pos, true);

        getActivity().showRunFragment()
    } 
}