通过片段方向更改保留布局

时间:2016-06-20 15:48:01

标签: java android xml android-layout android-fragments

我有一个名为InfoFragment的片段的两个布局:info_layout.xml(land)和info_layout.xml(port)。当我开始更改方向时,Android会根据需要更改布局。但是,它会创建一个新视图(我认为应该如此),但保留了旧视图。结果是,每次切换方向时,另一个视图都是添加的(在一个方向更改后有2个视图,在两个视图之后,...)

我不确定这个问题来自何处,因为只要在(端口)和(陆地)中存在某些内容,我就可以看到Android应该处理它。

这是我的清单:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.anothergamedesigner.listviewtest">
<supports-screens
    android:smallScreens="true"
    android:normalScreens="true"
    android:largeScreens="true"
    android:xlargeScreens="true" />

<application
    android:allowBackup="true"
    android:icon="@drawable/icon"
    android:logo="@drawable/logo"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".ToolReaderActivity"
        android:label="@string/app_name"
        android:theme="@style/Theme.AppCompat">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name=".InfoActivity"
        android:theme="@style/Theme.AppCompat"
        android:configChanges="keyboardHidden|orientation|screenSize"
        />
</application>

这是我的片段:

public class InfoFragment extends Fragment {

    private TextView mTitle;
    private TextView mSubtitle;
    private TextView mDescription;

    //Tool to be displayed
    Tool mTool = null;

    //Parameterless constructor needed for framework
    public InfoFragment(){
        super();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.info_layout, container, false);
        loadView();
        return view;
    }

    @Override
    public void onViewCreated(View view,Bundle savedInstanceState) {

        mTitle = (TextView) view.findViewById(R.id.title);
        mSubtitle = (TextView) view.findViewById(R.id.subtitle);
        mDescription = (TextView) view.findViewById(R.id.description);

        if (mTool != null) {
            updateDisplay();
        }
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        String title, subtitle, description;

        if (savedInstanceState != null) {
            //Restore the fragment's state here
            title = savedInstanceState.getString("title");
            subtitle = savedInstanceState.getString("subtitle");
            description = savedInstanceState.getString("description");

            if(title != null)
                mTitle.setText(title);
            if(subtitle != null)
                mSubtitle.setText(subtitle);
            if(description != null)
                mDescription.setText(description);
        }
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        //Save the fragment's state here
        outState.putString("title", mTitle.getText().toString());
        outState.putString("subtitle",  mSubtitle.getText().toString());
        outState.putString("description",  mDescription.getText().toString());
    }

    public void displayTool(Tool t){
        mTool = t;
        loadView();
    }

    private void loadView(){
        if (getView() != null) {
           updateDisplay();
        }
    }

    private void updateDisplay() {
        mTitle.setText(mTool.getTitleTxt());
        mSubtitle.setText(mTool.getSubtitleTxt());
        mDescription.setText(mTool.getDescriptionTxt());
    }
}

0 个答案:

没有答案