0

我正在关注教程。我的应用程序中有 3 个选项卡。在 tab3 中,我正在对一些视图(如按钮和 EditText 微调器等)进行更改,并且代表这些更改,我必须在 tab2 中执行一些操作。简单地说,我在 tab3 中更改了一些值,效果发生在 tab2 中。我知道该怎么做。我只是希望我的视图值每次在tab2和之间切换时都会重置为默认值tab3

  • 我的问题是我怎样才能保存我的观点状态。因此,在恢复选项卡时,我必须获得我之前离开的视图的默认外观。

还有一件事我告诉你,我在 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()片段的方法。但没有调用。

谢谢

4

1 回答 1

1

如果我理解正确,那么可能会有用。而不是每次都重新创建片段,您可以隐藏和显示它们。

这当然会保留您的 Fragments,因此可能只是您需要做的事情,因为您有几个选项卡。这样做的好处是

  1. 您无需担心保存数据和重新创建片段
  2. 选择相关选项卡后,用户可以立即使用更改。
于 2013-02-23T08:39:38.623 回答