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



我只做了几个月的android编程,所以我可能在这里做一些愚蠢的事情。。。

在我的活动中,我使用了一个带有LinearLayout的布局文件,我通过编程将子视图添加到活动的OnCreate()中。有一个子视图的布局文件,它包含3个EditText,我正在用保存在某个地方的数据填充这些EditText。

当我导航到活动并首次调用onCreate()时,每个子视图都会按照我的预期进行填充。但是,如果我将显示从纵向->横向旋转,反之亦然,当页面刷新时,所有子视图都具有上一个子视图的内容。当我使用断点遍历代码时,我觉得一切都很好:父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看了这篇文章,我有了了解onRestoreInstanceState(Bundle savedInstanceState)中发生了什么的想法。令我惊讶的是,覆盖它并使其变空,解决了我的问题。我不确定这是否是最正统的解决方案。

此行之后:

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"/>

最新更新