在一个同时显示标题列表和 详情的页面中,TitleFragment 类用于显示标题列表
1. TitleFragment 继承ListFragment, 与前面介绍DetailsFragment 不同,此碎片没有在onCreateView() 回调中执行任何操作. 这是因为扩展了ListFragment 类, 它已经包含了一个ListView 组件
2. ListFragment 的默认onCreateView() 会创建此ListView 并返回它, ListView 的资源Id 为 android.R.id.list1, 但是, 如果需要获取它的引用, 可以调用getListView() , 在onActivityCreated() 中完成, 注意getListView() 是ListFragment 提供的.
3. 由于ListFragment不同于ListView, 所以没有直接将适配器设置到ListView, 可以使用ListFragment的setListAdapter() .
4. ListFragment 类在列表上有一个列表的条目点击监听接口的回调方法, 当用户点击另一个标题时,将调用onListItemClick(), 然后通过showDetails() 切换为与该标题对应的文本.
5. TitleFragment 标题碎片与前面的细节碎片DetailsFragment另一个不同之处在于, 销毁并重新创建此碎片时,将状态保存在一个包中,并在onCreate() 中读取它, 与在Activity布局上交换FrameLayout 的DetailFragment不同,只需考虑一个标题碎片. 所以当配置更改并且标题碎片经历保存并还原操作时,需要记住当前哪个位置。
主要代码如下:
public class TitlesFragment extends ListFragment {
private MainActivity myActivity = null;
int mCurCheckPosition = 0;
@Override
public void onInflate(Activity act, AttributeSet attrs, Bundle icicle) {
for (int i = 0; i < attrs.getAttributeCount(); i++) {
Log.v(MainActivity.TAG, " " + attrs.getAttributeName(i) + " = " + attrs.getAttributeValue(i));
}
super.onInflate(null, attrs, icicle);
/*
* See defect 14796. onInflate() is getting called after onCreateView()
<span style="white-space:pre"> </span> * when the orientation changes from landscape to portrait. If we try to set
* arguments, we get an IllegalStateException. Bundle bundle = this.getArguments();
<span style="white-space:pre"> </span> * if(bundle == null) bundle = new Bundle(); bundle.putString("Dave",
* "Dave"); this.setArguments(bundle);
*/
}
@Override
public void onAttach(Activity myActivity) {
super.onAttach(myActivity);
this.myActivity = (MainActivity) myActivity;
}
@Override
public void onCreate(Bundle icicle) {
if (icicle != null) {
for (String key : icicle.keySet()) {
Log.v(MainActivity.TAG, " " + key);
}
}
super.onCreate(icicle);
if (icicle != null) {
// Restore last state for checked position. 还原<span style="font-family: Arial, Helvetica, sans-serif;">
<span style="white-space:pre"> </span> //最后选中的位置的</span><span style="font-family: Arial, Helvetica, sans-serif;">状态,</span>
mCurCheckPosition = icicle.getInt("curChoice", 0);
}
}
@Override
public View onCreateView(LayoutInflater myInflater, ViewGroup container, Bundle icicle) {
return super.onCreateView(myInflater, container, icicle);
}
@Override
public void onActivityCreated(Bundle icicle) {
if (icicle != null) {
for (String key : icicle.keySet()) {
Log.v(MainActivity.TAG, " " + key);
}
}
super.onActivityCreated(icicle);
// Populate list with our static array of titles.
setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, Shakespeare.TITLES));
ListView lv = getListView();
lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
lv.setSelection(mCurCheckPosition);
myActivity.showDetails(mCurCheckPosition);
}
@Override
public void onSaveInstanceState(Bundle icicle) {
super.onSaveInstanceState(icicle);
icicle.putInt("curChoice", mCurCheckPosition);
}
@Override
public void onListItemClick(ListView l, View v, int pos, long id) {
myActivity.showDetails(pos);
mCurCheckPosition = pos;
}
@Override
public void onDetach() {
super.onDetach();
myActivity = null;
}}