我的应用程序的布局看起来像这样 LAyout
这是由适配器膨胀,创建一个包含许多此类布局的列表视图,其中布局的内容根据位置而变化。 一切都很好。
现在我需要在playButton上设置onClickListener,它将播放非常短的音乐,其imagesrc将从播放图像更改为暂停图像。
我通过在我充气的布局中的每个playButton上设置onClickListeners来实现在适配器中播放音乐。在getView()中。 (这是最好的方法吗?)
现在这也很有效,直到playButtton被点击时我才尝试更改imagesrc。
最后一个布局是适配器膨胀,只有它的imagesrc在点击时会发生变化。这就是无论我点击哪个布局的playButton,都会播放正确的音乐,但是最后一个布局中由适配器充气的playButton中的imagesrc被更改了。 为什么会这样?
这是我的适配器的代码
public class MyAdapter extends ArrayAdapter<MyCustomObject> {
private static AudioManager mAudioManager;
private static MediaPlayer mMedia;
private ImageView mPlayButton;
private Context mContext;
/**
* A custom created Constructor
* this is used to inflate a custom created layout file
*/
public MyAdapter(Activity context, ArrayList<MyCustomObject> wordArrayList) {
super(context, 0, wordArrayList);
this.mContext = context;
}
private MediaPlayer.OnCompletionListener mCompletionListener = new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mPlayButton.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.ic_action_play));
releaseMediaPlayer();
}
};
public static AudioManager.OnAudioFocusChangeListener mOnAudioFocusChangeListener = new AudioManager.OnAudioFocusChangeListener() {
@Override
public void onAudioFocusChange(int focusChange) {
if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT ||
focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK) {
mMedia.pause();
mMedia.seekTo(0);
} else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
mMedia.start();
} else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
releaseMediaPlayer();
}
}
};
public static void releaseMediaPlayer() {
if (mMedia != null) {
mMedia.release();
mMedia = null;
mAudioManager.abandonAudioFocus(mOnAudioFocusChangeListener);
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
View currentView = convertView;
if (currentView == null)
currentView = LayoutInflater.from(getContext()).inflate(R.layout.layout_to_be_inflated, parent, false);
final MyCustomObject currentObject = getItem(position);
/**Setting the {@id englishID} of the miwokenglishlist.xml
* to the the desired english value
*/
//Getting the ID
TextView defalutTextView = (TextView) currentView.findViewById(R.id.englishID);
//Changing the text of the view
defalutTextView.setText(currentWordObject.getEnglishWord());
/**
* Setting the {@id miwokID} of the miwokenglishlist.xml
* to the current miwok word
*/
// Getting the ID
TextView secondaryTextView = (TextView) currentView.findViewById(R.id.secondID);
// Changing the text to the current miwok word
miwokTextView.setText(currentObject.getSecondWord());
/**
* Checking whether current Word object has a image associated with it or not
*/
// Getting the image id from the current word object
int currImageID = currentObject.getImageResourceID();
// Getting the ID for the image in miwokenglishlist.xml
ImageView imageViewID = (ImageView) currentView.findViewById(R.id.imageID);
// if image is present for the current object
if (currImageID != -1) {
// Setting the image on the layout
imageViewID.setImageResource(currImageID);
}
// if image is not present for the current object
else {
imageViewID.setVisibility(View.GONE);
}
mPlayButton = (ImageView) currentView.findViewById(R.id.imageButton);
mPlayButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// If there are already underlying resources
releaseMediaPlayer();
int result = mAudioManager.requestAudioFocus(mOnAudioFocusChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
mMedia = MediaPlayer.create(getContext(), currentWordObject.getPronunciation());
mMedia.start();
mMedia.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
`//Changing the playButton resource to pause`
mPlayButton.setImageDrawable(ContextCompat.getDrawable(mContext,
R.drawable.ic_action_pause));
}
});
mMedia.setOnCompletionListener(mCompletionListener);
}
}
});
// Returning the modified inflated View
return currentView;
}
}
那我该怎么做?
UPDATE:我解决了这个问题,我在mPlayButton id上调用了setDrawablesrc,它引用了最后一个布局中的playButton。所以,我在onClickListener的onClick方法中传递的视图上调用了setDrawablesrc。 这是更新的代码
mPlayButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
// If there are already underlying resources
releaseMediaPlayer();
int result = mAudioManager.requestAudioFocus(mOnAudioFocusChangeListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
//Storing the view in the instance variable to refer to it in the onCompletionlIstener of the MediaPlayer
mPlayButtonView = (ImageView) view;
mMedia = MediaPlayer.create(getContext(), currentWordObject.getPronunciation());
mMedia.start();
mMedia.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
//Changing the imagesrc of the view that is passed
((ImageView)view).setImageDrawable(ContextCompat.getDrawable(mContext,
R.drawable.ic_action_pause));
}
});
mMedia.setOnCompletionListener(mCompletionListener);
}
}
});
在onCompletionListener中,我获取了实例变量并更改了其imagesrc。 工作得很完美! :)
现在有一个问题,这是最好的方法吗?