1、背景
如图,在图片中,要实现ListFragment中点击的不同,就要实现对DetailFragment的更新,在DetailFragment中的内容出现变化时,ListFragment也要做相应的变化,那么如何,实现DetailFragment与ListFragment之间的通信呢?
2、实现方式
为了保持Fragment的独立性,我们可以在Fragment中定义回调接口,委托托管的Activity来完成那些不应该由Fragment处理的任务,托管activity将实现回调接口,履行托管Fragment的任务
1、在ListFragment中定义了一个内部接口,
public class CrimeListFragment extends ListFragment {
private Callbacks mCallbacks;
public interface Callbacks{
void onCrimeSelected(Crime crime);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mCallbacks=(Callbacks) activity;
}
@Override
public void onDetach() {
// TODO Auto-generated method stub
super.onDetach();
mCallbacks=null;
}
2、同时在托管的Activty中实现这个接口,并重写了接口中的onCrimeSelected(Crime crime)方法,该方法主要用于当点击ListFragment中的item时,更新DetailFragment,
public class CrimeListActivity extends SingleFragmentActivity implements CrimeListFragment.Callbacks{
@Override
public void onCrimeSelected(Crime crime) {
FragmentManager fm=getSupportFragmentManager();
FragmentTransaction ft=fm.beginTransaction();
Fragment oldDetail=fm.findFragmentById(R.id.detailFragmentContainer);
Fragment newDetial=CrimeFragment.newInstance(crime.getId());
if(oldDetail!=null){
ft.remove(oldDetail);
}
ft.add(R.id.detailFragmentContainer, newDetial);
ft.commit();
}
}
}
3、那么如何在ListFragment中实现呢?首先在ListFragment被托管在Activity时,实例化mCallback对象,当Fragment被托管的Activity移除时,设置
mCallback对象为空,因为ListFragment不被托管时,更新界面是不起作用的。同时当点击ListFragment中的数据时,调用
mCallback.onCrimeSelected()方法
<pre name="code" class="java">public class CrimeListFragment extends ListFragment {
public void onListItemClick(ListView l, View v, int position, long id) {
// get the Crime from the adapter
Crime c = ((CrimeAdapter)getListAdapter()).getItem(position);
// start an instance of CrimePagerActivity
// Intent i = new Intent(getActivity(), CrimePagerActivity.class);
// i.putExtra(CrimeFragment.EXTRA_CRIME_ID, c.getId());
// startActivityForResult(i, 0);
mCallbacks.onCrimeSelected(c);
public interface Callbacks{
void onCrimeSelected(Crime crime);
}
public void UpdateUI(){
((CrimeAdapter)getListAdapter()).notifyDataSetChanged();
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mCallbacks=(Callbacks) activity;
}
@Override
public void onDetach() {
// TODO Auto-generated method stub
super.onDetach();
mCallbacks=null;
}
}
mCallback对象其实还是一个Activity对象,所以在此方法中调用的onCrimeSelected()方法就是activity中被重写的
onCrimeSelected()方法,那么ListFragment更新Detail界面的流程就是这样。同理,在DetailFragment中更新ListFragment中的数据,只需要在DetailFragment中声明一个接口,同时在Activity中实现,在实现的过程中调用ListFragment中的 UpdateUI方法,就可以实现DetailFragment中的数据变化后ListFragment中的界面也会相应的进行改变,具体代码如下:
public class CrimeFragment extends Fragment {
/**
*
* @author user
* Required interface for hosting activites.
*/
public interface CallBacks{
void onCrimeUpdated(Crime crime);
}
@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
mCallbacks=(CallBacks) activity;
}
@Override
public void onDetach() {
// TODO Auto-generated method stub
super.onDetach();
mCallbacks=null;
}
{
在Activity中重写DetailFragment中的方法
public class CrimeListActivity extends SingleFragmentActivity implements CrimeListFragment.Callbacks,CrimeFragment.CallBacks {
@Override
public void onCrimeUpdated(Crime crime) {
// TODO Auto-generated method stub
FragmentManager fm=getSupportFragmentManager();
CrimeListFragment listFragment=(CrimeListFragment) fm.findFragmentById(R.id.fragmentContainer);
listFragment.UpdateUI();
}
}