偵聽器是隻是在JAVA中的一個接口。所以你不能創建一個接口的對象(「新」運算符)
1)創建一個接口
public Interface OnUpdateGridView2Listener{
public void onUpdate() //Add the kind of data you want in the parameters
}
2)gridView1的適配器創建界面中的一員,並調用其方法
public GidViewAdapter1 extends Adapter{
private OnUpdateGridView2Listener myListener;
//Your code
@Override
public void notifyDataSetChanged(){
//Your code
myListener.onUpdate();
}
public void setOnUpdateGridView2Listener(OnUpdateGridView2Listener list){
this.myListener = list;
}
}
3)實現在片段中的接口,並通知適配器
public FragmentGridView2 extends Fragment implements OnUpdateGridView2Listener{
@Override
public void onUpdate(){
//Your code
gridView2Adapter.notifyDataSetChanged();
}
}
4)在你的活動中,當你創建你的分片時設置監聽器GridView 1
public YourActivity{
//Your code
FragmentGridView1 fragment = new FragmentGridView1();
fragment.setOnUpdateGridView2Listener(getApplication());
}
或者,你可以使用EventBus或Otto庫。 – 2014-11-05 14:16:49