【发布时间】:2015-08-06 01:02:55
【问题描述】:
我了解Fragments 可以通过onAttach() 和implementations 等Listeners 的方法从父Activity 接收信息,但这些方法涉及仅在每个Fragment 的概念下传递信息。我想知道如何在Fragment 运行时从Activity 更改Fragment 中的信息;例如,就我而言,我有一个Navigation Drawer,其中包含检查项目作为我的父导航Activity。检查对应于ArrayList 的哪些项目应显示在Fragment. 中
如何在不重新创建Fragment 的情况下使我的Fragment's ArrayList 立即响应父Activity 中的更改?
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, final int childPosition, long id) {
checkBox = (CheckBox) v.findViewById(R.id.show_child_subject_checkBox);
checkBox.toggle();
tempSI = SubjectInfo.findById(SubjectInfo.class,
childItem.get(groupPosition).get(childPosition).getId());
onSubjectCheck(tempSI);
tempSI.subjectChecked = !tempSI.subjectChecked;
tempSI.save();
childItem.get(groupPosition).set(childPosition, tempSI);
myAdapter.notifyDataSetChanged();
Toast.makeText(getApplicationContext(), "Toggled " +groupPosition + "|" + childPosition,
Toast.LENGTH_SHORT).show();
// the oncheckedchangedListener now resides and operates out of adapter
// in a much more efficient, clean manner
return true;
}
onSubjectCheck是Activity中Listener的实现。也许这不是最好的方法,但我想我至少会展示我是如何玩弄这个想法的。
@Override
public void onSubjectCheck(SubjectInfo si) {
for (int go = 0; go < subjectList.size(); go++) {
if (subjectList.get(go).equals(si)) {
si = new SubjectInfo(si.subjectName, si.itemHeaderTitle, si.subjectGrade,
si.subjectArchived, !si.subjectChecked);
amFragment.subjectList.set(go, si);
}
}
amFragment.sorterAndFilter(false);
}
【问题讨论】:
-
使用接口将数据从活动传递到正在运行的片段,借助以下链接laaptu.wordpress.com/tag/…
标签: java android android-fragments arraylist listener