1

I am having a ViewPager to allow the user to slide between 5 different views where each "view" extends Fragment.

I have my own adapter which extend FragmentPagerAdapter and which implement getItem() as

@Override public Fragment getItem(int position) {
   switch(position) {
      case 0: 
          return new TextDescriptionFragment();
      // Handle the 4 other cases the same way
   }
}

This works fine and the user can swipe between the 5 different views. But here comes the problem: Each of the first 4 views contains Views such as Button and EditText which the user can interact with.

And I then want the last page(Page number 5) to show all the user input values from all the views from the 4 previous pages(fragments). How do I do that?

I can't find any way to read the user input values from the previous fragments. The views may not even exist anymore(But will be recreated if the user goes back).

And I can't seem to get the existing fragments.

4

2 回答 2

1

我会考虑有一个自定义对象来保存每个片段填充的数据。就像是:

public class FillerData implements Parcelable {
    private String page0$data0;
    private String page0$data1;
    private String page0$data2;

    // getters and setters if you wish

    // implement Parcelable interface as this object will be managed by host activity
}

您将只有一个由父活动管理的此类对象,并且父活动将实现一个接口来公开此对象:

public static interface FillerDataExposer {
    public FillerData exposeFiller();
}

public class MyFragmentHostActivity extends FragmentActivity implements FillerDataExposer {
    private static final String FILLER_KEY = "FILLER_KEY";
    private FillerData myFillerData;

    protected void onCreate(Bundle savedInstance) {
        .......
        if(savedInstance != null) {
            myFillerData = (FillerData) savedInstance.getParcelable(FILLER_KEY);
        } else {
            myFillerData = new FillerData();
        }
    }

    protected void onSaveInstanceState(Bundle savedInstance) {
        super.onSaveInstanceState();
        savedInstance.putExtra(FILLER_KEY, myFillerData);
    }

    public FillerData exposeFiller() {
        return this.myFillerData;
    }
}

现在,您的每个片段都可以通过父活动访问该集中式数据填充对象。为了减轻代码的重量,您的所有片段都可以从提供对FillerDataExposer实现的访问(实际上是父活动)的基本片段类扩展:

public abstract class AbstractFillerFragment extends Fragment {
    protected FillerDataExposer dataExposer;

    public void onAttach(Activity act) {
        super.onAttach(act);
        // make sure no ClassCastExceptions
        this.dataExposer = (FillerDataExposer) act;
    }
}

应该只记录填充数据的片段可能如下所示:

public class Page1Fragment extends AbstractFillerFragment {

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = // inflate XML resource ...
        yourEditText = (EditText) view.findViewById(...);
        // other relevant code ....
    }

    public void onViewCreated(View view, Bundle savedInstanceState) {
       yourEditText.setText(dataExposer.exposeFiller.setPageX$DataY());
       // some code for EditText.addTextChangedListener(new TextWatcher() could look like:
        yourEditText.addTextChangedListener(new TextWatcher() {

          public void afterTextChanged(Editable s) {

            dataExposer.exposeFiller().setPage1$Data0(s.toString());

          }

          public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

          public void onTextChanged(CharSequence s, int start, int before, int count) {}
       });
    }
}

虽然需要访问所有存储数据的片段可能如下所示:

public class FinalFragment extends AbstractFillerFragment {
    public void collectAllData() {
        DataFiller allDataCollectedObject = dataExposer.exposeFiller();
        // by calling get...() you should have access to collected data.
    }
}

这只是一个草图,但你会得到图片。这个想法是在活动重新启动时管理活动中的单个对象,并使其通过接口可访问,因此您将尊重活动模式的片段。

希望这是有道理的...

于 2013-07-30T14:00:22.870 回答
0

我想到了2个解决方案。

第一个是在调用前 4 个片段的 onpause() 方法时保存用户输入数据。您可以将数据保存到首选项,然后从您的第 5 个片段中检索它。

第二种方法是在刷卡时保留您的片段。这样刷卡将更快更干净,并且每次刷卡时都希望重新创建它们:

yourcustomviewpager.setOffscreenPageLimit(5);

关于android 文档中的setOffscreenPageLimit :

设置应保留到处于空闲状态的视图层次结构中当前页面的任一侧的页面数。超出此限制的页面将在需要时从适配器重新创建。这是作为优化提供的。如果您事先知道需要支持的页面数量或在页面上设置了延迟加载机制,则调整此设置可以提高分页动画和交互的感知流畅度。如果您有少量页面 (3-4) 可以同时保持活动状态,那么在用户页面来回切换时,新创建的视图子树的布局将花费更少的时间。您应该将此限制保持在较低水平,尤其是在您的页面具有复杂布局的情况下。此设置默认为 1。

于 2013-07-30T13:36:31.387 回答