方向更改后的布局并不令人耳目一新并不、耳目、布局、方向

2023-09-12 05:53:16 作者:我酷你随意

我有以下活动定义:

<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/inspectionMainLayout"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:singleLine="false"
        android:id="@+id/breadCrumb"
        android:visibility="gone">
    </LinearLayout>

    <ExpandableListView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/expandableListView" />

</LinearLayout>

现在在我的code我做动态的面包屑的LinearLayout添加按钮:

Now in my code I do add buttons dynamically in breadCrumb LinearLayout:

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

    setContentView(R.layout.activity_inspection);
    LinearLayout mainLayout = (LinearLayout) findViewById(R.id.inspectionMainLayout);

    if (mainLayout != null) {
        ExpandableListView list = (ExpandableListView) findViewById(R.id.expandableListView);

        list.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i2, long l) {
                LinearLayout breadCrumb = (LinearLayout) findViewById(R.id.breadCrumb);

                Button filterButton = new Button(InspectionActivity.this);
                filterButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        onFilterButtonClick((Button) view);
                    }
                });

                filterButton.setText(item.getFormattedFilter());

                breadCrumb.addView(filterButton);
            }
        }
    }       
    ...
}

这code效果很好,直到我不改变设备的方向,我的活动是重新创建。虽然所有的code被正确执行,屏幕似乎没有更新。一旦我恢复previous方向,所有的项目突然出现。任何想法,为什么,以及如何解决它?

This code works well, until I do not change the device orientation and my Activity is recreated. Although all the code is executing correctly, screen seems not being updated. Once I restore the previous orientation, all the items suddenly appear. Any idea why and how to fix it?

感谢

编辑:

我不认为我运行到同样的问题,描述了这个帖子: Android: findViewById给了我错了指针?

I do think that I'm running into the same problem as describe in this post: Android: findViewById gives me wrong pointer?

在如何解决这个任何想法?

Any idea on how to solve this?

根据要求我onRestoreInstanceState:

As requested my onRestoreInstanceState:

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

    baseCategories = savedInstanceState.getParcelable(BASE_CATEGORIES_STATE);
    currentFilter = savedInstanceState.getParcelable(FILTERS_STATE);
}

和上的onSaveInstanceState:

and on onSaveInstanceState:

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    outState.putParcelable(BASE_CATEGORIES_STATE, baseCategories);
    outState.putParcelable(FILTERS_STATE, currentFilter);
}

现在我的两个类都实现Parcelable接口。 他们坚持和正确还原。

now both of my classes do implement Parcelable interface. They are persisted and restored correctly.

不过对于一些resaon的调用findViewById获得是我指出了错误的对象(即不重新创建的)。

Still for some resaon the call to the findViewById get's me pointed to the wrong object (not the one that is recreated).

推荐答案

我发现,为什么会这样。

I found out why this is happening.

的OnSave / onRestoreInstanceState我坚持它有一些定制的听众就可以了currentFilter类。

onSave/onRestoreInstanceState I was persisting the currentFilter class which has some custom listeners on it.

随着onResume方法,我是做了以下内容:

As onResume method I was doing the following:

@Override
protected void onResume() {
    if (currentFilter == null) {
            currentFilter = new FilterItemList();

            currentFilter.addListener(new FilterItemListListener() {
                @Override
                public void filterChanged(FilterChangedEvent e) {
                    filterCategories(categoryRepository);
                }

                @Override
                public void filterAdded(FilterAddedEvent e) {
                    FilterItem addedItem = e.getAddedItem();
                    baseCategories.remove(new BaseCategory("", addedItem.getSectionName()));
                }

                @Override
                public void filterRemoved(FilterRemovedEvent e) {
                    FilterItem removedItem = e.getRemovedItem();
                    baseCategories.add(new BaseCategory("", removedItem.getSectionName()));
                }
            });
    }
}

本指针previous实例被持久化。这就是为什么我的界面行为不正确。

The pointer to the previous instance was persisted. That's why my interface was not behaving correctly.

现在我重新注册监听器,即使currentFilter不为空(这是恢复),使他们能够指向正确的实例。

Now I do re-register listeners even when currentFilter is not null (it is restored) so they can point to the right instance.

有没有在处理这一情况的任何模式?

Is there any pattern in handling this situations?

感谢