我正在创建一个包含var styles = document.createElement('link');
styles.type="text/css";
styles.rel="stylesheet";
styles.href='../scss/Overlay.scss';
document.getElementsByTagName('head')[0].appendChild(styles);
和Fragment
的应用,其中ListView
包含ListView
。看起来像这样:
every ListView item includes button
要在ListView中显示项目,我有一个button
。似乎将每个按钮都裁剪为自己的ArrayAdapter
的唯一正确方法是在MediaPlayer
中将onClickListener
设置为按钮右:
ArrayAdapter
我想在我的应用程序中控制AudioFocus,所以我需要一个public class PlaceAdapter extends ArrayAdapter<Place> {
private MediaPlayer mMediaPlayer;
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
//...
Button buttonVoice = listItemView.findViewById(R.id.place_button_voice);
if (currentPlace.isHaveVoice()) {
buttonVoice.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
releaseMediaPlayer();
mMediaPlayer = MediaPlayer.create(mContext, currentPlace.getVoiceResourceID());
mMediaPlayer.start();
mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
releaseMediaPlayer();
}
});
}
});
}
}
//...
}
。如果我在AudioManager
中创建AudioManager
,则会占用大量内存。这就是为什么我需要在我的ArrayAdapter
中创建它的原因:
Fragment
在public class HotelsFragment() extends Fragment {
private AudioManager mAudioManager;
private AudioFocusRequest mAudioFocusRequest;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mAudioManager = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_GAME)
.setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
.build();
mAudioFocusRequest = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN_TRANSIENT)
.setAudioAttributes(audioAttributes)
.setAcceptsDelayedFocusGain(true)
.setOnAudioFocusChangeListener(placeAdapter.onAudioFocusChangeListener()).build();
//...
}
}
中创建onAudioFocusChangeListener
是否正确
因为我需要访问mMediaPlayer变量?
ArrayAdapter