片段按钮在方向更改后不起作用

时间:2015-09-19 05:44:02

标签: android android-fragments

当我更改为横向模式并返回纵向时,按钮不起作用。无论如何,这是我的代码:

public class fragmentone extends Fragment {
    Button biological;
    Button natural;
    View myView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        myView = inflater.inflate(R.layout.firstlay, container, false);
        biological = (Button) myView.findViewById(R.id.biological);
        biological.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BiologicalHazards fragment = new BiologicalHazards();
                FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
                fragmentManager.beginTransaction().addToBackStack("hi")
                        .setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out)
                        .replace(R.id.container1, fragment)
                        .commit();
            }
        });
        natural = (Button) myView.findViewById(R.id.natural);
        natural.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NaturalHazards fragment = new NaturalHazards();
                FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
                fragmentManager.beginTransaction().addToBackStack("hi")
                        .setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out)
                        .replace(R.id.container1, fragment)
                        .commit();
            }
        });
        return myView;
    }
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){
            getActivity().setContentView(R.layout.firstlay);
        }else{
            getActivity().setContentView(R.layout.firstlay);
        }
    }
}

有谁知道如何解决这个问题?任何帮助都表示赞赏,并提前感谢。

2 个答案:

答案 0 :(得分:0)

你需要添加setRetainInstance(true); //你的片段onCreateView()

也不必这样做getActivity()。setContentView(R.layout.firstlay);在你的onConfigurationChanged()方法中。

希望这有帮助:)

答案 1 :(得分:0)

我认为你在改变方向时需要重新启动

尝试修改后的代码

private LayoutInflater inflater;
@Override
public View onCreateView(LayoutInflater inflater,
        @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    myView = inflater.inflate(R.layout.firstlay, container, false);
    reInitilize(myView);
    return myView;
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
    // TODO Auto-generated method stub
    super.onConfigurationChanged(newConfig);
    if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){
        myView = inflater.inflate(R.layout.firstlay, null);
        reInitilize(myView);
    }else{
        myView = inflater.inflate(R.layout.firstlay, null);
        reInitilize(myView);
    }
}
private void reInitilize(View myView) {
    // TODO Auto-generated method stub
    biological = (Button) myView.findViewById(R.id.biological);
    biological.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            BiologicalHazards fragment = new BiologicalHazards();
            FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
            fragmentManager.beginTransaction().addToBackStack("hi")
                    .setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out)
                    .replace(R.id.container1, fragment)
                    .commit();
        }
    });
    natural = (Button) myView.findViewById(R.id.natural);
    natural.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            NaturalHazards fragment = new NaturalHazards();
            FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
            fragmentManager.beginTransaction().addToBackStack("hi")
                    .setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out)
                    .replace(R.id.container1, fragment)
                    .commit();
        }
    });

}