屏幕方向更改后还原Android GridView(无需重新加载)
我是Android的新手(这是我的第一个应用程序),我在活动
中制作了 GridView
我希望它根据屏幕位置(人像/风景)做出响应。当然,我为纵向和横向的行宽度设置了 GridView
的自定义布局值,并且效果很好。
除了每次屏幕方向更改时, GridView
都会重新加载。这不利于用户体验。我只喜欢 GridView
(当然还有它的行)来调整到新的屏幕宽度,而无需执行所有 onCreate()
指令(从Internet检索 GridView
的数据,在 HashMap
对象中分配已检索的数据,设置 GridView
的 Adapter
,然后显示 GridView
的行)。
我浏览了许多论坛主题和一些Google文档,这些文档都涉及恢复简单值,但是在没有重新加载 GridView
的情况下,从未找到答案。
我读到有关 onSaveInstanceState()
和 onRestoreInstanceState()
的信息,但我不知道如何使用在我的情况下( GridView
情况)。
I'm new to Android (this is my first application) and I made a GridView
in an Activity
which I'd like it to be responsive according to screen position (portrait / landscape). Of course I made some custom layout values of GridView
rows width for portrait as well as for landscape, and it works perfect.
Except that, on every screen orientation change, the GridView
is reloading again. And that's not good for user experience. I only like the GridView
(and definitely its rows) to adjust to new screen width without doing all the onCreate()
instructions (retrieveing GridView
's data from internet, assigning retireved data in a HashMap
object, setting the GridView
's Adapter
, then display the GridView
's rows).
I went through many forum threads and some Google documentation that talk about restoring simple values, but never found an answer for restoring a GridView
without having it to reload again.
I read about onSaveInstanceState()
and onRestoreInstanceState()
, but I didn't know how to use them in my case (GridView
case).
有人可以为此提供一个简单的代码示例吗?还是至少要遵循一些头条新闻?
Could any one provide a simple code sample for this? Or at least some headlines to follow?
以下是我在代码中尝试的一些活动,在 Activity
类中:
Here's some of what I tried in my code, in the Activity
class :
private Parcelable mListInstanceState;
private Parcelable mAdapterInstanceState;
private GridView gridView = null;
private MyAdapter myAdapter = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (GridView) findViewById(R.id.gridview);
// Check whether we're recreating a previously destroyed instance
if(savedInstanceState != null) {
// Restore value of members from saved state
mListInstanceState = savedInstanceState.getParcelable("gridview");
mAdapterInstanceState = savedInstanceState.getParcelable("adapter");
}
else {
// Retrieve data from internet, fill HashMap object,
// set Adapter and display GridView only for the first time the activity is created
attemptToDisplayGirdViewItems();
}
}
@Override
protected void onSaveInstanceState(Bundle state) {
// Save activity state
super.onSaveInstanceState(state);
state.putParcelable("gridview", gridView.onSaveInstanceState());
// state.putParcelable("adapter", myAdapter);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
// Retrieve and restore activity state
// Restore state members from saved instance
mListInstanceState = savedInstanceState.getParcelable("gridview");
mAdapterInstanceState = savedInstanceState.getParcelable("adapter");
super.onRestoreInstanceState(savedInstanceState);
}
您不需要保存视图。如果它们具有ID,则会自动保存(例如GridView的滚动位置)。您需要保存的是适配器的数据集。
You don't need to save the views. If they have ID, they'll be saved automatically (such as scroll position of a GridView). What you need to save is the adapter's data set.
您的适配器可能包含一个项目列表。除非这些项目是基元或 String
或实现 Serializable
,否则它们需要实现 Parcelable
才能起作用。
Your adapter presumably holds a list of items. Unless these items are primitives or String
or implement Serializable
, they need to implement Parcelable
for this to work.
在此处查看操作方法:如何使自定义对象可拆分?
See here how to do that: How can I make my custom objects Parcelable?
您的适配器需要一个 getItems
方法可返回数据集-可包裹物品的 ArrayList
。它还需要一个 setItems(...)
方法或一个将项目列表作为参数的构造函数。
Your adapter needs a getItems
method which returns the data set - an ArrayList
of your parcelable items. It also needs to have a setItems(...)
method or a constructor taking a list of items as parameter.
然后您的活动将如下所示:
Then your activity will look like this:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (GridView) findViewById(R.id.gridview);
myAdapter = new MyAdapter(); // Create empty adapter.
if(savedInstanceState != null) {
ArrayList<MyItem> items = savedInstanceState.getParcelableArrayList("myAdapter");
myAdapter.setItems(items); // Load saved data if any.
}
gridView.setAdapter(myAdapter);
}
@Override
protected void onSaveInstanceState(Bundle state) {
super.onSaveInstanceState(state);
state.putParcelableArrayList("myAdapter", myAdapter.getItems());
}