我有一个包含多个片段的Activity。片段都在MasterActivty中引用数据对象。 MasterActivtiy可以刷新其数据。
如何通知数据更改的片段?
我尝试过广播公司和其他方法,但我没有理解如何做到这一点。
我查看了这些问题One,two。但他们对我的实施没有任何意义。我使用FragmentPagerAdapter
加载不同的标签。
这是我的代码:
MasterActivity.java
public class MasterActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_master);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.ic_details));
tabLayout.addTab(tabLayout.newTab().setIcon(R.drawable.ic_notes));
final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
final PagerAdapter adapter = new TabPagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
refreshData();
}
public void refreshData() {
//do a background task to get data
//WHEN FINISHED TELL FRAGMENTS TO reloadData()
}
}
TabPagerAdapter.java
public class TabPagerAdapter extends FragmentPagerAdapter {
int tabCount;
public TabPagerAdapter(FragmentManager fm, int numberOfTabs) {
super(fm);
this.tabCount = numberOfTabs;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
DetailsFragment tab1 = new DetailsFragment();
return tab1;
case 1:
NotesFragment tab4 = new NotesFragment();
return tab1;
default:
return null;
}
}
@Override
public int getCount() {
return tabCount;
}
}
片段
public class DetailsFragment extends Fragment {
private String TAG = getClass().getSimpleName();
public DetailsFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_details, container, false);
viewStatus = (LinearLayout) view.findViewById(R.id.viewStatus);
scrollViewDetails = (LinearLayout) view.findViewById(R.id.scrollViewDetails);
return view;
}
private void reloadData() {
Log.i(TAG, "REALOAD DETAILS FRAGMENT");
//Fill the text views with the data from the MasterActivity
}
}
答案 0 :(得分:1)
我通常这样做的方式是:
定义界面
public interface DataReloadListener {
void onDataReloaded(Object data); // use your data type here
}
让活动能够注册/取消注册impls(片段)
向您的活动添加数据结构以跟踪听众:
private List<DataReloadListener> mListeners;
不要忘记在构造函数中初始化:
mListeners = new ArrayList<>();
将注册/取消注册方法添加到活动:
public synchronized void registerDataReloadListener(DataReloadListener listener) {
mListeners.add(listener);
}
public synchronized void unregisterDataReloadListener(DataReloadListener listener) {
mListeners.remove(listener);
}
让片段实现接口并注册/取消注册事件
@Override
public void onDataReloaded(Object data) {
// update any fragment state here
// if you are updating the UI, check that the fragment currently has a view
if (getView() != null) {
// update UI here
}
}
覆盖片段中的onAttach()
和onDetach()
以注册/取消注册:
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((MasterActivity) activity).registerDataReloadListener(this);
}
@Override
public void onDetach() {
((MasterActivity) getActivity()).unregisterDataReloadListener(this);
super.onDetach();
}
当事件发生时,为活动中所有已注册的听众调用onDataReloaded
public synchronized void onDataReloaded(Object data) {
for (DataReloadListener listener : mListeners) {
listener.onDataReloaded(data);
}
}
所有这些基础设施都是必要的,因为您的片段在活动时可能存在,也可能不存在,具体取决于用户如何滑动。