【问题标题】:Fragment Management Best Practices for Multiple ListFragments多个 ListFragment 的片段管理最佳实践
【发布时间】:2012-02-04 14:04:56
【问题描述】:

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

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 知道要显示什么。这些应该通过 Activity 来代替吗?

【问题讨论】:

    标签: android listview android-activity android-fragments


    【解决方案1】:

    我通常这样做的方式是让 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()
        } 
    } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-02
      • 1970-01-01
      相关资源
      最近更新 更多