我正在尝试使用 2 个选项卡实现一个基本活动,每个选项卡都包含一个列表。按照指南,我编写了以下代码:
MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Notice that setContentView() is not used, because we use the root
// android.R.id.content as the container for each fragment
// setup action bar for tabs
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.setDisplayShowTitleEnabled(true);
ActionBar.Tab tab = actionBar.newTab()
.setText(R.string.tab_last_transactions)
.setTabListener(new TabListener<List1Fragment>(
this, "list1_fragment", List1Fragment.class));
actionBar.addTab(tab);
tab = actionBar.newTab()
.setText(R.string.tab_accounts_balance)
.setTabListener(new TabListener<List2Fragment>(
this, "list2_fragment", List2Fragment.class));
actionBar.addTab(tab);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
new MenuInflater(this).inflate(R.menu.home_menu, menu);
return (super.onCreateOptionsMenu(menu));
}
}
List1Fragment.java
public class List1Fragment extends ListFragment {
private static final String[] items = { "Android", "Bluetooth", "Chrome",
"Docs", "Email", "Facebook", "Google", "Hungary", "Iphone",
"Korea", "Machintosh", "Nokia", "Orkut", "Picasa", "Singapore",
"Talk", "Windows", "Youtube" };
private ArrayList<String> words = null;
ArrayAdapter<String> adapter = null;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Populate list with our static array of titles.
words = new ArrayList<String>();
for (String s : items) {
words.add(s);
}
adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, words);
setListAdapter(adapter);
}
}
list1_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#00FF00"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
</LinearLayout>
MainActivity 从 LoginActivity 实例化,如下所示:
LoginActivity.java 的一部分
Intent mainIntent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(mainIntent);
finish();
LoginActivity 看起来不错。第一次启动MainActivity,没问题,按预期得到列表:
现在我想查看我的应用程序生命周期,所以我将其更改为横向以杀死它并重新创建它,它会在顶部创建一个额外的列表(如果我切换回纵向,还会再创建一个额外的列表)。活动的(滚动的)似乎是第一个创建的。其他的看起来修复了。
类似的结果发布在另一个主题:
如果我检查 (savedInstanceState == null) 并仅在 null 时设置 ListAdapter,它似乎具有所需的行为(只有一个列表),但列表顶部有一个“正在加载...”。添加 setListShown(true) 会抛出异常并退出程序:
修改 List1_Fragment:
if (savedInstanceState==null)
{
// Populate list with our static array of titles.
words = new ArrayList<String>();
for (String s : items) {
words.add(s);
}
adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, words);
setListAdapter(adapter);
}
else {
setListShown(true);
}
我究竟做错了什么?如何避免重新创建列表?
(防止它在改变方向时重新生成不是一个选项,我会在应用程序完成后执行此操作,但想在开发过程中使用此功能测试生命周期,以模拟终止/重新启动)
编辑: 使用 setListShownNoAnimation 而不是 setListShown 工作...
编辑2:
TabListener.java
public class TabListener<T extends Fragment> implements ActionBar.TabListener {
private Fragment mFragment;
private final Activity mActivity;
private final String mTag;
private final Class<T> mClass;
/** Constructor used each time a new tab is created.
* @param activity The host Activity, used to instantiate the fragment
* @param tag The identifier tag for the fragment
* @param clz The fragment's Class, used to instantiate the fragment
*/
public TabListener(Activity activity, String tag, Class<T> clz) {
mActivity = activity;
mTag = tag;
mClass = clz;
}
/* The following are each of the ActionBar.TabListener callbacks */
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
// Check if the fragment is already initialized
if (mFragment == null) {
// If not, instantiate and add it to the activity
mFragment = Fragment.instantiate(mActivity, mClass.getName());
ft.add(android.R.id.content, mFragment, mTag);
} else {
// If it exists, simply attach it in order to show it
ft.attach(mFragment);
}
}
public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction ft) {
if (mFragment != null) {
// Detach the fragment, because another one is being attached
ft.detach(mFragment);
}
}
public void onTabReselected(ActionBar.Tab tab, FragmentTransaction ft) {
// User selected the already selected tab. Usually do nothing.
}
}