我有Fragment
接口OnWeekViewClickListener
:
public interface OnWeekViewClickListener {
// TODO: Update argument type and name
public void onWeekViewClick();
}
我在onAttach
方法中分配了监听器:
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
this.activity = activity;
try {
mListener = (OnWeekViewClickListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnFragmentInteractionListener");
}
}
但我的Activity
implements OnWeekViewClickListener
从未收到过回电话?也没有任何错误?
以下是该活动:
public class RoomDetailsActivity extends FragmentActivity implements RoomAmenityHorizontalViewer.OnFragmentInteractionListener, DayScheduleViewer.OnWeekViewClickListener {
private RoomAmenityHorizontalViewer amenityFrag = RoomAmenityHorizontalViewer.newInstance();
private DayScheduleViewer dayFrag = DayScheduleViewer.newInstance();
private FrameLayout dayViewFrame;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(getWindow().FEATURE_NO_TITLE);
setContentView(R.layout.detail_view_info);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.room_amenity_spinner_view, amenityFrag, "amenityFrag");
transaction.replace(R.id.room_dayview_layout, dayFrag, "dayFrag");
transaction.commit();
ImageButton backButton = (ImageButton) findViewById(R.id.roomDetailsBackBtn);
backButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent myBackIntent = new Intent(getBaseContext(), ProximityActivity.class);
startActivity(myBackIntent);
}
});
RoundedImageView pinOverlay = (RoundedImageView) findViewById(R.id.imageView4);
ImageView roomImage = (ImageView) findViewById(R.id.roomImageView);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_room_details, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onWeekViewClick() {
Log.v("Fragment Interaction", "Fragment item touched!");
}
@Override
public void onFragmentInteraction(Uri uri) {
}
}
这里片段正在调用监听器:
myWeekView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mListener.onWeekViewClick();
}
});