如何在Tab更改时保存/恢复片段视图的状态

时间:2013-02-23 07:45:16

标签: android android-fragments android-tabhost android-lifecycle fragment-tab-host

我正在关注this教程。 我的应用程序中有3个选项卡。在tab3中我对一些视图进行了更改(比如按钮和EditText微调等),并且代表这些更改我必须在tab2中执行一些操作。简单地说,你可以说我在tab3中更改了一些值,效果在tab2中发生。我知道怎么做。我只是希望在tab2tab3

之间切换时,我的视图值每次都会重置为默认值
  • 我的问题是如何保存我的观点状态。所以在恢复标签时我必须得到我之前离开的视图的默认外观。

我还告诉你,我正在onCreateView()方法中完成所有工作。这是正确的方法。像这样。

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {

Toast.makeText(getActivity(), "onCreateView", Toast.LENGTH_SHORT)
        .show();

if (container == null) {

    return null;
}

View vi = inflater.inflate(R.layout.settings, container, false);

btnInsert = (Button) vi.findViewById(R.id.btnInsert);
btnInsert.setOnClickListener(this);
btnPosition = (Button) vi.findViewById(R.id.btnPosition);
btnPosition.setOnClickListener(this);
txtPosition = (TextView) vi.findViewById(R.id.txtPosition);
txtLogo = (TextView) vi.findViewById(R.id.txtLogo);
imgLogoPreview = (ImageView) vi.findViewById(R.id.imgLogoPreview);
imgLogoPreview.setOnClickListener(this);
edTxtUserText = (EditText) vi.findViewById(R.id.edTxtPreview);
relLogo = (RelativeLayout) vi.findViewById(R.id.RelLogo);
relText = (RelativeLayout) vi.findViewById(R.id.RelText);

logoWheel = (WheelView) vi.findViewById(R.id.wheelLogo);

logoWheel.setAdapter(new ArrayWheelAdapter<String>(logoWheelList));
logoWheel.setVisibleItems(4);
logoWheel.setCurrentItem(1);
positionWheel = (WheelView) vi.findViewById(R.id.wheelPosition);
positionWheel.setAdapter(new ArrayWheelAdapter<String>(
        positionWheelTextList));

// LogoWheel changed listener
changedListenerLogo = new OnWheelChangedListener() {
    public void onChanged(WheelView wheel, int oldValue, int newValue) {
        if (!wheelScrolled) {

        }
    }
};

logoWheel.addChangingListener(changedListenerLogo);

// Wheel scrolled listener
scrolledListenerLogo = new OnWheelScrollListener() {

    public void onScrollStarts(WheelView wheel) {
        wheelScrolled = true;

    }

    public void onScrollEnds(WheelView wheel) {
        wheelScrolled = false;
        btnInsert.setText(logoWheelList[wheel.getCurrentItem()] + "");
        wheel.setVisibility(View.INVISIBLE);
        if (wheel.getCurrentItem() == 2) {

            txtPosition.setVisibility(View.INVISIBLE);
            btnPosition.setVisibility(View.INVISIBLE);
            relText.setVisibility(View.INVISIBLE);
            relLogo.setVisibility(View.INVISIBLE);

        } else if (wheel.getCurrentItem() == 1) {

            relText.setVisibility(View.VISIBLE);
            relLogo.setVisibility(View.INVISIBLE);
            txtPosition.setVisibility(View.VISIBLE);
            btnPosition.setVisibility(View.VISIBLE);
            btnPosition.setText("Top");
            positionWheel.setAdapter(new ArrayWheelAdapter<String>(
                    positionWheelTextList));
            positionWheel.setVisibleItems(4);
            positionWheel.setCurrentItem(1);

        } else if (wheel.getCurrentItem() == 0) {

            relLogo.setVisibility(View.VISIBLE);
            relText.setVisibility(View.INVISIBLE);
            txtPosition.setVisibility(View.VISIBLE);
            btnPosition.setVisibility(View.VISIBLE);
            btnPosition.setText("Top Left");
            positionWheel.setAdapter(new ArrayWheelAdapter<String>(
                    positionWheelLogoList));
            positionWheel.setVisibleItems(4);
            positionWheel.setCurrentItem(1);

        }
    }
};

logoWheel.addScrollingListener(scrolledListenerLogo);

// /////////////////////Positon Wheel Listners///////////

// LogoWheel changed listener
changedListenerPosition = new OnWheelChangedListener() {
    public void onChanged(WheelView wheel, int oldValue, int newValue) {
        if (!wheelScrolled) {

        }
    }
};

positionWheel.addChangingListener(changedListenerPosition);

// Wheel scrolled listener
scrolledListenerPosition = new OnWheelScrollListener() {

    public void onScrollStarts(WheelView wheel) {
        wheelScrolled = true;

    }

    public void onScrollEnds(WheelView wheel) {
        wheelScrolled = false;

        String btnStatus = btnInsert.getText().toString();
        if (btnStatus.equals("Logo")) {
            btnPosition.setText(positionWheelLogoList[positionWheel
                    .getCurrentItem()] + "");

        } else if (btnStatus.equals("Text")) {
            btnPosition.setText(positionWheelTextList[positionWheel
                    .getCurrentItem()] + "");
        }

        wheel.setVisibility(View.INVISIBLE);

    }
};

positionWheel.addScrollingListener(scrolledListenerPosition);

return vi;

}

  • 在什么时候我必须保存状态,以及我应该在什么时候检索已保存的状态?

  • 请告诉我如何用简单的词语实现片段的生命周期。

我也试过片段的saveInstance()方法。但没有打电话。

由于

1 个答案:

答案 0 :(得分:1)

如果我理解正确,那么this可能会有用。每次你可以隐藏和显示碎片时,而不是重新创建碎片。

这当然会保留你的片段,所以可能只有你做了一些你有几个标签的东西。这样做的好处是

  1. 您无需担心保存数据并重新创建片段
  2. 选择相关标签后,会立即向用户提供更改。