我想在java中做选择器。屏幕视图包含列表视图。我希望您在用户按下喷枪很短的时间后更换喷枪图像,并影响选择器。但我无法检查ListView项目。我把变化的图片扔进了ArrayList
。我该怎么做呢?
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Vibrator;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;**strong text**
import android.widget.ListView;
import java.util.ArrayList;
public class MakinalilarFragment extends Fragment {
private MediaPlayer mMediaPlayer;
private AudioManager mAudioManager;
private 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) {
mMediaPlayer.pause();
mMediaPlayer.seekTo(0);
} else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
mMediaPlayer.start();
} else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
releaseMediaPlayer();
}
}
};
private MediaPlayer.OnCompletionListener mCompletionListener = new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
releaseMediaPlayer();
}
};
public MakinalilarFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.word_list, container, false);
mAudioManager = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);
final ArrayList<Gun> guns = new ArrayList<Gun>();
guns.add(new Gun(R.string.name_aa_12,R.drawable.aa12_medium, R.raw.aa12_sound,R.drawable.fire_aa12));
guns.add(new Gun(R.string.name_acr,R.drawable.acr_medium, R.raw.acr_sound,R.drawable.fire_acr));
guns.add(new Gun(R.string.name_ak_47,R.drawable.ak47_medium, R.raw.ak47_sound,R.drawable.fire_ak47));
guns.add(new Gun(R.string.name_fal,R.drawable.fal_medium, R.raw.fal_sound,R.drawable.fire_fal));
guns.add(new Gun(R.string.name_intervention,R.drawable.intervention_medium, R.raw.intervention_sound,R.drawable.fire_intervention));
guns.add(new Gun(R.string.name_m1,R.drawable.m1_medium, R.raw.m1_sound,R.drawable.fire_m1));
guns.add(new Gun(R.string.name_m4,R.drawable.m4_medium, R.raw.m4_sound,R.drawable.fire_m4));
GunAdapter adapter = new GunAdapter(getActivity(), guns);
ListView listView = (ListView) rootView.findViewById(R.id.list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
Gun gun = guns.get(position);
int result = mAudioManager.requestAudioFocus(mOnAudioFocusChangeListener,
AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
mMediaPlayer = MediaPlayer.create(getActivity(), gun.getAudioResourceId());
mMediaPlayer.start();
Vibrator v = (Vibrator) getActivity().getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(500);
mMediaPlayer.setOnCompletionListener(mCompletionListener);
}
}
});
return rootView;
}
@Override
public void onStop() {
super.onStop();
releaseMediaPlayer();
}
private void releaseMediaPlayer() {
if (mMediaPlayer != null) {
mMediaPlayer.release();
mMediaPlayer = null;
mAudioManager.abandonAudioFocus(mOnAudioFocusChangeListener);
}
}
}
以下是适配器代码
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
public class GunAdapter extends ArrayAdapter<Gun> {
public GunAdapter(Context context, ArrayList<Gun> guns) {
super(context, 0, guns);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
Gun currentGun = getItem(position);
TextView nameGunTextView = (TextView) listItemView.findViewById(R.id.textView);
nameGunTextView.setText(currentGun.getNameGunId());
ImageView imageView = (ImageView) listItemView.findViewById(R.id.image);
imageView.setImageResource(currentGun.getImageResourceId());
return listItemView;
}
}