以编程方式向父LinearLayout添加视图在旋转设备时复制最后一个条目

时间:2017-11-15 17:23:27

标签: android android-linearlayout device-orientation

我只做了几个月的android编程,所以我可以在这里做些傻事......

在我的活动中,我正在使用一个带有LinearLayout的布局文件,我正在以编程方式在活动的OnCreate()中添加子视图。子视图有一个布局文件,它包含3个EditTexts,我用我保存的数据填充。

当我导航到活动并首次调用onCreate()时,每个子视图都会填充how I would expect。但是,如果我从纵向旋转显示 - >当页面刷新时,all child views have the content of the last child view,或者反之亦然。当我使用断点逐步执行代码时,一切看起来都很好:父LinearLayout具有预期的子视图,子视图使用预期数据填充。所以,我不知道为什么所有的儿童观点都有来自最后一个孩子的数据,以及为什么这只会在旋转时发生。无论设备大小或方向如何,我都在使用相同的布局文件。

有什么想法吗?

edit_profile_activity.xml:

<android.support.design.widget.CoordinatorLayout
    ...>

    <android.support.design.widget.AppBarLayout
        ...>

        <android.support.v7.widget.Toolbar
            .../>

    </android.support.design.widget.AppBarLayout>

    <!-- The main content view -->
    <android.support.v4.widget.NestedScrollView
        ...>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:scrollbarAlwaysDrawVerticalTrack="true"
            android:focusable="true"
            android:focusableInTouchMode="true">

            <!-- Edit Allies -->
            <LinearLayout
                android:id="@+id/profile_edit_ally_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingLeft="@dimen/left_margin"
                android:paddingRight="@dimen/right_margin"
                android:orientation="vertical">

                <!-- This content is added programmatically. -->

            </LinearLayout>

        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>

edit_profile_activity_ally_list_item.xml:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <EditText
        android:id="@+id/profile_edit_ally_name_edit_text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:hint="Name"
        android:imeOptions="flagNoExtractUi"
        android:inputType="textNoSuggestions"
        android:maxLines="1"
        android:privateImeOptions="nm"
        android:selectAllOnFocus="true"
        android:textAppearance="@style/FontEditText"/>
    <EditText
        android:id="@+id/profile_edit_ally_init_mod_edit_text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_marginLeft="@dimen/left_margin"
        android:hint="Init Mod"
        android:imeOptions="flagNoExtractUi"
        android:inputType="numberSigned"
        android:maxLines="1"
        android:privateImeOptions="nm"
        android:selectAllOnFocus="true"
        android:textAppearance="@style/FontEditText"/>
    <EditText
        android:id="@+id/profile_edit_ally_caster_level_edit_text"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_marginLeft="@dimen/left_margin"
        android:hint="Caster Level"
        android:imeOptions="flagNoExtractUi"
        android:inputType="numberSigned"
        android:maxLines="1"
        android:privateImeOptions="nm"
        android:selectAllOnFocus="true"
        android:textAppearance="@style/FontEditText"/>
    <ImageButton
        android:id="@+id/profile_edit_delete_ally_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:background="@drawable/ic_delete"/>
</LinearLayout>

在我的活动中:

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    // Set the content of the activity to use the activity_main.xml layout file
    setContentView(R.layout.edit_profile_activity);

    // ...

    mAllyLayout = (LinearLayout) findViewById(R.id.profile_edit_ally_layout);
    for (TreeMap.Entry<String, AllyDB_Data> entry : mProfileData.mAllies.entrySet())
    {
        addAllyRowToLayout(entry.getKey(),
            Integer.toString(entry.getValue().mCasterLevel),
            Integer.toString(entry.getValue().mInitMod));
    }

    // Make sure there's at least one "empty" ally set.
    if (mAllyLayout.getChildCount() <= 0)
    {
        addAllyRowToLayout(null, null, null);
    }

    // ...
}

// ...

private void addAllyRowToLayout(String name, String casterLevel, String initMod)
{
    // Inflate the Ally Row
    View ally_layout_row = getLayoutInflater().inflate(R.layout.edit_profile_activity_ally_list_item, null, false);

    // EditText Name
    EditText name_edit_text = (EditText) ally_layout_row.findViewById(R.id.profile_edit_ally_name_edit_text);
    name_edit_text.setTextSize(14.0f);
    name_edit_text.setTextColor(Color.DKGRAY);
    if (name != null)
    {
        if (name.isEmpty() == false)
        {
            name_edit_text.setText(name);
        }
    }
    name_edit_text.addTextChangedListener(new TextWatcher()
    {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

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

        @Override
        public void afterTextChanged(Editable s) {}
    });

    // EditText Init Mod
    EditText init_mod_edit_text = (EditText) ally_layout_row.findViewById(R.id.profile_edit_ally_init_mod_edit_text);
    init_mod_edit_text.setTextSize(14.0f);
    init_mod_edit_text.setTextColor(Color.DKGRAY);
    if (initMod != null)
    {
        if (initMod.isEmpty() == false)
        {
            init_mod_edit_text.setText(initMod);
        }
    }

    // EditText Caster Level
    EditText caster_level_edit_text = (EditText) ally_layout_row.findViewById(R.id.profile_edit_ally_caster_level_edit_text);
    caster_level_edit_text.setTextSize(14.0f);
    caster_level_edit_text.setTextColor(Color.DKGRAY);
    if (casterLevel != null)
    {
        if (casterLevel.isEmpty() == false)
        {
            caster_level_edit_text.setText(casterLevel);
        }
    }

    // ImageButton Delete Ally Button
    ImageButton delete_button = (ImageButton) ally_layout_row.findViewById(R.id.profile_edit_delete_ally_button);
    delete_button.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            View parent_view = (View) v.getParent();
            if (parent_view != null)
            {
                mAllyLayout.removeView(parent_view);
            }
            if (mAllyLayout.getChildCount() == 0)
            {
                addAllyRowToLayout("", "", "");
            }
        }
    });

    // Add the Ally Row to the Ally Layout.
    mAllyLayout.addView(ally_layout_row);
}

修改1 活动的onCreate()中有一个if语句,我在第一次发布时没有显示。 if (allyLayout.getChildCount() <= 0) ...

编辑2 查看this帖子让我想到了onRestoreInstanceState(Bundle savedInstanceState)中发生了什么。对我来说,令人惊讶的是,压倒它并使它变空,照顾我的问题。我不确定这是否是最正统的解决方案。

1 个答案:

答案 0 :(得分:0)

这一行之后:

mAllyLayout = (LinearLayout) findViewById(R.id.profile_edit_ally_layout);

检查线性布局是否有一些视图。

   if (mAllyLayout.getChildCount()>0){
    //add your views

    }

<强>更新 如果您不想在轮换后处理数据,请在清单上添加:

<activity
            android:name="Your activity"
            android:configChanges="orientation|keyboardHidden|screenSize"/>