您應該將播放器的UI邏輯與管理廣告點按的處理程式分開。 其中一個方法是為活動實作多個片段。
實作一個片段以包含 MediaPlayer
.
此片段應該呼叫 notifyClick()
和將負責視訊播放。
public class PlayerFragment extends SherlockFragment {
...
public void notifyAdClick () {
_mediaPlayer.notifyClick();
}
...
}
實作不同的片段來顯示UI元素(表示廣告可點按)、監視該UI元素,並將使用者點按的片段傳達給包含 MediaPlayer
.
此片段應該宣告介面以進行片段通訊。 片段在其期間擷取介面實施 onAttach()
生命週期方法,並可呼叫介面方法來與活動通訊。
public class PlayerClickableAdFragment extends SherlockFragment {
private ViewGroup viewGroup;
private Button button;
OnAdUserInteraction callback;
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
// the custom fragment is defined by a custom button
viewGroup = (ViewGroup) inflater.inflate(R.layout.fragment_player_clickable_ad,
container, false);
button = (Button) viewGroup.findViewById(R.id.clickButton);
// register a click listener to detect user interaction
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// send the event back to the activity
callback.onAdClick();
}
});
viewGroup.setVisibility(View.INVISIBLE);
return viewGroup;
}
public void hide() {
viewGroup.setVisibility(View.INVISIBLE);
}
public void show() {
viewGroup.setVisibility(View.VISIBLE);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// attaches the interface implementation
// if the container activity does not implement the methods
// from the interface an exception will be thrown
try {
callback = (OnAdUserInteraction) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnAdUserInteraction");
}
}
// user defined interface that allows fragment communication
// must be implemented by the container activity
public interface OnAdUserInteraction {
public void onAdClick();
}
}